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> |
| 14 | #include <linux/module.h> |
| 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> |
Paul Jackson | 38837fc | 2006-09-29 02:01:16 -0700 | [diff] [blame] | 25 | #include <linux/cpuset.h> |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 26 | #include <linux/delay.h> |
| 27 | #include <linux/migrate.h> |
| 28 | #include <linux/page-isolation.h> |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 29 | |
| 30 | #include <asm/tlbflush.h> |
| 31 | |
Keith Mannthey | 45e0b78 | 2006-09-30 23:27:09 -0700 | [diff] [blame] | 32 | /* add this memory to iomem resource */ |
| 33 | static struct resource *register_memory_resource(u64 start, u64 size) |
| 34 | { |
| 35 | struct resource *res; |
| 36 | res = kzalloc(sizeof(struct resource), GFP_KERNEL); |
| 37 | BUG_ON(!res); |
| 38 | |
| 39 | res->name = "System RAM"; |
| 40 | res->start = start; |
| 41 | res->end = start + size - 1; |
Yasunori Goto | 887c3cb | 2007-11-14 16:59:20 -0800 | [diff] [blame] | 42 | res->flags = IORESOURCE_MEM | IORESOURCE_BUSY; |
Keith Mannthey | 45e0b78 | 2006-09-30 23:27:09 -0700 | [diff] [blame] | 43 | if (request_resource(&iomem_resource, res) < 0) { |
| 44 | printk("System RAM resource %llx - %llx cannot be added\n", |
| 45 | (unsigned long long)res->start, (unsigned long long)res->end); |
| 46 | kfree(res); |
| 47 | res = NULL; |
| 48 | } |
| 49 | return res; |
| 50 | } |
| 51 | |
| 52 | static void release_memory_resource(struct resource *res) |
| 53 | { |
| 54 | if (!res) |
| 55 | return; |
| 56 | release_resource(res); |
| 57 | kfree(res); |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | |
Keith Mannthey | 5394702 | 2006-09-30 23:27:08 -0700 | [diff] [blame] | 62 | #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE |
Yasunori Goto | 718127c | 2006-06-23 02:03:10 -0700 | [diff] [blame] | 63 | static int __add_zone(struct zone *zone, unsigned long phys_start_pfn) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 64 | { |
| 65 | struct pglist_data *pgdat = zone->zone_pgdat; |
| 66 | int nr_pages = PAGES_PER_SECTION; |
| 67 | int nid = pgdat->node_id; |
| 68 | int zone_type; |
| 69 | |
| 70 | zone_type = zone - pgdat->node_zones; |
Yasunori Goto | 13466c8 | 2007-06-01 00:46:53 -0700 | [diff] [blame] | 71 | if (!zone->wait_table) { |
Yasunori Goto | 718127c | 2006-06-23 02:03:10 -0700 | [diff] [blame] | 72 | int ret = 0; |
Dave Hansen | a2f3aa02 | 2007-01-10 23:15:30 -0800 | [diff] [blame] | 73 | ret = init_currently_empty_zone(zone, phys_start_pfn, |
| 74 | nr_pages, MEMMAP_HOTPLUG); |
Yasunori Goto | 718127c | 2006-06-23 02:03:10 -0700 | [diff] [blame] | 75 | if (ret < 0) |
| 76 | return ret; |
| 77 | } |
Dave Hansen | a2f3aa02 | 2007-01-10 23:15:30 -0800 | [diff] [blame] | 78 | memmap_init_zone(nr_pages, nid, zone_type, |
| 79 | phys_start_pfn, MEMMAP_HOTPLUG); |
Yasunori Goto | 718127c | 2006-06-23 02:03:10 -0700 | [diff] [blame] | 80 | return 0; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 83 | static int __add_section(struct zone *zone, unsigned long phys_start_pfn) |
| 84 | { |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 85 | int nr_pages = PAGES_PER_SECTION; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 86 | int ret; |
| 87 | |
KAMEZAWA Hiroyuki | ebd1530 | 2006-08-05 12:15:06 -0700 | [diff] [blame] | 88 | if (pfn_valid(phys_start_pfn)) |
| 89 | return -EEXIST; |
| 90 | |
Dave Hansen | 0b0acbec | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 91 | ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 92 | |
| 93 | if (ret < 0) |
| 94 | return ret; |
| 95 | |
Yasunori Goto | 718127c | 2006-06-23 02:03:10 -0700 | [diff] [blame] | 96 | ret = __add_zone(zone, phys_start_pfn); |
| 97 | |
| 98 | if (ret < 0) |
| 99 | return ret; |
| 100 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 101 | return register_new_memory(__pfn_to_section(phys_start_pfn)); |
| 102 | } |
| 103 | |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 104 | static int __remove_section(struct zone *zone, struct mem_section *ms) |
| 105 | { |
| 106 | unsigned long flags; |
| 107 | struct pglist_data *pgdat = zone->zone_pgdat; |
| 108 | int ret = -EINVAL; |
| 109 | |
| 110 | if (!valid_section(ms)) |
| 111 | return ret; |
| 112 | |
| 113 | ret = unregister_memory_section(ms); |
| 114 | if (ret) |
| 115 | return ret; |
| 116 | |
| 117 | pgdat_resize_lock(pgdat, &flags); |
| 118 | sparse_remove_one_section(zone, ms); |
| 119 | pgdat_resize_unlock(pgdat, &flags); |
| 120 | return 0; |
| 121 | } |
| 122 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 123 | /* |
| 124 | * Reasonably generic function for adding memory. It is |
| 125 | * expected that archs that support memory hotplug will |
| 126 | * call this function after deciding the zone to which to |
| 127 | * add the new pages. |
| 128 | */ |
| 129 | int __add_pages(struct zone *zone, unsigned long phys_start_pfn, |
| 130 | unsigned long nr_pages) |
| 131 | { |
| 132 | unsigned long i; |
| 133 | int err = 0; |
KAMEZAWA Hiroyuki | 6f71271 | 2006-08-05 12:14:58 -0700 | [diff] [blame] | 134 | int start_sec, end_sec; |
| 135 | /* during initialize mem_map, align hot-added range to section */ |
| 136 | start_sec = pfn_to_section_nr(phys_start_pfn); |
| 137 | end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 138 | |
KAMEZAWA Hiroyuki | 6f71271 | 2006-08-05 12:14:58 -0700 | [diff] [blame] | 139 | for (i = start_sec; i <= end_sec; i++) { |
| 140 | err = __add_section(zone, i << PFN_SECTION_SHIFT); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 141 | |
KAMEZAWA Hiroyuki | 6f71271 | 2006-08-05 12:14:58 -0700 | [diff] [blame] | 142 | /* |
Simon Arlott | 183ff22 | 2007-10-20 01:27:18 +0200 | [diff] [blame] | 143 | * EEXIST is finally dealt with by ioresource collision |
KAMEZAWA Hiroyuki | 6f71271 | 2006-08-05 12:14:58 -0700 | [diff] [blame] | 144 | * check. see add_memory() => register_memory_resource() |
| 145 | * Warning will be printed if there is collision. |
Joel H Schopp | bed120c | 2006-05-01 12:16:11 -0700 | [diff] [blame] | 146 | */ |
| 147 | if (err && (err != -EEXIST)) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 148 | break; |
KAMEZAWA Hiroyuki | 6f71271 | 2006-08-05 12:14:58 -0700 | [diff] [blame] | 149 | err = 0; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | return err; |
| 153 | } |
Joel H Schopp | bed120c | 2006-05-01 12:16:11 -0700 | [diff] [blame] | 154 | EXPORT_SYMBOL_GPL(__add_pages); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 155 | |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 156 | /** |
| 157 | * __remove_pages() - remove sections of pages from a zone |
| 158 | * @zone: zone from which pages need to be removed |
| 159 | * @phys_start_pfn: starting pageframe (must be aligned to start of a section) |
| 160 | * @nr_pages: number of pages to remove (must be multiple of section size) |
| 161 | * |
| 162 | * Generic helper function to remove section mappings and sysfs entries |
| 163 | * for the section of the memory we are removing. Caller needs to make |
| 164 | * sure that pages are marked reserved and zones are adjust properly by |
| 165 | * calling offline_pages(). |
| 166 | */ |
| 167 | int __remove_pages(struct zone *zone, unsigned long phys_start_pfn, |
| 168 | unsigned long nr_pages) |
| 169 | { |
| 170 | unsigned long i, ret = 0; |
| 171 | int sections_to_remove; |
| 172 | |
| 173 | /* |
| 174 | * We can only remove entire sections |
| 175 | */ |
| 176 | BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK); |
| 177 | BUG_ON(nr_pages % PAGES_PER_SECTION); |
| 178 | |
| 179 | release_mem_region(phys_start_pfn << PAGE_SHIFT, nr_pages * PAGE_SIZE); |
| 180 | |
| 181 | sections_to_remove = nr_pages / PAGES_PER_SECTION; |
| 182 | for (i = 0; i < sections_to_remove; i++) { |
| 183 | unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION; |
| 184 | ret = __remove_section(zone, __pfn_to_section(pfn)); |
| 185 | if (ret) |
| 186 | break; |
| 187 | } |
| 188 | return ret; |
| 189 | } |
| 190 | EXPORT_SYMBOL_GPL(__remove_pages); |
| 191 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 192 | static void grow_zone_span(struct zone *zone, |
| 193 | unsigned long start_pfn, unsigned long end_pfn) |
| 194 | { |
| 195 | unsigned long old_zone_end_pfn; |
| 196 | |
| 197 | zone_span_writelock(zone); |
| 198 | |
| 199 | old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages; |
| 200 | if (start_pfn < zone->zone_start_pfn) |
| 201 | zone->zone_start_pfn = start_pfn; |
| 202 | |
Yasunori Goto | 25a6df9 | 2006-05-30 21:25:42 -0700 | [diff] [blame] | 203 | zone->spanned_pages = max(old_zone_end_pfn, end_pfn) - |
| 204 | zone->zone_start_pfn; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 205 | |
| 206 | zone_span_writeunlock(zone); |
| 207 | } |
| 208 | |
| 209 | static void grow_pgdat_span(struct pglist_data *pgdat, |
| 210 | unsigned long start_pfn, unsigned long end_pfn) |
| 211 | { |
| 212 | unsigned long old_pgdat_end_pfn = |
| 213 | pgdat->node_start_pfn + pgdat->node_spanned_pages; |
| 214 | |
| 215 | if (start_pfn < pgdat->node_start_pfn) |
| 216 | pgdat->node_start_pfn = start_pfn; |
| 217 | |
Yasunori Goto | 25a6df9 | 2006-05-30 21:25:42 -0700 | [diff] [blame] | 218 | pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) - |
| 219 | pgdat->node_start_pfn; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Jeremy Fitzhardinge | 180c06e | 2008-04-28 02:12:03 -0700 | [diff] [blame^] | 222 | void online_page(struct page *page) |
| 223 | { |
| 224 | totalram_pages++; |
| 225 | num_physpages++; |
| 226 | |
| 227 | #ifdef CONFIG_HIGHMEM |
| 228 | if (PageHighMem(page)) |
| 229 | totalhigh_pages++; |
| 230 | #endif |
| 231 | |
| 232 | #ifdef CONFIG_FLATMEM |
| 233 | max_mapnr = max(page_to_pfn(page), max_mapnr); |
| 234 | #endif |
| 235 | |
| 236 | ClearPageReserved(page); |
| 237 | init_page_count(page); |
| 238 | __free_page(page); |
| 239 | } |
| 240 | |
KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 241 | static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages, |
| 242 | void *arg) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 243 | { |
| 244 | unsigned long i; |
KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 245 | unsigned long onlined_pages = *(unsigned long *)arg; |
| 246 | struct page *page; |
| 247 | if (PageReserved(pfn_to_page(start_pfn))) |
| 248 | for (i = 0; i < nr_pages; i++) { |
| 249 | page = pfn_to_page(start_pfn + i); |
| 250 | online_page(page); |
| 251 | onlined_pages++; |
| 252 | } |
| 253 | *(unsigned long *)arg = onlined_pages; |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | |
| 258 | int online_pages(unsigned long pfn, unsigned long nr_pages) |
| 259 | { |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 260 | unsigned long flags; |
| 261 | unsigned long onlined_pages = 0; |
| 262 | struct zone *zone; |
Yasunori Goto | 6811378 | 2006-06-23 02:03:11 -0700 | [diff] [blame] | 263 | int need_zonelists_rebuild = 0; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 264 | int nid; |
| 265 | int ret; |
| 266 | struct memory_notify arg; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 267 | |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 268 | arg.start_pfn = pfn; |
| 269 | arg.nr_pages = nr_pages; |
| 270 | arg.status_change_nid = -1; |
| 271 | |
| 272 | nid = page_to_nid(pfn_to_page(pfn)); |
| 273 | if (node_present_pages(nid) == 0) |
| 274 | arg.status_change_nid = nid; |
| 275 | |
| 276 | ret = memory_notify(MEM_GOING_ONLINE, &arg); |
| 277 | ret = notifier_to_errno(ret); |
| 278 | if (ret) { |
| 279 | memory_notify(MEM_CANCEL_ONLINE, &arg); |
| 280 | return ret; |
| 281 | } |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 282 | /* |
| 283 | * This doesn't need a lock to do pfn_to_page(). |
| 284 | * The section can't be removed here because of the |
Daniel Walker | da19cbc | 2008-02-04 23:35:47 -0800 | [diff] [blame] | 285 | * memory_block->state_mutex. |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 286 | */ |
| 287 | zone = page_zone(pfn_to_page(pfn)); |
| 288 | pgdat_resize_lock(zone->zone_pgdat, &flags); |
| 289 | grow_zone_span(zone, pfn, pfn + nr_pages); |
| 290 | grow_pgdat_span(zone->zone_pgdat, pfn, pfn + nr_pages); |
| 291 | pgdat_resize_unlock(zone->zone_pgdat, &flags); |
| 292 | |
Yasunori Goto | 6811378 | 2006-06-23 02:03:11 -0700 | [diff] [blame] | 293 | /* |
| 294 | * If this zone is not populated, then it is not in zonelist. |
| 295 | * This means the page allocator ignores this zone. |
| 296 | * So, zonelist must be updated after online. |
| 297 | */ |
| 298 | if (!populated_zone(zone)) |
| 299 | need_zonelists_rebuild = 1; |
| 300 | |
KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 301 | walk_memory_resource(pfn, nr_pages, &onlined_pages, |
| 302 | online_pages_range); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 303 | zone->present_pages += onlined_pages; |
Yasunori Goto | f2937be | 2006-03-09 17:33:51 -0800 | [diff] [blame] | 304 | zone->zone_pgdat->node_present_pages += onlined_pages; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 305 | |
Dave Hansen | 61b1399 | 2005-10-29 18:16:56 -0700 | [diff] [blame] | 306 | setup_per_zone_pages_min(); |
Christoph Lameter | 7ea1530 | 2007-10-16 01:25:29 -0700 | [diff] [blame] | 307 | if (onlined_pages) { |
| 308 | kswapd_run(zone_to_nid(zone)); |
| 309 | node_set_state(zone_to_nid(zone), N_HIGH_MEMORY); |
| 310 | } |
Dave Hansen | 61b1399 | 2005-10-29 18:16:56 -0700 | [diff] [blame] | 311 | |
Yasunori Goto | 6811378 | 2006-06-23 02:03:11 -0700 | [diff] [blame] | 312 | if (need_zonelists_rebuild) |
| 313 | build_all_zonelists(); |
KAMEZAWA Hiroyuki | 5a4d436 | 2006-06-23 02:03:47 -0700 | [diff] [blame] | 314 | vm_total_pages = nr_free_pagecache_pages(); |
Chandra Seetharaman | 2d1d43f | 2006-09-29 02:01:25 -0700 | [diff] [blame] | 315 | writeback_set_ratelimit(); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 316 | |
| 317 | if (onlined_pages) |
| 318 | memory_notify(MEM_ONLINE, &arg); |
| 319 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 320 | return 0; |
| 321 | } |
Keith Mannthey | 5394702 | 2006-09-30 23:27:08 -0700 | [diff] [blame] | 322 | #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */ |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 323 | |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 324 | static pg_data_t *hotadd_new_pgdat(int nid, u64 start) |
| 325 | { |
| 326 | struct pglist_data *pgdat; |
| 327 | unsigned long zones_size[MAX_NR_ZONES] = {0}; |
| 328 | unsigned long zholes_size[MAX_NR_ZONES] = {0}; |
| 329 | unsigned long start_pfn = start >> PAGE_SHIFT; |
| 330 | |
| 331 | pgdat = arch_alloc_nodedata(nid); |
| 332 | if (!pgdat) |
| 333 | return NULL; |
| 334 | |
| 335 | arch_refresh_nodedata(nid, pgdat); |
| 336 | |
| 337 | /* we can use NODE_DATA(nid) from here */ |
| 338 | |
| 339 | /* init node's zones as empty zones, we don't have any present pages.*/ |
| 340 | free_area_init_node(nid, pgdat, zones_size, start_pfn, zholes_size); |
| 341 | |
| 342 | return pgdat; |
| 343 | } |
| 344 | |
| 345 | static void rollback_node_hotadd(int nid, pg_data_t *pgdat) |
| 346 | { |
| 347 | arch_refresh_nodedata(nid, NULL); |
| 348 | arch_free_nodedata(pgdat); |
| 349 | return; |
| 350 | } |
| 351 | |
KAMEZAWA Hiroyuki | 0a54703 | 2006-06-27 02:53:35 -0700 | [diff] [blame] | 352 | |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 353 | int add_memory(int nid, u64 start, u64 size) |
| 354 | { |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 355 | pg_data_t *pgdat = NULL; |
| 356 | int new_pgdat = 0; |
KAMEZAWA Hiroyuki | ebd1530 | 2006-08-05 12:15:06 -0700 | [diff] [blame] | 357 | struct resource *res; |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 358 | int ret; |
| 359 | |
KAMEZAWA Hiroyuki | ebd1530 | 2006-08-05 12:15:06 -0700 | [diff] [blame] | 360 | res = register_memory_resource(start, size); |
| 361 | if (!res) |
| 362 | return -EEXIST; |
| 363 | |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 364 | if (!node_online(nid)) { |
| 365 | pgdat = hotadd_new_pgdat(nid, start); |
| 366 | if (!pgdat) |
| 367 | return -ENOMEM; |
| 368 | new_pgdat = 1; |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 371 | /* call arch's memory hotadd */ |
| 372 | ret = arch_add_memory(nid, start, size); |
| 373 | |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 374 | if (ret < 0) |
| 375 | goto error; |
| 376 | |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 377 | /* we online node here. we can't roll back from here. */ |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 378 | node_set_online(nid); |
| 379 | |
Paul Jackson | 38837fc | 2006-09-29 02:01:16 -0700 | [diff] [blame] | 380 | cpuset_track_online_nodes(); |
| 381 | |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 382 | if (new_pgdat) { |
| 383 | ret = register_one_node(nid); |
| 384 | /* |
| 385 | * If sysfs file of new node can't create, cpu on the node |
| 386 | * can't be hot-added. There is no rollback way now. |
| 387 | * So, check by BUG_ON() to catch it reluctantly.. |
| 388 | */ |
| 389 | BUG_ON(ret); |
| 390 | } |
| 391 | |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 392 | return ret; |
| 393 | error: |
| 394 | /* rollback pgdat allocation and others */ |
| 395 | if (new_pgdat) |
| 396 | rollback_node_hotadd(nid, pgdat); |
KAMEZAWA Hiroyuki | ebd1530 | 2006-08-05 12:15:06 -0700 | [diff] [blame] | 397 | if (res) |
| 398 | release_memory_resource(res); |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 399 | |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 400 | return ret; |
| 401 | } |
| 402 | EXPORT_SYMBOL_GPL(add_memory); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 403 | |
| 404 | #ifdef CONFIG_MEMORY_HOTREMOVE |
| 405 | /* |
| 406 | * Confirm all pages in a range [start, end) is belongs to the same zone. |
| 407 | */ |
| 408 | static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn) |
| 409 | { |
| 410 | unsigned long pfn; |
| 411 | struct zone *zone = NULL; |
| 412 | struct page *page; |
| 413 | int i; |
| 414 | for (pfn = start_pfn; |
| 415 | pfn < end_pfn; |
| 416 | pfn += MAX_ORDER_NR_PAGES) { |
| 417 | i = 0; |
| 418 | /* This is just a CONFIG_HOLES_IN_ZONE check.*/ |
| 419 | while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i)) |
| 420 | i++; |
| 421 | if (i == MAX_ORDER_NR_PAGES) |
| 422 | continue; |
| 423 | page = pfn_to_page(pfn + i); |
| 424 | if (zone && page_zone(page) != zone) |
| 425 | return 0; |
| 426 | zone = page_zone(page); |
| 427 | } |
| 428 | return 1; |
| 429 | } |
| 430 | |
| 431 | /* |
| 432 | * Scanning pfn is much easier than scanning lru list. |
| 433 | * Scan pfn from start to end and Find LRU page. |
| 434 | */ |
| 435 | int scan_lru_pages(unsigned long start, unsigned long end) |
| 436 | { |
| 437 | unsigned long pfn; |
| 438 | struct page *page; |
| 439 | for (pfn = start; pfn < end; pfn++) { |
| 440 | if (pfn_valid(pfn)) { |
| 441 | page = pfn_to_page(pfn); |
| 442 | if (PageLRU(page)) |
| 443 | return pfn; |
| 444 | } |
| 445 | } |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | static struct page * |
| 450 | hotremove_migrate_alloc(struct page *page, |
| 451 | unsigned long private, |
| 452 | int **x) |
| 453 | { |
| 454 | /* This should be improoooooved!! */ |
| 455 | return alloc_page(GFP_HIGHUSER_PAGECACHE); |
| 456 | } |
| 457 | |
| 458 | |
| 459 | #define NR_OFFLINE_AT_ONCE_PAGES (256) |
| 460 | static int |
| 461 | do_migrate_range(unsigned long start_pfn, unsigned long end_pfn) |
| 462 | { |
| 463 | unsigned long pfn; |
| 464 | struct page *page; |
| 465 | int move_pages = NR_OFFLINE_AT_ONCE_PAGES; |
| 466 | int not_managed = 0; |
| 467 | int ret = 0; |
| 468 | LIST_HEAD(source); |
| 469 | |
| 470 | for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) { |
| 471 | if (!pfn_valid(pfn)) |
| 472 | continue; |
| 473 | page = pfn_to_page(pfn); |
| 474 | if (!page_count(page)) |
| 475 | continue; |
| 476 | /* |
| 477 | * We can skip free pages. And we can only deal with pages on |
| 478 | * LRU. |
| 479 | */ |
| 480 | ret = isolate_lru_page(page, &source); |
| 481 | if (!ret) { /* Success */ |
| 482 | move_pages--; |
| 483 | } else { |
| 484 | /* Becasue we don't have big zone->lock. we should |
| 485 | check this again here. */ |
| 486 | if (page_count(page)) |
| 487 | not_managed++; |
| 488 | #ifdef CONFIG_DEBUG_VM |
| 489 | printk(KERN_INFO "removing from LRU failed" |
| 490 | " %lx/%d/%lx\n", |
| 491 | pfn, page_count(page), page->flags); |
| 492 | #endif |
| 493 | } |
| 494 | } |
| 495 | ret = -EBUSY; |
| 496 | if (not_managed) { |
| 497 | if (!list_empty(&source)) |
| 498 | putback_lru_pages(&source); |
| 499 | goto out; |
| 500 | } |
| 501 | ret = 0; |
| 502 | if (list_empty(&source)) |
| 503 | goto out; |
| 504 | /* this function returns # of failed pages */ |
| 505 | ret = migrate_pages(&source, hotremove_migrate_alloc, 0); |
| 506 | |
| 507 | out: |
| 508 | return ret; |
| 509 | } |
| 510 | |
| 511 | /* |
| 512 | * remove from free_area[] and mark all as Reserved. |
| 513 | */ |
| 514 | static int |
| 515 | offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages, |
| 516 | void *data) |
| 517 | { |
| 518 | __offline_isolated_pages(start, start + nr_pages); |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | static void |
| 523 | offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn) |
| 524 | { |
| 525 | walk_memory_resource(start_pfn, end_pfn - start_pfn, NULL, |
| 526 | offline_isolated_pages_cb); |
| 527 | } |
| 528 | |
| 529 | /* |
| 530 | * Check all pages in range, recoreded as memory resource, are isolated. |
| 531 | */ |
| 532 | static int |
| 533 | check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages, |
| 534 | void *data) |
| 535 | { |
| 536 | int ret; |
| 537 | long offlined = *(long *)data; |
| 538 | ret = test_pages_isolated(start_pfn, start_pfn + nr_pages); |
| 539 | offlined = nr_pages; |
| 540 | if (!ret) |
| 541 | *(long *)data += offlined; |
| 542 | return ret; |
| 543 | } |
| 544 | |
| 545 | static long |
| 546 | check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn) |
| 547 | { |
| 548 | long offlined = 0; |
| 549 | int ret; |
| 550 | |
| 551 | ret = walk_memory_resource(start_pfn, end_pfn - start_pfn, &offlined, |
| 552 | check_pages_isolated_cb); |
| 553 | if (ret < 0) |
| 554 | offlined = (long)ret; |
| 555 | return offlined; |
| 556 | } |
| 557 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 558 | int offline_pages(unsigned long start_pfn, |
| 559 | unsigned long end_pfn, unsigned long timeout) |
| 560 | { |
| 561 | unsigned long pfn, nr_pages, expire; |
| 562 | long offlined_pages; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 563 | int ret, drain, retry_max, node; |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 564 | struct zone *zone; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 565 | struct memory_notify arg; |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 566 | |
| 567 | BUG_ON(start_pfn >= end_pfn); |
| 568 | /* at least, alignment against pageblock is necessary */ |
| 569 | if (!IS_ALIGNED(start_pfn, pageblock_nr_pages)) |
| 570 | return -EINVAL; |
| 571 | if (!IS_ALIGNED(end_pfn, pageblock_nr_pages)) |
| 572 | return -EINVAL; |
| 573 | /* This makes hotplug much easier...and readable. |
| 574 | we assume this for now. .*/ |
| 575 | if (!test_pages_in_a_zone(start_pfn, end_pfn)) |
| 576 | return -EINVAL; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 577 | |
| 578 | zone = page_zone(pfn_to_page(start_pfn)); |
| 579 | node = zone_to_nid(zone); |
| 580 | nr_pages = end_pfn - start_pfn; |
| 581 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 582 | /* set above range as isolated */ |
| 583 | ret = start_isolate_page_range(start_pfn, end_pfn); |
| 584 | if (ret) |
| 585 | return ret; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 586 | |
| 587 | arg.start_pfn = start_pfn; |
| 588 | arg.nr_pages = nr_pages; |
| 589 | arg.status_change_nid = -1; |
| 590 | if (nr_pages >= node_present_pages(node)) |
| 591 | arg.status_change_nid = node; |
| 592 | |
| 593 | ret = memory_notify(MEM_GOING_OFFLINE, &arg); |
| 594 | ret = notifier_to_errno(ret); |
| 595 | if (ret) |
| 596 | goto failed_removal; |
| 597 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 598 | pfn = start_pfn; |
| 599 | expire = jiffies + timeout; |
| 600 | drain = 0; |
| 601 | retry_max = 5; |
| 602 | repeat: |
| 603 | /* start memory hot removal */ |
| 604 | ret = -EAGAIN; |
| 605 | if (time_after(jiffies, expire)) |
| 606 | goto failed_removal; |
| 607 | ret = -EINTR; |
| 608 | if (signal_pending(current)) |
| 609 | goto failed_removal; |
| 610 | ret = 0; |
| 611 | if (drain) { |
| 612 | lru_add_drain_all(); |
| 613 | flush_scheduled_work(); |
| 614 | cond_resched(); |
Christoph Lameter | 9f8f217 | 2008-02-04 22:29:11 -0800 | [diff] [blame] | 615 | drain_all_pages(); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | pfn = scan_lru_pages(start_pfn, end_pfn); |
| 619 | if (pfn) { /* We have page on LRU */ |
| 620 | ret = do_migrate_range(pfn, end_pfn); |
| 621 | if (!ret) { |
| 622 | drain = 1; |
| 623 | goto repeat; |
| 624 | } else { |
| 625 | if (ret < 0) |
| 626 | if (--retry_max == 0) |
| 627 | goto failed_removal; |
| 628 | yield(); |
| 629 | drain = 1; |
| 630 | goto repeat; |
| 631 | } |
| 632 | } |
| 633 | /* drain all zone's lru pagevec, this is asyncronous... */ |
| 634 | lru_add_drain_all(); |
| 635 | flush_scheduled_work(); |
| 636 | yield(); |
| 637 | /* drain pcp pages , this is synchrouns. */ |
Christoph Lameter | 9f8f217 | 2008-02-04 22:29:11 -0800 | [diff] [blame] | 638 | drain_all_pages(); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 639 | /* check again */ |
| 640 | offlined_pages = check_pages_isolated(start_pfn, end_pfn); |
| 641 | if (offlined_pages < 0) { |
| 642 | ret = -EBUSY; |
| 643 | goto failed_removal; |
| 644 | } |
| 645 | printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages); |
| 646 | /* Ok, all of our target is islaoted. |
| 647 | We cannot do rollback at this point. */ |
| 648 | offline_isolated_pages(start_pfn, end_pfn); |
KAMEZAWA Hiroyuki | dbc0e4c | 2007-11-14 16:59:12 -0800 | [diff] [blame] | 649 | /* reset pagetype flags and makes migrate type to be MOVABLE */ |
| 650 | undo_isolate_page_range(start_pfn, end_pfn); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 651 | /* removal success */ |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 652 | zone->present_pages -= offlined_pages; |
| 653 | zone->zone_pgdat->node_present_pages -= offlined_pages; |
| 654 | totalram_pages -= offlined_pages; |
| 655 | num_physpages -= offlined_pages; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 656 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 657 | vm_total_pages = nr_free_pagecache_pages(); |
| 658 | writeback_set_ratelimit(); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 659 | |
| 660 | memory_notify(MEM_OFFLINE, &arg); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 661 | return 0; |
| 662 | |
| 663 | failed_removal: |
| 664 | printk(KERN_INFO "memory offlining %lx to %lx failed\n", |
| 665 | start_pfn, end_pfn); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 666 | memory_notify(MEM_CANCEL_OFFLINE, &arg); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 667 | /* pushback to free area */ |
| 668 | undo_isolate_page_range(start_pfn, end_pfn); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 669 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 670 | return ret; |
| 671 | } |
KAMEZAWA Hiroyuki | 48e9419 | 2007-10-16 01:26:14 -0700 | [diff] [blame] | 672 | #else |
| 673 | int remove_memory(u64 start, u64 size) |
| 674 | { |
| 675 | return -EINVAL; |
| 676 | } |
| 677 | EXPORT_SYMBOL_GPL(remove_memory); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 678 | #endif /* CONFIG_MEMORY_HOTREMOVE */ |