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) { |
| 77 | printk("System RAM resource %llx - %llx cannot be added\n", |
| 78 | (unsigned long long)res->start, (unsigned long long)res->end); |
| 79 | kfree(res); |
| 80 | res = NULL; |
| 81 | } |
| 82 | return res; |
| 83 | } |
| 84 | |
| 85 | static void release_memory_resource(struct resource *res) |
| 86 | { |
| 87 | if (!res) |
| 88 | return; |
| 89 | release_resource(res); |
| 90 | kfree(res); |
| 91 | return; |
| 92 | } |
| 93 | |
Keith Mannthey | 5394702 | 2006-09-30 23:27:08 -0700 | [diff] [blame] | 94 | #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 95 | #ifndef CONFIG_SPARSEMEM_VMEMMAP |
Andrea Arcangeli | 5f24ce5 | 2011-01-13 15:47:00 -0800 | [diff] [blame] | 96 | static void get_page_bootmem(unsigned long info, struct page *page, |
| 97 | unsigned long type) |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 98 | { |
Andrea Arcangeli | 5f24ce5 | 2011-01-13 15:47:00 -0800 | [diff] [blame] | 99 | page->lru.next = (struct list_head *) type; |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 100 | SetPagePrivate(page); |
| 101 | set_page_private(page, info); |
| 102 | atomic_inc(&page->_count); |
| 103 | } |
| 104 | |
Rakib Mullick | 23ce932 | 2009-12-14 17:59:44 -0800 | [diff] [blame] | 105 | /* reference to __meminit __free_pages_bootmem is valid |
| 106 | * so use __ref to tell modpost not to generate a warning */ |
| 107 | void __ref put_page_bootmem(struct page *page) |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 108 | { |
Andrea Arcangeli | 5f24ce5 | 2011-01-13 15:47:00 -0800 | [diff] [blame] | 109 | unsigned long type; |
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); |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 119 | __free_pages_bootmem(page, 0); |
| 120 | } |
| 121 | |
| 122 | } |
| 123 | |
Adrian Bunk | d92bc31 | 2008-07-23 21:28:12 -0700 | [diff] [blame] | 124 | static void register_page_bootmem_info_section(unsigned long start_pfn) |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 125 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 126 | unsigned long *usemap, mapsize, page_mapsize, section_nr, i, j; |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 127 | struct mem_section *ms; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 128 | struct page *page, *memmap, *page_page; |
| 129 | int memmap_page_valid; |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 130 | |
| 131 | if (!pfn_valid(start_pfn)) |
| 132 | return; |
| 133 | |
| 134 | section_nr = pfn_to_section_nr(start_pfn); |
| 135 | ms = __nr_to_section(section_nr); |
| 136 | |
| 137 | /* Get section's memmap address */ |
| 138 | memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr); |
| 139 | |
| 140 | /* |
| 141 | * Get page for the memmap's phys address |
| 142 | * XXX: need more consideration for sparse_vmemmap... |
| 143 | */ |
| 144 | page = virt_to_page(memmap); |
| 145 | mapsize = sizeof(struct page) * PAGES_PER_SECTION; |
| 146 | mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT; |
| 147 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 148 | page_mapsize = PAGE_SIZE/sizeof(struct page); |
| 149 | |
| 150 | /* remember memmap's page, except those that reference only holes */ |
| 151 | for (i = 0; i < mapsize; i++, page++) { |
| 152 | memmap_page_valid = 0; |
| 153 | page_page = __va(page_to_pfn(page) << PAGE_SHIFT); |
| 154 | for (j = 0; j < page_mapsize; j++, page_page++) { |
| 155 | if (early_pfn_valid(page_to_pfn(page_page))) { |
| 156 | memmap_page_valid = 1; |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | if (memmap_page_valid) |
| 161 | get_page_bootmem(section_nr, page, SECTION_INFO); |
| 162 | } |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 163 | |
| 164 | usemap = __nr_to_section(section_nr)->pageblock_flags; |
| 165 | page = virt_to_page(usemap); |
| 166 | |
| 167 | mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT; |
| 168 | |
| 169 | for (i = 0; i < mapsize; i++, page++) |
Yasunori Goto | af370fb | 2008-07-23 21:28:17 -0700 | [diff] [blame] | 170 | get_page_bootmem(section_nr, page, MIX_SECTION_INFO); |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 171 | |
| 172 | } |
| 173 | |
| 174 | void register_page_bootmem_info_node(struct pglist_data *pgdat) |
| 175 | { |
| 176 | unsigned long i, pfn, end_pfn, nr_pages; |
| 177 | int node = pgdat->node_id; |
| 178 | struct page *page; |
| 179 | struct zone *zone; |
| 180 | |
| 181 | nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT; |
| 182 | page = virt_to_page(pgdat); |
| 183 | |
| 184 | for (i = 0; i < nr_pages; i++, page++) |
| 185 | get_page_bootmem(node, page, NODE_INFO); |
| 186 | |
| 187 | zone = &pgdat->node_zones[0]; |
| 188 | for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) { |
| 189 | if (zone->wait_table) { |
| 190 | nr_pages = zone->wait_table_hash_nr_entries |
| 191 | * sizeof(wait_queue_head_t); |
| 192 | nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT; |
| 193 | page = virt_to_page(zone->wait_table); |
| 194 | |
| 195 | for (i = 0; i < nr_pages; i++, page++) |
| 196 | get_page_bootmem(node, page, NODE_INFO); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | pfn = pgdat->node_start_pfn; |
| 201 | end_pfn = pfn + pgdat->node_spanned_pages; |
| 202 | |
| 203 | /* register_section info */ |
| 204 | for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) |
| 205 | register_page_bootmem_info_section(pfn); |
| 206 | |
| 207 | } |
| 208 | #endif /* !CONFIG_SPARSEMEM_VMEMMAP */ |
| 209 | |
Heiko Carstens | 76cdd58 | 2008-05-14 16:05:52 -0700 | [diff] [blame] | 210 | static void grow_zone_span(struct zone *zone, unsigned long start_pfn, |
| 211 | unsigned long end_pfn) |
| 212 | { |
| 213 | unsigned long old_zone_end_pfn; |
| 214 | |
| 215 | zone_span_writelock(zone); |
| 216 | |
| 217 | old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages; |
| 218 | if (start_pfn < zone->zone_start_pfn) |
| 219 | zone->zone_start_pfn = start_pfn; |
| 220 | |
| 221 | zone->spanned_pages = max(old_zone_end_pfn, end_pfn) - |
| 222 | zone->zone_start_pfn; |
| 223 | |
| 224 | zone_span_writeunlock(zone); |
| 225 | } |
| 226 | |
| 227 | static void grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn, |
| 228 | unsigned long end_pfn) |
| 229 | { |
| 230 | unsigned long old_pgdat_end_pfn = |
| 231 | pgdat->node_start_pfn + pgdat->node_spanned_pages; |
| 232 | |
| 233 | if (start_pfn < pgdat->node_start_pfn) |
| 234 | pgdat->node_start_pfn = start_pfn; |
| 235 | |
| 236 | pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) - |
| 237 | pgdat->node_start_pfn; |
| 238 | } |
| 239 | |
Al Viro | 3116848 | 2008-11-22 17:33:24 +0000 | [diff] [blame] | 240 | 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] | 241 | { |
| 242 | struct pglist_data *pgdat = zone->zone_pgdat; |
| 243 | int nr_pages = PAGES_PER_SECTION; |
| 244 | int nid = pgdat->node_id; |
| 245 | int zone_type; |
Heiko Carstens | 76cdd58 | 2008-05-14 16:05:52 -0700 | [diff] [blame] | 246 | unsigned long flags; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 247 | |
| 248 | zone_type = zone - pgdat->node_zones; |
Heiko Carstens | 76cdd58 | 2008-05-14 16:05:52 -0700 | [diff] [blame] | 249 | if (!zone->wait_table) { |
| 250 | int ret; |
| 251 | |
| 252 | ret = init_currently_empty_zone(zone, phys_start_pfn, |
| 253 | nr_pages, MEMMAP_HOTPLUG); |
| 254 | if (ret) |
| 255 | return ret; |
| 256 | } |
| 257 | pgdat_resize_lock(zone->zone_pgdat, &flags); |
| 258 | grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages); |
| 259 | grow_pgdat_span(zone->zone_pgdat, phys_start_pfn, |
| 260 | phys_start_pfn + nr_pages); |
| 261 | pgdat_resize_unlock(zone->zone_pgdat, &flags); |
Dave Hansen | a2f3aa0 | 2007-01-10 23:15:30 -0800 | [diff] [blame] | 262 | memmap_init_zone(nr_pages, nid, zone_type, |
| 263 | phys_start_pfn, MEMMAP_HOTPLUG); |
Yasunori Goto | 718127c | 2006-06-23 02:03:10 -0700 | [diff] [blame] | 264 | return 0; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 267 | static int __meminit __add_section(int nid, struct zone *zone, |
| 268 | unsigned long phys_start_pfn) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 269 | { |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 270 | int nr_pages = PAGES_PER_SECTION; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 271 | int ret; |
| 272 | |
KAMEZAWA Hiroyuki | ebd1530 | 2006-08-05 12:15:06 -0700 | [diff] [blame] | 273 | if (pfn_valid(phys_start_pfn)) |
| 274 | return -EEXIST; |
| 275 | |
Dave Hansen | 0b0acbe | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 276 | ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 277 | |
| 278 | if (ret < 0) |
| 279 | return ret; |
| 280 | |
Yasunori Goto | 718127c | 2006-06-23 02:03:10 -0700 | [diff] [blame] | 281 | ret = __add_zone(zone, phys_start_pfn); |
| 282 | |
| 283 | if (ret < 0) |
| 284 | return ret; |
| 285 | |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 286 | return register_new_memory(nid, __pfn_to_section(phys_start_pfn)); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Yasunori Goto | 0c0a4a5 | 2008-04-28 02:13:34 -0700 | [diff] [blame] | 289 | #ifdef CONFIG_SPARSEMEM_VMEMMAP |
| 290 | static int __remove_section(struct zone *zone, struct mem_section *ms) |
| 291 | { |
| 292 | /* |
| 293 | * XXX: Freeing memmap with vmemmap is not implement yet. |
| 294 | * This should be removed later. |
| 295 | */ |
| 296 | return -EBUSY; |
| 297 | } |
| 298 | #else |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 299 | static int __remove_section(struct zone *zone, struct mem_section *ms) |
| 300 | { |
| 301 | unsigned long flags; |
| 302 | struct pglist_data *pgdat = zone->zone_pgdat; |
| 303 | int ret = -EINVAL; |
| 304 | |
| 305 | if (!valid_section(ms)) |
| 306 | return ret; |
| 307 | |
| 308 | ret = unregister_memory_section(ms); |
| 309 | if (ret) |
| 310 | return ret; |
| 311 | |
| 312 | pgdat_resize_lock(pgdat, &flags); |
| 313 | sparse_remove_one_section(zone, ms); |
| 314 | pgdat_resize_unlock(pgdat, &flags); |
| 315 | return 0; |
| 316 | } |
Yasunori Goto | 0c0a4a5 | 2008-04-28 02:13:34 -0700 | [diff] [blame] | 317 | #endif |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 318 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 319 | /* |
| 320 | * Reasonably generic function for adding memory. It is |
| 321 | * expected that archs that support memory hotplug will |
| 322 | * call this function after deciding the zone to which to |
| 323 | * add the new pages. |
| 324 | */ |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 325 | int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn, |
| 326 | unsigned long nr_pages) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 327 | { |
| 328 | unsigned long i; |
| 329 | int err = 0; |
KAMEZAWA Hiroyuki | 6f71271 | 2006-08-05 12:14:58 -0700 | [diff] [blame] | 330 | int start_sec, end_sec; |
| 331 | /* during initialize mem_map, align hot-added range to section */ |
| 332 | start_sec = pfn_to_section_nr(phys_start_pfn); |
| 333 | end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 334 | |
KAMEZAWA Hiroyuki | 6f71271 | 2006-08-05 12:14:58 -0700 | [diff] [blame] | 335 | for (i = start_sec; i <= end_sec; i++) { |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 336 | err = __add_section(nid, zone, i << PFN_SECTION_SHIFT); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 337 | |
KAMEZAWA Hiroyuki | 6f71271 | 2006-08-05 12:14:58 -0700 | [diff] [blame] | 338 | /* |
Simon Arlott | 183ff22 | 2007-10-20 01:27:18 +0200 | [diff] [blame] | 339 | * EEXIST is finally dealt with by ioresource collision |
KAMEZAWA Hiroyuki | 6f71271 | 2006-08-05 12:14:58 -0700 | [diff] [blame] | 340 | * check. see add_memory() => register_memory_resource() |
| 341 | * Warning will be printed if there is collision. |
Joel H Schopp | bed120c | 2006-05-01 12:16:11 -0700 | [diff] [blame] | 342 | */ |
| 343 | if (err && (err != -EEXIST)) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 344 | break; |
KAMEZAWA Hiroyuki | 6f71271 | 2006-08-05 12:14:58 -0700 | [diff] [blame] | 345 | err = 0; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | return err; |
| 349 | } |
Joel H Schopp | bed120c | 2006-05-01 12:16:11 -0700 | [diff] [blame] | 350 | EXPORT_SYMBOL_GPL(__add_pages); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 351 | |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 352 | /** |
| 353 | * __remove_pages() - remove sections of pages from a zone |
| 354 | * @zone: zone from which pages need to be removed |
| 355 | * @phys_start_pfn: starting pageframe (must be aligned to start of a section) |
| 356 | * @nr_pages: number of pages to remove (must be multiple of section size) |
| 357 | * |
| 358 | * Generic helper function to remove section mappings and sysfs entries |
| 359 | * for the section of the memory we are removing. Caller needs to make |
| 360 | * sure that pages are marked reserved and zones are adjust properly by |
| 361 | * calling offline_pages(). |
| 362 | */ |
| 363 | int __remove_pages(struct zone *zone, unsigned long phys_start_pfn, |
| 364 | unsigned long nr_pages) |
| 365 | { |
| 366 | unsigned long i, ret = 0; |
| 367 | int sections_to_remove; |
| 368 | |
| 369 | /* |
| 370 | * We can only remove entire sections |
| 371 | */ |
| 372 | BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK); |
| 373 | BUG_ON(nr_pages % PAGES_PER_SECTION); |
| 374 | |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 375 | sections_to_remove = nr_pages / PAGES_PER_SECTION; |
| 376 | for (i = 0; i < sections_to_remove; i++) { |
| 377 | unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION; |
Nathan Fontenot | de7f0cb | 2008-10-18 20:27:14 -0700 | [diff] [blame] | 378 | release_mem_region(pfn << PAGE_SHIFT, |
| 379 | PAGES_PER_SECTION << PAGE_SHIFT); |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 380 | ret = __remove_section(zone, __pfn_to_section(pfn)); |
| 381 | if (ret) |
| 382 | break; |
| 383 | } |
| 384 | return ret; |
| 385 | } |
| 386 | EXPORT_SYMBOL_GPL(__remove_pages); |
| 387 | |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 388 | int set_online_page_callback(online_page_callback_t callback) |
| 389 | { |
| 390 | int rc = -EINVAL; |
| 391 | |
| 392 | lock_memory_hotplug(); |
| 393 | |
| 394 | if (online_page_callback == generic_online_page) { |
| 395 | online_page_callback = callback; |
| 396 | rc = 0; |
| 397 | } |
| 398 | |
| 399 | unlock_memory_hotplug(); |
| 400 | |
| 401 | return rc; |
| 402 | } |
| 403 | EXPORT_SYMBOL_GPL(set_online_page_callback); |
| 404 | |
| 405 | int restore_online_page_callback(online_page_callback_t callback) |
| 406 | { |
| 407 | int rc = -EINVAL; |
| 408 | |
| 409 | lock_memory_hotplug(); |
| 410 | |
| 411 | if (online_page_callback == callback) { |
| 412 | online_page_callback = generic_online_page; |
| 413 | rc = 0; |
| 414 | } |
| 415 | |
| 416 | unlock_memory_hotplug(); |
| 417 | |
| 418 | return rc; |
| 419 | } |
| 420 | EXPORT_SYMBOL_GPL(restore_online_page_callback); |
| 421 | |
| 422 | void __online_page_set_limits(struct page *page) |
Jeremy Fitzhardinge | 180c06e | 2008-04-28 02:12:03 -0700 | [diff] [blame] | 423 | { |
Jan Beulich | 4738e1b | 2009-09-21 17:03:03 -0700 | [diff] [blame] | 424 | unsigned long pfn = page_to_pfn(page); |
| 425 | |
Jeremy Fitzhardinge | 180c06e | 2008-04-28 02:12:03 -0700 | [diff] [blame] | 426 | totalram_pages++; |
Jack Cheung | 59f9f1c | 2011-11-29 16:52:49 -0800 | [diff] [blame] | 427 | #ifdef CONFIG_FIX_MOVABLE_ZONE |
| 428 | if (zone_idx(page_zone(page)) != ZONE_MOVABLE) |
| 429 | total_unmovable_pages++; |
| 430 | #endif |
Jan Beulich | 4738e1b | 2009-09-21 17:03:03 -0700 | [diff] [blame] | 431 | if (pfn >= num_physpages) |
| 432 | num_physpages = pfn + 1; |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 433 | } |
| 434 | EXPORT_SYMBOL_GPL(__online_page_set_limits); |
| 435 | |
| 436 | void __online_page_increment_counters(struct page *page) |
| 437 | { |
| 438 | totalram_pages++; |
Jeremy Fitzhardinge | 180c06e | 2008-04-28 02:12:03 -0700 | [diff] [blame] | 439 | |
| 440 | #ifdef CONFIG_HIGHMEM |
| 441 | if (PageHighMem(page)) |
| 442 | totalhigh_pages++; |
| 443 | #endif |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 444 | } |
| 445 | EXPORT_SYMBOL_GPL(__online_page_increment_counters); |
Jeremy Fitzhardinge | 180c06e | 2008-04-28 02:12:03 -0700 | [diff] [blame] | 446 | |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 447 | void __online_page_free(struct page *page) |
| 448 | { |
Jeremy Fitzhardinge | 180c06e | 2008-04-28 02:12:03 -0700 | [diff] [blame] | 449 | ClearPageReserved(page); |
| 450 | init_page_count(page); |
| 451 | __free_page(page); |
| 452 | } |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 453 | EXPORT_SYMBOL_GPL(__online_page_free); |
| 454 | |
| 455 | static void generic_online_page(struct page *page) |
| 456 | { |
| 457 | __online_page_set_limits(page); |
| 458 | __online_page_increment_counters(page); |
| 459 | __online_page_free(page); |
| 460 | } |
Jeremy Fitzhardinge | 180c06e | 2008-04-28 02:12:03 -0700 | [diff] [blame] | 461 | |
KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 462 | static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages, |
| 463 | void *arg) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 464 | { |
| 465 | unsigned long i; |
KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 466 | unsigned long onlined_pages = *(unsigned long *)arg; |
| 467 | struct page *page; |
| 468 | if (PageReserved(pfn_to_page(start_pfn))) |
| 469 | for (i = 0; i < nr_pages; i++) { |
| 470 | page = pfn_to_page(start_pfn + i); |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 471 | (*online_page_callback)(page); |
KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 472 | onlined_pages++; |
| 473 | } |
| 474 | *(unsigned long *)arg = onlined_pages; |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | |
KOSAKI Motohiro | 839a4fc | 2011-05-24 17:11:31 -0700 | [diff] [blame] | 479 | int __ref online_pages(unsigned long pfn, unsigned long nr_pages) |
KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 480 | { |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 481 | unsigned long onlined_pages = 0; |
| 482 | struct zone *zone; |
Yasunori Goto | 6811378 | 2006-06-23 02:03:11 -0700 | [diff] [blame] | 483 | int need_zonelists_rebuild = 0; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 484 | int nid; |
| 485 | int ret; |
| 486 | struct memory_notify arg; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 487 | |
KAMEZAWA Hiroyuki | 925268a | 2011-01-11 16:44:01 +0900 | [diff] [blame] | 488 | lock_memory_hotplug(); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 489 | arg.start_pfn = pfn; |
| 490 | arg.nr_pages = nr_pages; |
| 491 | arg.status_change_nid = -1; |
| 492 | |
| 493 | nid = page_to_nid(pfn_to_page(pfn)); |
| 494 | if (node_present_pages(nid) == 0) |
| 495 | arg.status_change_nid = nid; |
| 496 | |
| 497 | ret = memory_notify(MEM_GOING_ONLINE, &arg); |
| 498 | ret = notifier_to_errno(ret); |
| 499 | if (ret) { |
| 500 | memory_notify(MEM_CANCEL_ONLINE, &arg); |
KAMEZAWA Hiroyuki | 925268a | 2011-01-11 16:44:01 +0900 | [diff] [blame] | 501 | unlock_memory_hotplug(); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 502 | return ret; |
| 503 | } |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 504 | /* |
| 505 | * This doesn't need a lock to do pfn_to_page(). |
| 506 | * The section can't be removed here because of the |
Daniel Walker | da19cbc | 2008-02-04 23:35:47 -0800 | [diff] [blame] | 507 | * memory_block->state_mutex. |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 508 | */ |
| 509 | zone = page_zone(pfn_to_page(pfn)); |
Yasunori Goto | 6811378 | 2006-06-23 02:03:11 -0700 | [diff] [blame] | 510 | /* |
| 511 | * If this zone is not populated, then it is not in zonelist. |
| 512 | * This means the page allocator ignores this zone. |
| 513 | * So, zonelist must be updated after online. |
| 514 | */ |
Haicheng Li | 4eaf3f6 | 2010-05-24 14:32:52 -0700 | [diff] [blame] | 515 | mutex_lock(&zonelists_mutex); |
Yasunori Goto | 6811378 | 2006-06-23 02:03:11 -0700 | [diff] [blame] | 516 | if (!populated_zone(zone)) |
| 517 | need_zonelists_rebuild = 1; |
| 518 | |
KAMEZAWA Hiroyuki | 908eedc | 2009-09-22 16:45:46 -0700 | [diff] [blame] | 519 | ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages, |
KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 520 | online_pages_range); |
Geoff Levand | fd8a422 | 2008-05-14 16:05:50 -0700 | [diff] [blame] | 521 | if (ret) { |
Haicheng Li | 4eaf3f6 | 2010-05-24 14:32:52 -0700 | [diff] [blame] | 522 | mutex_unlock(&zonelists_mutex); |
Geoff Levand | fd8a422 | 2008-05-14 16:05:50 -0700 | [diff] [blame] | 523 | printk(KERN_DEBUG "online_pages %lx at %lx failed\n", |
| 524 | nr_pages, pfn); |
| 525 | memory_notify(MEM_CANCEL_ONLINE, &arg); |
KAMEZAWA Hiroyuki | 925268a | 2011-01-11 16:44:01 +0900 | [diff] [blame] | 526 | unlock_memory_hotplug(); |
Geoff Levand | fd8a422 | 2008-05-14 16:05:50 -0700 | [diff] [blame] | 527 | return ret; |
| 528 | } |
| 529 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 530 | zone->present_pages += onlined_pages; |
Yasunori Goto | f2937be | 2006-03-09 17:33:51 -0800 | [diff] [blame] | 531 | zone->zone_pgdat->node_present_pages += onlined_pages; |
Jack Cheung | aa7e9de | 2012-01-19 17:06:04 -0800 | [diff] [blame] | 532 | drain_all_pages(); |
Haicheng Li | 1f52250 | 2010-05-24 14:32:51 -0700 | [diff] [blame] | 533 | if (need_zonelists_rebuild) |
| 534 | build_all_zonelists(zone); |
| 535 | else |
| 536 | zone_pcp_update(zone); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 537 | |
Haicheng Li | 4eaf3f6 | 2010-05-24 14:32:52 -0700 | [diff] [blame] | 538 | mutex_unlock(&zonelists_mutex); |
KOSAKI Motohiro | 1b79acc | 2011-05-24 17:11:32 -0700 | [diff] [blame] | 539 | |
| 540 | init_per_zone_wmark_min(); |
| 541 | |
Christoph Lameter | 7ea1530 | 2007-10-16 01:25:29 -0700 | [diff] [blame] | 542 | if (onlined_pages) { |
| 543 | kswapd_run(zone_to_nid(zone)); |
| 544 | node_set_state(zone_to_nid(zone), N_HIGH_MEMORY); |
| 545 | } |
Dave Hansen | 61b1399 | 2005-10-29 18:16:56 -0700 | [diff] [blame] | 546 | |
Haicheng Li | 1f52250 | 2010-05-24 14:32:51 -0700 | [diff] [blame] | 547 | vm_total_pages = nr_free_pagecache_pages(); |
Kent Liu | 2f7f24e | 2008-07-23 21:28:18 -0700 | [diff] [blame] | 548 | |
Chandra Seetharaman | 2d1d43f | 2006-09-29 02:01:25 -0700 | [diff] [blame] | 549 | writeback_set_ratelimit(); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 550 | |
| 551 | if (onlined_pages) |
| 552 | memory_notify(MEM_ONLINE, &arg); |
KAMEZAWA Hiroyuki | 925268a | 2011-01-11 16:44:01 +0900 | [diff] [blame] | 553 | unlock_memory_hotplug(); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 554 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 555 | return 0; |
| 556 | } |
Keith Mannthey | 5394702 | 2006-09-30 23:27:08 -0700 | [diff] [blame] | 557 | #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */ |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 558 | |
Hidetoshi Seto | e131933 | 2009-11-17 14:06:18 -0800 | [diff] [blame] | 559 | /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */ |
| 560 | static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start) |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 561 | { |
| 562 | struct pglist_data *pgdat; |
| 563 | unsigned long zones_size[MAX_NR_ZONES] = {0}; |
| 564 | unsigned long zholes_size[MAX_NR_ZONES] = {0}; |
| 565 | unsigned long start_pfn = start >> PAGE_SHIFT; |
| 566 | |
| 567 | pgdat = arch_alloc_nodedata(nid); |
| 568 | if (!pgdat) |
| 569 | return NULL; |
| 570 | |
| 571 | arch_refresh_nodedata(nid, pgdat); |
| 572 | |
| 573 | /* we can use NODE_DATA(nid) from here */ |
| 574 | |
| 575 | /* 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] | 576 | free_area_init_node(nid, zones_size, start_pfn, zholes_size); |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 577 | |
KAMEZAWA Hiroyuki | 959ecc4 | 2011-06-15 15:08:38 -0700 | [diff] [blame] | 578 | /* |
| 579 | * The node we allocated has no zone fallback lists. For avoiding |
| 580 | * to access not-initialized zonelist, build here. |
| 581 | */ |
David Rientjes | f957db4 | 2011-06-22 18:13:04 -0700 | [diff] [blame] | 582 | mutex_lock(&zonelists_mutex); |
KAMEZAWA Hiroyuki | 959ecc4 | 2011-06-15 15:08:38 -0700 | [diff] [blame] | 583 | build_all_zonelists(NULL); |
David Rientjes | f957db4 | 2011-06-22 18:13:04 -0700 | [diff] [blame] | 584 | mutex_unlock(&zonelists_mutex); |
KAMEZAWA Hiroyuki | 959ecc4 | 2011-06-15 15:08:38 -0700 | [diff] [blame] | 585 | |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 586 | return pgdat; |
| 587 | } |
| 588 | |
| 589 | static void rollback_node_hotadd(int nid, pg_data_t *pgdat) |
| 590 | { |
| 591 | arch_refresh_nodedata(nid, NULL); |
| 592 | arch_free_nodedata(pgdat); |
| 593 | return; |
| 594 | } |
| 595 | |
KAMEZAWA Hiroyuki | 0a54703 | 2006-06-27 02:53:35 -0700 | [diff] [blame] | 596 | |
minskey guo | cf23422 | 2010-05-24 14:32:41 -0700 | [diff] [blame] | 597 | /* |
| 598 | * called by cpu_up() to online a node without onlined memory. |
| 599 | */ |
| 600 | int mem_online_node(int nid) |
| 601 | { |
| 602 | pg_data_t *pgdat; |
| 603 | int ret; |
| 604 | |
KOSAKI Motohiro | 20d6c96 | 2010-12-02 14:31:19 -0800 | [diff] [blame] | 605 | lock_memory_hotplug(); |
minskey guo | cf23422 | 2010-05-24 14:32:41 -0700 | [diff] [blame] | 606 | pgdat = hotadd_new_pgdat(nid, 0); |
David Rientjes | 7553e8f | 2011-06-22 18:13:01 -0700 | [diff] [blame] | 607 | if (!pgdat) { |
minskey guo | cf23422 | 2010-05-24 14:32:41 -0700 | [diff] [blame] | 608 | ret = -ENOMEM; |
| 609 | goto out; |
| 610 | } |
| 611 | node_set_online(nid); |
| 612 | ret = register_one_node(nid); |
| 613 | BUG_ON(ret); |
| 614 | |
| 615 | out: |
KOSAKI Motohiro | 20d6c96 | 2010-12-02 14:31:19 -0800 | [diff] [blame] | 616 | unlock_memory_hotplug(); |
minskey guo | cf23422 | 2010-05-24 14:32:41 -0700 | [diff] [blame] | 617 | return ret; |
| 618 | } |
| 619 | |
Al Viro | 3116848 | 2008-11-22 17:33:24 +0000 | [diff] [blame] | 620 | /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */ |
| 621 | int __ref add_memory(int nid, u64 start, u64 size) |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 622 | { |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 623 | pg_data_t *pgdat = NULL; |
| 624 | int new_pgdat = 0; |
KAMEZAWA Hiroyuki | ebd1530 | 2006-08-05 12:15:06 -0700 | [diff] [blame] | 625 | struct resource *res; |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 626 | int ret; |
| 627 | |
KOSAKI Motohiro | 20d6c96 | 2010-12-02 14:31:19 -0800 | [diff] [blame] | 628 | lock_memory_hotplug(); |
Andi Kleen | 6ad696d | 2009-11-17 14:06:22 -0800 | [diff] [blame] | 629 | |
KAMEZAWA Hiroyuki | ebd1530 | 2006-08-05 12:15:06 -0700 | [diff] [blame] | 630 | res = register_memory_resource(start, size); |
Andi Kleen | 6ad696d | 2009-11-17 14:06:22 -0800 | [diff] [blame] | 631 | ret = -EEXIST; |
KAMEZAWA Hiroyuki | ebd1530 | 2006-08-05 12:15:06 -0700 | [diff] [blame] | 632 | if (!res) |
Andi Kleen | 6ad696d | 2009-11-17 14:06:22 -0800 | [diff] [blame] | 633 | goto out; |
KAMEZAWA Hiroyuki | ebd1530 | 2006-08-05 12:15:06 -0700 | [diff] [blame] | 634 | |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 635 | if (!node_online(nid)) { |
| 636 | pgdat = hotadd_new_pgdat(nid, start); |
Andi Kleen | 6ad696d | 2009-11-17 14:06:22 -0800 | [diff] [blame] | 637 | ret = -ENOMEM; |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 638 | if (!pgdat) |
Andi Kleen | 6ad696d | 2009-11-17 14:06:22 -0800 | [diff] [blame] | 639 | goto out; |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 640 | new_pgdat = 1; |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 643 | /* call arch's memory hotadd */ |
| 644 | ret = arch_add_memory(nid, start, size); |
| 645 | |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 646 | if (ret < 0) |
| 647 | goto error; |
| 648 | |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 649 | /* we online node here. we can't roll back from here. */ |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 650 | node_set_online(nid); |
| 651 | |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 652 | if (new_pgdat) { |
| 653 | ret = register_one_node(nid); |
| 654 | /* |
| 655 | * If sysfs file of new node can't create, cpu on the node |
| 656 | * can't be hot-added. There is no rollback way now. |
| 657 | * So, check by BUG_ON() to catch it reluctantly.. |
| 658 | */ |
| 659 | BUG_ON(ret); |
| 660 | } |
| 661 | |
akpm@linux-foundation.org | d96ae53 | 2010-03-05 13:41:58 -0800 | [diff] [blame] | 662 | /* create new memmap entry */ |
| 663 | firmware_map_add_hotplug(start, start + size, "System RAM"); |
| 664 | |
Andi Kleen | 6ad696d | 2009-11-17 14:06:22 -0800 | [diff] [blame] | 665 | goto out; |
| 666 | |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 667 | error: |
| 668 | /* rollback pgdat allocation and others */ |
| 669 | if (new_pgdat) |
| 670 | rollback_node_hotadd(nid, pgdat); |
KAMEZAWA Hiroyuki | ebd1530 | 2006-08-05 12:15:06 -0700 | [diff] [blame] | 671 | if (res) |
| 672 | release_memory_resource(res); |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 673 | |
Andi Kleen | 6ad696d | 2009-11-17 14:06:22 -0800 | [diff] [blame] | 674 | out: |
KOSAKI Motohiro | 20d6c96 | 2010-12-02 14:31:19 -0800 | [diff] [blame] | 675 | unlock_memory_hotplug(); |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 676 | return ret; |
| 677 | } |
| 678 | EXPORT_SYMBOL_GPL(add_memory); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 679 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 680 | int __ref physical_remove_memory(u64 start, u64 size) |
| 681 | { |
| 682 | int ret; |
| 683 | struct resource *res, *res_old; |
| 684 | res = kzalloc(sizeof(struct resource), GFP_KERNEL); |
| 685 | BUG_ON(!res); |
| 686 | |
| 687 | ret = arch_physical_remove_memory(start, size); |
Larry Bassel | 2421717 | 2011-10-14 10:36:45 -0700 | [diff] [blame] | 688 | if (!ret) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 689 | kfree(res); |
Larry Bassel | 2421717 | 2011-10-14 10:36:45 -0700 | [diff] [blame] | 690 | return 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | res->name = "System RAM"; |
| 694 | res->start = start; |
| 695 | res->end = start + size - 1; |
| 696 | res->flags = IORESOURCE_MEM | IORESOURCE_BUSY; |
| 697 | |
| 698 | res_old = locate_resource(&iomem_resource, res); |
Larry Bassel | 2421717 | 2011-10-14 10:36:45 -0700 | [diff] [blame] | 699 | if (res_old) { |
| 700 | release_resource(res_old); |
| 701 | if (PageSlab(virt_to_head_page(res_old))) |
| 702 | kfree(res_old); |
| 703 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 704 | kfree(res); |
| 705 | |
| 706 | return ret; |
| 707 | } |
| 708 | EXPORT_SYMBOL_GPL(physical_remove_memory); |
| 709 | |
| 710 | int __ref physical_active_memory(u64 start, u64 size) |
| 711 | { |
| 712 | int ret; |
| 713 | |
| 714 | ret = arch_physical_active_memory(start, size); |
| 715 | return ret; |
| 716 | } |
| 717 | EXPORT_SYMBOL_GPL(physical_active_memory); |
| 718 | |
| 719 | int __ref physical_low_power_memory(u64 start, u64 size) |
| 720 | { |
| 721 | int ret; |
| 722 | |
| 723 | ret = arch_physical_low_power_memory(start, size); |
| 724 | return ret; |
| 725 | } |
| 726 | EXPORT_SYMBOL_GPL(physical_low_power_memory); |
| 727 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 728 | #ifdef CONFIG_MEMORY_HOTREMOVE |
| 729 | /* |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 730 | * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy |
| 731 | * set and the size of the free page is given by page_order(). Using this, |
| 732 | * the function determines if the pageblock contains only free pages. |
| 733 | * Due to buddy contraints, a free page at least the size of a pageblock will |
| 734 | * be located at the start of the pageblock |
| 735 | */ |
| 736 | static inline int pageblock_free(struct page *page) |
| 737 | { |
| 738 | return PageBuddy(page) && page_order(page) >= pageblock_order; |
| 739 | } |
| 740 | |
| 741 | /* Return the start of the next active pageblock after a given page */ |
| 742 | static struct page *next_active_pageblock(struct page *page) |
| 743 | { |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 744 | /* Ensure the starting page is pageblock-aligned */ |
| 745 | BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1)); |
| 746 | |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 747 | /* 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] | 748 | if (pageblock_free(page)) { |
| 749 | int order; |
| 750 | /* be careful. we don't have locks, page_order can be changed.*/ |
| 751 | order = page_order(page); |
| 752 | if ((order < MAX_ORDER) && (order >= pageblock_order)) |
| 753 | return page + (1 << order); |
| 754 | } |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 755 | |
KAMEZAWA Hiroyuki | 0dcc48c | 2010-09-09 16:38:01 -0700 | [diff] [blame] | 756 | return page + pageblock_nr_pages; |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | /* Checks if this range of memory is likely to be hot-removable. */ |
| 760 | int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages) |
| 761 | { |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 762 | struct page *page = pfn_to_page(start_pfn); |
| 763 | struct page *end_page = page + nr_pages; |
| 764 | |
| 765 | /* Check the starting page of each pageblock within the range */ |
| 766 | for (; page < end_page; page = next_active_pageblock(page)) { |
KAMEZAWA Hiroyuki | 49ac825 | 2010-10-26 14:21:30 -0700 | [diff] [blame] | 767 | if (!is_pageblock_removable_nolock(page)) |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 768 | return 0; |
KAMEZAWA Hiroyuki | 49ac825 | 2010-10-26 14:21:30 -0700 | [diff] [blame] | 769 | cond_resched(); |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | /* All pageblocks in the memory block are likely to be hot-removable */ |
| 773 | return 1; |
| 774 | } |
| 775 | |
| 776 | /* |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 777 | * Confirm all pages in a range [start, end) is belongs to the same zone. |
| 778 | */ |
| 779 | static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn) |
| 780 | { |
| 781 | unsigned long pfn; |
| 782 | struct zone *zone = NULL; |
| 783 | struct page *page; |
| 784 | int i; |
| 785 | for (pfn = start_pfn; |
| 786 | pfn < end_pfn; |
| 787 | pfn += MAX_ORDER_NR_PAGES) { |
| 788 | i = 0; |
| 789 | /* This is just a CONFIG_HOLES_IN_ZONE check.*/ |
| 790 | while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i)) |
| 791 | i++; |
| 792 | if (i == MAX_ORDER_NR_PAGES) |
| 793 | continue; |
| 794 | page = pfn_to_page(pfn + i); |
| 795 | if (zone && page_zone(page) != zone) |
| 796 | return 0; |
| 797 | zone = page_zone(page); |
| 798 | } |
| 799 | return 1; |
| 800 | } |
| 801 | |
| 802 | /* |
| 803 | * Scanning pfn is much easier than scanning lru list. |
| 804 | * Scan pfn from start to end and Find LRU page. |
| 805 | */ |
Andrew Morton | 7bbc090 | 2010-10-26 14:22:05 -0700 | [diff] [blame] | 806 | static unsigned long scan_lru_pages(unsigned long start, unsigned long end) |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 807 | { |
| 808 | unsigned long pfn; |
| 809 | struct page *page; |
| 810 | for (pfn = start; pfn < end; pfn++) { |
| 811 | if (pfn_valid(pfn)) { |
| 812 | page = pfn_to_page(pfn); |
| 813 | if (PageLRU(page)) |
| 814 | return pfn; |
| 815 | } |
| 816 | } |
| 817 | return 0; |
| 818 | } |
| 819 | |
| 820 | static struct page * |
Hugh Dickins | 3c1d437 | 2009-01-06 14:39:23 -0800 | [diff] [blame] | 821 | hotremove_migrate_alloc(struct page *page, unsigned long private, int **x) |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 822 | { |
Hugh Dickins | 3c1d437 | 2009-01-06 14:39:23 -0800 | [diff] [blame] | 823 | /* This should be improooooved!! */ |
Jack Cheung | 1e6ec3c | 2011-11-21 16:44:05 -0800 | [diff] [blame] | 824 | return alloc_page(GFP_HIGHUSER_MOVABLE | __GFP_NORETRY | __GFP_NOWARN | |
| 825 | __GFP_NOMEMALLOC); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 826 | } |
| 827 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 828 | #define NR_OFFLINE_AT_ONCE_PAGES (256) |
| 829 | static int |
| 830 | do_migrate_range(unsigned long start_pfn, unsigned long end_pfn) |
| 831 | { |
| 832 | unsigned long pfn; |
| 833 | struct page *page; |
| 834 | int move_pages = NR_OFFLINE_AT_ONCE_PAGES; |
| 835 | int not_managed = 0; |
| 836 | int ret = 0; |
| 837 | LIST_HEAD(source); |
| 838 | |
| 839 | for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) { |
| 840 | if (!pfn_valid(pfn)) |
| 841 | continue; |
| 842 | page = pfn_to_page(pfn); |
Konstantin Khlebnikov | 700c2a4 | 2011-05-24 17:12:19 -0700 | [diff] [blame] | 843 | if (!get_page_unless_zero(page)) |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 844 | continue; |
| 845 | /* |
| 846 | * We can skip free pages. And we can only deal with pages on |
| 847 | * LRU. |
| 848 | */ |
Nick Piggin | 62695a8 | 2008-10-18 20:26:09 -0700 | [diff] [blame] | 849 | ret = isolate_lru_page(page); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 850 | if (!ret) { /* Success */ |
Konstantin Khlebnikov | 700c2a4 | 2011-05-24 17:12:19 -0700 | [diff] [blame] | 851 | put_page(page); |
Nick Piggin | 62695a8 | 2008-10-18 20:26:09 -0700 | [diff] [blame] | 852 | list_add_tail(&page->lru, &source); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 853 | move_pages--; |
KOSAKI Motohiro | 6d9c285 | 2009-12-14 17:58:11 -0800 | [diff] [blame] | 854 | inc_zone_page_state(page, NR_ISOLATED_ANON + |
| 855 | page_is_file_cache(page)); |
| 856 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 857 | } else { |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 858 | #ifdef CONFIG_DEBUG_VM |
Wu Fengguang | 718a382 | 2010-03-10 15:20:43 -0800 | [diff] [blame] | 859 | printk(KERN_ALERT "removing pfn %lx from LRU failed\n", |
| 860 | pfn); |
| 861 | dump_page(page); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 862 | #endif |
Konstantin Khlebnikov | 700c2a4 | 2011-05-24 17:12:19 -0700 | [diff] [blame] | 863 | put_page(page); |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 864 | /* Because we don't have big zone->lock. we should |
Bob Liu | 809c444 | 2010-10-26 14:22:10 -0700 | [diff] [blame] | 865 | check this again here. */ |
| 866 | if (page_count(page)) { |
| 867 | not_managed++; |
Bob Liu | f3ab263 | 2010-10-26 14:22:10 -0700 | [diff] [blame] | 868 | ret = -EBUSY; |
Bob Liu | 809c444 | 2010-10-26 14:22:10 -0700 | [diff] [blame] | 869 | break; |
| 870 | } |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 871 | } |
| 872 | } |
Bob Liu | f3ab263 | 2010-10-26 14:22:10 -0700 | [diff] [blame] | 873 | if (!list_empty(&source)) { |
| 874 | if (not_managed) { |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 875 | putback_lru_pages(&source); |
Bob Liu | f3ab263 | 2010-10-26 14:22:10 -0700 | [diff] [blame] | 876 | goto out; |
| 877 | } |
| 878 | /* this function returns # of failed pages */ |
Mel Gorman | 77f1fe6 | 2011-01-13 15:45:57 -0800 | [diff] [blame] | 879 | ret = migrate_pages(&source, hotremove_migrate_alloc, 0, |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 880 | true, MIGRATE_SYNC); |
Bob Liu | f3ab263 | 2010-10-26 14:22:10 -0700 | [diff] [blame] | 881 | if (ret) |
| 882 | putback_lru_pages(&source); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 883 | } |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 884 | out: |
| 885 | return ret; |
| 886 | } |
| 887 | |
| 888 | /* |
| 889 | * remove from free_area[] and mark all as Reserved. |
| 890 | */ |
| 891 | static int |
| 892 | offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages, |
| 893 | void *data) |
| 894 | { |
| 895 | __offline_isolated_pages(start, start + nr_pages); |
| 896 | return 0; |
| 897 | } |
| 898 | |
| 899 | static void |
| 900 | offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn) |
| 901 | { |
KAMEZAWA Hiroyuki | 908eedc | 2009-09-22 16:45:46 -0700 | [diff] [blame] | 902 | walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL, |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 903 | offline_isolated_pages_cb); |
| 904 | } |
| 905 | |
| 906 | /* |
| 907 | * Check all pages in range, recoreded as memory resource, are isolated. |
| 908 | */ |
| 909 | static int |
| 910 | check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages, |
| 911 | void *data) |
| 912 | { |
| 913 | int ret; |
| 914 | long offlined = *(long *)data; |
| 915 | ret = test_pages_isolated(start_pfn, start_pfn + nr_pages); |
| 916 | offlined = nr_pages; |
| 917 | if (!ret) |
| 918 | *(long *)data += offlined; |
| 919 | return ret; |
| 920 | } |
| 921 | |
| 922 | static long |
| 923 | check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn) |
| 924 | { |
| 925 | long offlined = 0; |
| 926 | int ret; |
| 927 | |
KAMEZAWA Hiroyuki | 908eedc | 2009-09-22 16:45:46 -0700 | [diff] [blame] | 928 | ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined, |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 929 | check_pages_isolated_cb); |
| 930 | if (ret < 0) |
| 931 | offlined = (long)ret; |
| 932 | return offlined; |
| 933 | } |
| 934 | |
KOSAKI Motohiro | 839a4fc | 2011-05-24 17:11:31 -0700 | [diff] [blame] | 935 | static int __ref offline_pages(unsigned long start_pfn, |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 936 | unsigned long end_pfn, unsigned long timeout) |
| 937 | { |
| 938 | unsigned long pfn, nr_pages, expire; |
| 939 | long offlined_pages; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 940 | int ret, drain, retry_max, node; |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 941 | struct zone *zone; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 942 | struct memory_notify arg; |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 943 | |
| 944 | BUG_ON(start_pfn >= end_pfn); |
| 945 | /* at least, alignment against pageblock is necessary */ |
| 946 | if (!IS_ALIGNED(start_pfn, pageblock_nr_pages)) |
| 947 | return -EINVAL; |
| 948 | if (!IS_ALIGNED(end_pfn, pageblock_nr_pages)) |
| 949 | return -EINVAL; |
| 950 | /* This makes hotplug much easier...and readable. |
| 951 | we assume this for now. .*/ |
| 952 | if (!test_pages_in_a_zone(start_pfn, end_pfn)) |
| 953 | return -EINVAL; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 954 | |
KOSAKI Motohiro | 20d6c96 | 2010-12-02 14:31:19 -0800 | [diff] [blame] | 955 | lock_memory_hotplug(); |
Andi Kleen | 6ad696d | 2009-11-17 14:06:22 -0800 | [diff] [blame] | 956 | |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 957 | zone = page_zone(pfn_to_page(start_pfn)); |
| 958 | node = zone_to_nid(zone); |
| 959 | nr_pages = end_pfn - start_pfn; |
| 960 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 961 | /* set above range as isolated */ |
| 962 | ret = start_isolate_page_range(start_pfn, end_pfn); |
| 963 | if (ret) |
Andi Kleen | 6ad696d | 2009-11-17 14:06:22 -0800 | [diff] [blame] | 964 | goto out; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 965 | |
| 966 | arg.start_pfn = start_pfn; |
| 967 | arg.nr_pages = nr_pages; |
| 968 | arg.status_change_nid = -1; |
| 969 | if (nr_pages >= node_present_pages(node)) |
| 970 | arg.status_change_nid = node; |
| 971 | |
| 972 | ret = memory_notify(MEM_GOING_OFFLINE, &arg); |
| 973 | ret = notifier_to_errno(ret); |
| 974 | if (ret) |
| 975 | goto failed_removal; |
| 976 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 977 | pfn = start_pfn; |
| 978 | expire = jiffies + timeout; |
| 979 | drain = 0; |
| 980 | retry_max = 5; |
| 981 | repeat: |
| 982 | /* start memory hot removal */ |
| 983 | ret = -EAGAIN; |
| 984 | if (time_after(jiffies, expire)) |
| 985 | goto failed_removal; |
| 986 | ret = -EINTR; |
| 987 | if (signal_pending(current)) |
| 988 | goto failed_removal; |
| 989 | ret = 0; |
| 990 | if (drain) { |
| 991 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 992 | cond_resched(); |
Christoph Lameter | 9f8f217 | 2008-02-04 22:29:11 -0800 | [diff] [blame] | 993 | drain_all_pages(); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | pfn = scan_lru_pages(start_pfn, end_pfn); |
| 997 | if (pfn) { /* We have page on LRU */ |
| 998 | ret = do_migrate_range(pfn, end_pfn); |
| 999 | if (!ret) { |
| 1000 | drain = 1; |
| 1001 | goto repeat; |
| 1002 | } else { |
| 1003 | if (ret < 0) |
| 1004 | if (--retry_max == 0) |
| 1005 | goto failed_removal; |
| 1006 | yield(); |
| 1007 | drain = 1; |
| 1008 | goto repeat; |
| 1009 | } |
| 1010 | } |
| 1011 | /* drain all zone's lru pagevec, this is asyncronous... */ |
| 1012 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1013 | yield(); |
| 1014 | /* drain pcp pages , this is synchrouns. */ |
Christoph Lameter | 9f8f217 | 2008-02-04 22:29:11 -0800 | [diff] [blame] | 1015 | drain_all_pages(); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1016 | /* check again */ |
| 1017 | offlined_pages = check_pages_isolated(start_pfn, end_pfn); |
| 1018 | if (offlined_pages < 0) { |
| 1019 | ret = -EBUSY; |
| 1020 | goto failed_removal; |
| 1021 | } |
| 1022 | printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages); |
| 1023 | /* Ok, all of our target is islaoted. |
| 1024 | We cannot do rollback at this point. */ |
| 1025 | offline_isolated_pages(start_pfn, end_pfn); |
KAMEZAWA Hiroyuki | dbc0e4c | 2007-11-14 16:59:12 -0800 | [diff] [blame] | 1026 | /* reset pagetype flags and makes migrate type to be MOVABLE */ |
| 1027 | undo_isolate_page_range(start_pfn, end_pfn); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1028 | /* removal success */ |
Jack Cheung | 80c201e | 2011-11-29 16:53:34 -0800 | [diff] [blame] | 1029 | if (offlined_pages > zone->present_pages) |
| 1030 | zone->present_pages = 0; |
| 1031 | else |
| 1032 | zone->present_pages -= offlined_pages; |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1033 | zone->zone_pgdat->node_present_pages -= offlined_pages; |
| 1034 | totalram_pages -= offlined_pages; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1035 | |
Jack Cheung | 59f9f1c | 2011-11-29 16:52:49 -0800 | [diff] [blame] | 1036 | #ifdef CONFIG_FIX_MOVABLE_ZONE |
| 1037 | if (zone_idx(zone) != ZONE_MOVABLE) |
| 1038 | total_unmovable_pages -= offlined_pages; |
| 1039 | #endif |
KOSAKI Motohiro | 1b79acc | 2011-05-24 17:11:32 -0700 | [diff] [blame] | 1040 | init_per_zone_wmark_min(); |
| 1041 | |
David Rientjes | 8fe23e0 | 2009-12-14 17:58:33 -0800 | [diff] [blame] | 1042 | if (!node_present_pages(node)) { |
| 1043 | node_clear_state(node, N_HIGH_MEMORY); |
| 1044 | kswapd_stop(node); |
| 1045 | } |
Minchan Kim | bce7394 | 2009-06-16 15:32:50 -0700 | [diff] [blame] | 1046 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1047 | vm_total_pages = nr_free_pagecache_pages(); |
| 1048 | writeback_set_ratelimit(); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1049 | |
| 1050 | memory_notify(MEM_OFFLINE, &arg); |
KOSAKI Motohiro | 20d6c96 | 2010-12-02 14:31:19 -0800 | [diff] [blame] | 1051 | unlock_memory_hotplug(); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1052 | return 0; |
| 1053 | |
| 1054 | failed_removal: |
| 1055 | printk(KERN_INFO "memory offlining %lx to %lx failed\n", |
| 1056 | start_pfn, end_pfn); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1057 | memory_notify(MEM_CANCEL_OFFLINE, &arg); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1058 | /* pushback to free area */ |
| 1059 | undo_isolate_page_range(start_pfn, end_pfn); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1060 | |
Andi Kleen | 6ad696d | 2009-11-17 14:06:22 -0800 | [diff] [blame] | 1061 | out: |
KOSAKI Motohiro | 20d6c96 | 2010-12-02 14:31:19 -0800 | [diff] [blame] | 1062 | unlock_memory_hotplug(); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1063 | return ret; |
| 1064 | } |
Badari Pulavarty | 7108878 | 2008-10-18 20:25:58 -0700 | [diff] [blame] | 1065 | |
| 1066 | int remove_memory(u64 start, u64 size) |
| 1067 | { |
| 1068 | unsigned long start_pfn, end_pfn; |
| 1069 | |
| 1070 | start_pfn = PFN_DOWN(start); |
| 1071 | end_pfn = start_pfn + PFN_DOWN(size); |
| 1072 | return offline_pages(start_pfn, end_pfn, 120 * HZ); |
| 1073 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1074 | |
KAMEZAWA Hiroyuki | 48e9419 | 2007-10-16 01:26:14 -0700 | [diff] [blame] | 1075 | #else |
| 1076 | int remove_memory(u64 start, u64 size) |
| 1077 | { |
| 1078 | return -EINVAL; |
| 1079 | } |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1080 | #endif /* CONFIG_MEMORY_HOTREMOVE */ |
Badari Pulavarty | 7108878 | 2008-10-18 20:25:58 -0700 | [diff] [blame] | 1081 | EXPORT_SYMBOL_GPL(remove_memory); |