memory_hotplug: handle empty zone when online_movable/online_kernel
[platform/adaptation/renesas_rcar/renesas_kernel.git] / mm / memory_hotplug.c
1 /*
2  *  linux/mm/memory_hotplug.c
3  *
4  *  Copyright (C)
5  */
6
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/export.h>
15 #include <linux/pagevec.h>
16 #include <linux/writeback.h>
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>
24 #include <linux/ioport.h>
25 #include <linux/delay.h>
26 #include <linux/migrate.h>
27 #include <linux/page-isolation.h>
28 #include <linux/pfn.h>
29 #include <linux/suspend.h>
30 #include <linux/mm_inline.h>
31 #include <linux/firmware-map.h>
32
33 #include <asm/tlbflush.h>
34
35 #include "internal.h"
36
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
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
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;
75         res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
76         if (request_resource(&iomem_resource, res) < 0) {
77                 printk("System RAM resource %pR cannot be added\n", res);
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
93 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
94 #ifndef CONFIG_SPARSEMEM_VMEMMAP
95 static void get_page_bootmem(unsigned long info,  struct page *page,
96                              unsigned long type)
97 {
98         page->lru.next = (struct list_head *) type;
99         SetPagePrivate(page);
100         set_page_private(page, info);
101         atomic_inc(&page->_count);
102 }
103
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)
107 {
108         unsigned long type;
109
110         type = (unsigned long) page->lru.next;
111         BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
112                type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
113
114         if (atomic_dec_return(&page->_count) == 1) {
115                 ClearPagePrivate(page);
116                 set_page_private(page, 0);
117                 INIT_LIST_HEAD(&page->lru);
118                 __free_pages_bootmem(page, 0);
119         }
120
121 }
122
123 static void register_page_bootmem_info_section(unsigned long start_pfn)
124 {
125         unsigned long *usemap, mapsize, section_nr, i;
126         struct mem_section *ms;
127         struct page *page, *memmap;
128
129         section_nr = pfn_to_section_nr(start_pfn);
130         ms = __nr_to_section(section_nr);
131
132         /* Get section's memmap address */
133         memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
134
135         /*
136          * Get page for the memmap's phys address
137          * XXX: need more consideration for sparse_vmemmap...
138          */
139         page = virt_to_page(memmap);
140         mapsize = sizeof(struct page) * PAGES_PER_SECTION;
141         mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
142
143         /* remember memmap's page */
144         for (i = 0; i < mapsize; i++, page++)
145                 get_page_bootmem(section_nr, page, SECTION_INFO);
146
147         usemap = __nr_to_section(section_nr)->pageblock_flags;
148         page = virt_to_page(usemap);
149
150         mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
151
152         for (i = 0; i < mapsize; i++, page++)
153                 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
154
155 }
156
157 void register_page_bootmem_info_node(struct pglist_data *pgdat)
158 {
159         unsigned long i, pfn, end_pfn, nr_pages;
160         int node = pgdat->node_id;
161         struct page *page;
162         struct zone *zone;
163
164         nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
165         page = virt_to_page(pgdat);
166
167         for (i = 0; i < nr_pages; i++, page++)
168                 get_page_bootmem(node, page, NODE_INFO);
169
170         zone = &pgdat->node_zones[0];
171         for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
172                 if (zone->wait_table) {
173                         nr_pages = zone->wait_table_hash_nr_entries
174                                 * sizeof(wait_queue_head_t);
175                         nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
176                         page = virt_to_page(zone->wait_table);
177
178                         for (i = 0; i < nr_pages; i++, page++)
179                                 get_page_bootmem(node, page, NODE_INFO);
180                 }
181         }
182
183         pfn = pgdat->node_start_pfn;
184         end_pfn = pfn + pgdat->node_spanned_pages;
185
186         /* register_section info */
187         for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
188                 /*
189                  * Some platforms can assign the same pfn to multiple nodes - on
190                  * node0 as well as nodeN.  To avoid registering a pfn against
191                  * multiple nodes we check that this pfn does not already
192                  * reside in some other node.
193                  */
194                 if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
195                         register_page_bootmem_info_section(pfn);
196         }
197 }
198 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
199
200 static void grow_zone_span(struct zone *zone, unsigned long start_pfn,
201                            unsigned long end_pfn)
202 {
203         unsigned long old_zone_end_pfn;
204
205         zone_span_writelock(zone);
206
207         old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
208         if (!zone->spanned_pages || start_pfn < zone->zone_start_pfn)
209                 zone->zone_start_pfn = start_pfn;
210
211         zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
212                                 zone->zone_start_pfn;
213
214         zone_span_writeunlock(zone);
215 }
216
217 static void resize_zone(struct zone *zone, unsigned long start_pfn,
218                 unsigned long end_pfn)
219 {
220         zone_span_writelock(zone);
221
222         if (end_pfn - start_pfn) {
223                 zone->zone_start_pfn = start_pfn;
224                 zone->spanned_pages = end_pfn - start_pfn;
225         } else {
226                 /*
227                  * make it consist as free_area_init_core(),
228                  * if spanned_pages = 0, then keep start_pfn = 0
229                  */
230                 zone->zone_start_pfn = 0;
231                 zone->spanned_pages = 0;
232         }
233
234         zone_span_writeunlock(zone);
235 }
236
237 static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
238                 unsigned long end_pfn)
239 {
240         enum zone_type zid = zone_idx(zone);
241         int nid = zone->zone_pgdat->node_id;
242         unsigned long pfn;
243
244         for (pfn = start_pfn; pfn < end_pfn; pfn++)
245                 set_page_links(pfn_to_page(pfn), zid, nid, pfn);
246 }
247
248 static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
249                 unsigned long start_pfn, unsigned long end_pfn)
250 {
251         int ret;
252         unsigned long flags;
253         unsigned long z1_start_pfn;
254
255         if (!z1->wait_table) {
256                 ret = init_currently_empty_zone(z1, start_pfn,
257                         end_pfn - start_pfn, MEMMAP_HOTPLUG);
258                 if (ret)
259                         return ret;
260         }
261
262         pgdat_resize_lock(z1->zone_pgdat, &flags);
263
264         /* can't move pfns which are higher than @z2 */
265         if (end_pfn > z2->zone_start_pfn + z2->spanned_pages)
266                 goto out_fail;
267         /* the move out part mast at the left most of @z2 */
268         if (start_pfn > z2->zone_start_pfn)
269                 goto out_fail;
270         /* must included/overlap */
271         if (end_pfn <= z2->zone_start_pfn)
272                 goto out_fail;
273
274         /* use start_pfn for z1's start_pfn if z1 is empty */
275         if (z1->spanned_pages)
276                 z1_start_pfn = z1->zone_start_pfn;
277         else
278                 z1_start_pfn = start_pfn;
279
280         resize_zone(z1, z1_start_pfn, end_pfn);
281         resize_zone(z2, end_pfn, z2->zone_start_pfn + z2->spanned_pages);
282
283         pgdat_resize_unlock(z1->zone_pgdat, &flags);
284
285         fix_zone_id(z1, start_pfn, end_pfn);
286
287         return 0;
288 out_fail:
289         pgdat_resize_unlock(z1->zone_pgdat, &flags);
290         return -1;
291 }
292
293 static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
294                 unsigned long start_pfn, unsigned long end_pfn)
295 {
296         int ret;
297         unsigned long flags;
298         unsigned long z2_end_pfn;
299
300         if (!z2->wait_table) {
301                 ret = init_currently_empty_zone(z2, start_pfn,
302                         end_pfn - start_pfn, MEMMAP_HOTPLUG);
303                 if (ret)
304                         return ret;
305         }
306
307         pgdat_resize_lock(z1->zone_pgdat, &flags);
308
309         /* can't move pfns which are lower than @z1 */
310         if (z1->zone_start_pfn > start_pfn)
311                 goto out_fail;
312         /* the move out part mast at the right most of @z1 */
313         if (z1->zone_start_pfn + z1->spanned_pages >  end_pfn)
314                 goto out_fail;
315         /* must included/overlap */
316         if (start_pfn >= z1->zone_start_pfn + z1->spanned_pages)
317                 goto out_fail;
318
319         /* use end_pfn for z2's end_pfn if z2 is empty */
320         if (z2->spanned_pages)
321                 z2_end_pfn = z2->zone_start_pfn + z2->spanned_pages;
322         else
323                 z2_end_pfn = end_pfn;
324
325         resize_zone(z1, z1->zone_start_pfn, start_pfn);
326         resize_zone(z2, start_pfn, z2_end_pfn);
327
328         pgdat_resize_unlock(z1->zone_pgdat, &flags);
329
330         fix_zone_id(z2, start_pfn, end_pfn);
331
332         return 0;
333 out_fail:
334         pgdat_resize_unlock(z1->zone_pgdat, &flags);
335         return -1;
336 }
337
338 static void grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
339                             unsigned long end_pfn)
340 {
341         unsigned long old_pgdat_end_pfn =
342                 pgdat->node_start_pfn + pgdat->node_spanned_pages;
343
344         if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
345                 pgdat->node_start_pfn = start_pfn;
346
347         pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
348                                         pgdat->node_start_pfn;
349 }
350
351 static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
352 {
353         struct pglist_data *pgdat = zone->zone_pgdat;
354         int nr_pages = PAGES_PER_SECTION;
355         int nid = pgdat->node_id;
356         int zone_type;
357         unsigned long flags;
358
359         zone_type = zone - pgdat->node_zones;
360         if (!zone->wait_table) {
361                 int ret;
362
363                 ret = init_currently_empty_zone(zone, phys_start_pfn,
364                                                 nr_pages, MEMMAP_HOTPLUG);
365                 if (ret)
366                         return ret;
367         }
368         pgdat_resize_lock(zone->zone_pgdat, &flags);
369         grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
370         grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
371                         phys_start_pfn + nr_pages);
372         pgdat_resize_unlock(zone->zone_pgdat, &flags);
373         memmap_init_zone(nr_pages, nid, zone_type,
374                          phys_start_pfn, MEMMAP_HOTPLUG);
375         return 0;
376 }
377
378 static int __meminit __add_section(int nid, struct zone *zone,
379                                         unsigned long phys_start_pfn)
380 {
381         int nr_pages = PAGES_PER_SECTION;
382         int ret;
383
384         if (pfn_valid(phys_start_pfn))
385                 return -EEXIST;
386
387         ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
388
389         if (ret < 0)
390                 return ret;
391
392         ret = __add_zone(zone, phys_start_pfn);
393
394         if (ret < 0)
395                 return ret;
396
397         return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
398 }
399
400 #ifdef CONFIG_SPARSEMEM_VMEMMAP
401 static int __remove_section(struct zone *zone, struct mem_section *ms)
402 {
403         /*
404          * XXX: Freeing memmap with vmemmap is not implement yet.
405          *      This should be removed later.
406          */
407         return -EBUSY;
408 }
409 #else
410 static int __remove_section(struct zone *zone, struct mem_section *ms)
411 {
412         unsigned long flags;
413         struct pglist_data *pgdat = zone->zone_pgdat;
414         int ret = -EINVAL;
415
416         if (!valid_section(ms))
417                 return ret;
418
419         ret = unregister_memory_section(ms);
420         if (ret)
421                 return ret;
422
423         pgdat_resize_lock(pgdat, &flags);
424         sparse_remove_one_section(zone, ms);
425         pgdat_resize_unlock(pgdat, &flags);
426         return 0;
427 }
428 #endif
429
430 /*
431  * Reasonably generic function for adding memory.  It is
432  * expected that archs that support memory hotplug will
433  * call this function after deciding the zone to which to
434  * add the new pages.
435  */
436 int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
437                         unsigned long nr_pages)
438 {
439         unsigned long i;
440         int err = 0;
441         int start_sec, end_sec;
442         /* during initialize mem_map, align hot-added range to section */
443         start_sec = pfn_to_section_nr(phys_start_pfn);
444         end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
445
446         for (i = start_sec; i <= end_sec; i++) {
447                 err = __add_section(nid, zone, i << PFN_SECTION_SHIFT);
448
449                 /*
450                  * EEXIST is finally dealt with by ioresource collision
451                  * check. see add_memory() => register_memory_resource()
452                  * Warning will be printed if there is collision.
453                  */
454                 if (err && (err != -EEXIST))
455                         break;
456                 err = 0;
457         }
458
459         return err;
460 }
461 EXPORT_SYMBOL_GPL(__add_pages);
462
463 /**
464  * __remove_pages() - remove sections of pages from a zone
465  * @zone: zone from which pages need to be removed
466  * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
467  * @nr_pages: number of pages to remove (must be multiple of section size)
468  *
469  * Generic helper function to remove section mappings and sysfs entries
470  * for the section of the memory we are removing. Caller needs to make
471  * sure that pages are marked reserved and zones are adjust properly by
472  * calling offline_pages().
473  */
474 int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
475                  unsigned long nr_pages)
476 {
477         unsigned long i, ret = 0;
478         int sections_to_remove;
479
480         /*
481          * We can only remove entire sections
482          */
483         BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
484         BUG_ON(nr_pages % PAGES_PER_SECTION);
485
486         release_mem_region(phys_start_pfn << PAGE_SHIFT, nr_pages * PAGE_SIZE);
487
488         sections_to_remove = nr_pages / PAGES_PER_SECTION;
489         for (i = 0; i < sections_to_remove; i++) {
490                 unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
491                 ret = __remove_section(zone, __pfn_to_section(pfn));
492                 if (ret)
493                         break;
494         }
495         return ret;
496 }
497 EXPORT_SYMBOL_GPL(__remove_pages);
498
499 int set_online_page_callback(online_page_callback_t callback)
500 {
501         int rc = -EINVAL;
502
503         lock_memory_hotplug();
504
505         if (online_page_callback == generic_online_page) {
506                 online_page_callback = callback;
507                 rc = 0;
508         }
509
510         unlock_memory_hotplug();
511
512         return rc;
513 }
514 EXPORT_SYMBOL_GPL(set_online_page_callback);
515
516 int restore_online_page_callback(online_page_callback_t callback)
517 {
518         int rc = -EINVAL;
519
520         lock_memory_hotplug();
521
522         if (online_page_callback == callback) {
523                 online_page_callback = generic_online_page;
524                 rc = 0;
525         }
526
527         unlock_memory_hotplug();
528
529         return rc;
530 }
531 EXPORT_SYMBOL_GPL(restore_online_page_callback);
532
533 void __online_page_set_limits(struct page *page)
534 {
535         unsigned long pfn = page_to_pfn(page);
536
537         if (pfn >= num_physpages)
538                 num_physpages = pfn + 1;
539 }
540 EXPORT_SYMBOL_GPL(__online_page_set_limits);
541
542 void __online_page_increment_counters(struct page *page)
543 {
544         totalram_pages++;
545
546 #ifdef CONFIG_HIGHMEM
547         if (PageHighMem(page))
548                 totalhigh_pages++;
549 #endif
550 }
551 EXPORT_SYMBOL_GPL(__online_page_increment_counters);
552
553 void __online_page_free(struct page *page)
554 {
555         ClearPageReserved(page);
556         init_page_count(page);
557         __free_page(page);
558 }
559 EXPORT_SYMBOL_GPL(__online_page_free);
560
561 static void generic_online_page(struct page *page)
562 {
563         __online_page_set_limits(page);
564         __online_page_increment_counters(page);
565         __online_page_free(page);
566 }
567
568 static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
569                         void *arg)
570 {
571         unsigned long i;
572         unsigned long onlined_pages = *(unsigned long *)arg;
573         struct page *page;
574         if (PageReserved(pfn_to_page(start_pfn)))
575                 for (i = 0; i < nr_pages; i++) {
576                         page = pfn_to_page(start_pfn + i);
577                         (*online_page_callback)(page);
578                         onlined_pages++;
579                 }
580         *(unsigned long *)arg = onlined_pages;
581         return 0;
582 }
583
584 /* check which state of node_states will be changed when online memory */
585 static void node_states_check_changes_online(unsigned long nr_pages,
586         struct zone *zone, struct memory_notify *arg)
587 {
588         int nid = zone_to_nid(zone);
589         enum zone_type zone_last = ZONE_NORMAL;
590
591         /*
592          * If we have HIGHMEM, node_states[N_NORMAL_MEMORY] contains nodes
593          * which have 0...ZONE_NORMAL, set zone_last to ZONE_NORMAL.
594          *
595          * If we don't have HIGHMEM, node_states[N_NORMAL_MEMORY] contains nodes
596          * which have 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
597          */
598         if (N_HIGH_MEMORY == N_NORMAL_MEMORY)
599                 zone_last = ZONE_MOVABLE;
600
601         /*
602          * if the memory to be online is in a zone of 0...zone_last, and
603          * the zones of 0...zone_last don't have memory before online, we will
604          * need to set the node to node_states[N_NORMAL_MEMORY] after
605          * the memory is online.
606          */
607         if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
608                 arg->status_change_nid_normal = nid;
609         else
610                 arg->status_change_nid_normal = -1;
611
612         /*
613          * if the node don't have memory befor online, we will need to
614          * set the node to node_states[N_HIGH_MEMORY] after the memory
615          * is online.
616          */
617         if (!node_state(nid, N_HIGH_MEMORY))
618                 arg->status_change_nid = nid;
619         else
620                 arg->status_change_nid = -1;
621 }
622
623 static void node_states_set_node(int node, struct memory_notify *arg)
624 {
625         if (arg->status_change_nid_normal >= 0)
626                 node_set_state(node, N_NORMAL_MEMORY);
627
628         node_set_state(node, N_HIGH_MEMORY);
629 }
630
631
632 int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
633 {
634         unsigned long onlined_pages = 0;
635         struct zone *zone;
636         int need_zonelists_rebuild = 0;
637         int nid;
638         int ret;
639         struct memory_notify arg;
640
641         lock_memory_hotplug();
642         /*
643          * This doesn't need a lock to do pfn_to_page().
644          * The section can't be removed here because of the
645          * memory_block->state_mutex.
646          */
647         zone = page_zone(pfn_to_page(pfn));
648
649         if (online_type == ONLINE_KERNEL && zone_idx(zone) == ZONE_MOVABLE) {
650                 if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages)) {
651                         unlock_memory_hotplug();
652                         return -1;
653                 }
654         }
655         if (online_type == ONLINE_MOVABLE && zone_idx(zone) == ZONE_MOVABLE - 1) {
656                 if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages)) {
657                         unlock_memory_hotplug();
658                         return -1;
659                 }
660         }
661
662         /* Previous code may changed the zone of the pfn range */
663         zone = page_zone(pfn_to_page(pfn));
664
665         arg.start_pfn = pfn;
666         arg.nr_pages = nr_pages;
667         node_states_check_changes_online(nr_pages, zone, &arg);
668
669         nid = page_to_nid(pfn_to_page(pfn));
670
671         ret = memory_notify(MEM_GOING_ONLINE, &arg);
672         ret = notifier_to_errno(ret);
673         if (ret) {
674                 memory_notify(MEM_CANCEL_ONLINE, &arg);
675                 unlock_memory_hotplug();
676                 return ret;
677         }
678         /*
679          * If this zone is not populated, then it is not in zonelist.
680          * This means the page allocator ignores this zone.
681          * So, zonelist must be updated after online.
682          */
683         mutex_lock(&zonelists_mutex);
684         if (!populated_zone(zone)) {
685                 need_zonelists_rebuild = 1;
686                 build_all_zonelists(NULL, zone);
687         }
688
689         ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
690                 online_pages_range);
691         if (ret) {
692                 if (need_zonelists_rebuild)
693                         zone_pcp_reset(zone);
694                 mutex_unlock(&zonelists_mutex);
695                 printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
696                        (unsigned long long) pfn << PAGE_SHIFT,
697                        (((unsigned long long) pfn + nr_pages)
698                             << PAGE_SHIFT) - 1);
699                 memory_notify(MEM_CANCEL_ONLINE, &arg);
700                 unlock_memory_hotplug();
701                 return ret;
702         }
703
704         zone->present_pages += onlined_pages;
705         zone->zone_pgdat->node_present_pages += onlined_pages;
706         if (onlined_pages) {
707                 node_states_set_node(zone_to_nid(zone), &arg);
708                 if (need_zonelists_rebuild)
709                         build_all_zonelists(NULL, NULL);
710                 else
711                         zone_pcp_update(zone);
712         }
713
714         mutex_unlock(&zonelists_mutex);
715
716         init_per_zone_wmark_min();
717
718         if (onlined_pages)
719                 kswapd_run(zone_to_nid(zone));
720
721         vm_total_pages = nr_free_pagecache_pages();
722
723         writeback_set_ratelimit();
724
725         if (onlined_pages)
726                 memory_notify(MEM_ONLINE, &arg);
727         unlock_memory_hotplug();
728
729         return 0;
730 }
731 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
732
733 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
734 static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
735 {
736         struct pglist_data *pgdat;
737         unsigned long zones_size[MAX_NR_ZONES] = {0};
738         unsigned long zholes_size[MAX_NR_ZONES] = {0};
739         unsigned long start_pfn = start >> PAGE_SHIFT;
740
741         pgdat = arch_alloc_nodedata(nid);
742         if (!pgdat)
743                 return NULL;
744
745         arch_refresh_nodedata(nid, pgdat);
746
747         /* we can use NODE_DATA(nid) from here */
748
749         /* init node's zones as empty zones, we don't have any present pages.*/
750         free_area_init_node(nid, zones_size, start_pfn, zholes_size);
751
752         /*
753          * The node we allocated has no zone fallback lists. For avoiding
754          * to access not-initialized zonelist, build here.
755          */
756         mutex_lock(&zonelists_mutex);
757         build_all_zonelists(pgdat, NULL);
758         mutex_unlock(&zonelists_mutex);
759
760         return pgdat;
761 }
762
763 static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
764 {
765         arch_refresh_nodedata(nid, NULL);
766         arch_free_nodedata(pgdat);
767         return;
768 }
769
770
771 /*
772  * called by cpu_up() to online a node without onlined memory.
773  */
774 int mem_online_node(int nid)
775 {
776         pg_data_t       *pgdat;
777         int     ret;
778
779         lock_memory_hotplug();
780         pgdat = hotadd_new_pgdat(nid, 0);
781         if (!pgdat) {
782                 ret = -ENOMEM;
783                 goto out;
784         }
785         node_set_online(nid);
786         ret = register_one_node(nid);
787         BUG_ON(ret);
788
789 out:
790         unlock_memory_hotplug();
791         return ret;
792 }
793
794 /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
795 int __ref add_memory(int nid, u64 start, u64 size)
796 {
797         pg_data_t *pgdat = NULL;
798         int new_pgdat = 0;
799         struct resource *res;
800         int ret;
801
802         lock_memory_hotplug();
803
804         res = register_memory_resource(start, size);
805         ret = -EEXIST;
806         if (!res)
807                 goto out;
808
809         if (!node_online(nid)) {
810                 pgdat = hotadd_new_pgdat(nid, start);
811                 ret = -ENOMEM;
812                 if (!pgdat)
813                         goto error;
814                 new_pgdat = 1;
815         }
816
817         /* call arch's memory hotadd */
818         ret = arch_add_memory(nid, start, size);
819
820         if (ret < 0)
821                 goto error;
822
823         /* we online node here. we can't roll back from here. */
824         node_set_online(nid);
825
826         if (new_pgdat) {
827                 ret = register_one_node(nid);
828                 /*
829                  * If sysfs file of new node can't create, cpu on the node
830                  * can't be hot-added. There is no rollback way now.
831                  * So, check by BUG_ON() to catch it reluctantly..
832                  */
833                 BUG_ON(ret);
834         }
835
836         /* create new memmap entry */
837         firmware_map_add_hotplug(start, start + size, "System RAM");
838
839         goto out;
840
841 error:
842         /* rollback pgdat allocation and others */
843         if (new_pgdat)
844                 rollback_node_hotadd(nid, pgdat);
845         if (res)
846                 release_memory_resource(res);
847
848 out:
849         unlock_memory_hotplug();
850         return ret;
851 }
852 EXPORT_SYMBOL_GPL(add_memory);
853
854 #ifdef CONFIG_MEMORY_HOTREMOVE
855 /*
856  * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
857  * set and the size of the free page is given by page_order(). Using this,
858  * the function determines if the pageblock contains only free pages.
859  * Due to buddy contraints, a free page at least the size of a pageblock will
860  * be located at the start of the pageblock
861  */
862 static inline int pageblock_free(struct page *page)
863 {
864         return PageBuddy(page) && page_order(page) >= pageblock_order;
865 }
866
867 /* Return the start of the next active pageblock after a given page */
868 static struct page *next_active_pageblock(struct page *page)
869 {
870         /* Ensure the starting page is pageblock-aligned */
871         BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
872
873         /* If the entire pageblock is free, move to the end of free page */
874         if (pageblock_free(page)) {
875                 int order;
876                 /* be careful. we don't have locks, page_order can be changed.*/
877                 order = page_order(page);
878                 if ((order < MAX_ORDER) && (order >= pageblock_order))
879                         return page + (1 << order);
880         }
881
882         return page + pageblock_nr_pages;
883 }
884
885 /* Checks if this range of memory is likely to be hot-removable. */
886 int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
887 {
888         struct page *page = pfn_to_page(start_pfn);
889         struct page *end_page = page + nr_pages;
890
891         /* Check the starting page of each pageblock within the range */
892         for (; page < end_page; page = next_active_pageblock(page)) {
893                 if (!is_pageblock_removable_nolock(page))
894                         return 0;
895                 cond_resched();
896         }
897
898         /* All pageblocks in the memory block are likely to be hot-removable */
899         return 1;
900 }
901
902 /*
903  * Confirm all pages in a range [start, end) is belongs to the same zone.
904  */
905 static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
906 {
907         unsigned long pfn;
908         struct zone *zone = NULL;
909         struct page *page;
910         int i;
911         for (pfn = start_pfn;
912              pfn < end_pfn;
913              pfn += MAX_ORDER_NR_PAGES) {
914                 i = 0;
915                 /* This is just a CONFIG_HOLES_IN_ZONE check.*/
916                 while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
917                         i++;
918                 if (i == MAX_ORDER_NR_PAGES)
919                         continue;
920                 page = pfn_to_page(pfn + i);
921                 if (zone && page_zone(page) != zone)
922                         return 0;
923                 zone = page_zone(page);
924         }
925         return 1;
926 }
927
928 /*
929  * Scanning pfn is much easier than scanning lru list.
930  * Scan pfn from start to end and Find LRU page.
931  */
932 static unsigned long scan_lru_pages(unsigned long start, unsigned long end)
933 {
934         unsigned long pfn;
935         struct page *page;
936         for (pfn = start; pfn < end; pfn++) {
937                 if (pfn_valid(pfn)) {
938                         page = pfn_to_page(pfn);
939                         if (PageLRU(page))
940                                 return pfn;
941                 }
942         }
943         return 0;
944 }
945
946 #define NR_OFFLINE_AT_ONCE_PAGES        (256)
947 static int
948 do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
949 {
950         unsigned long pfn;
951         struct page *page;
952         int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
953         int not_managed = 0;
954         int ret = 0;
955         LIST_HEAD(source);
956
957         for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
958                 if (!pfn_valid(pfn))
959                         continue;
960                 page = pfn_to_page(pfn);
961                 if (!get_page_unless_zero(page))
962                         continue;
963                 /*
964                  * We can skip free pages. And we can only deal with pages on
965                  * LRU.
966                  */
967                 ret = isolate_lru_page(page);
968                 if (!ret) { /* Success */
969                         put_page(page);
970                         list_add_tail(&page->lru, &source);
971                         move_pages--;
972                         inc_zone_page_state(page, NR_ISOLATED_ANON +
973                                             page_is_file_cache(page));
974
975                 } else {
976 #ifdef CONFIG_DEBUG_VM
977                         printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
978                                pfn);
979                         dump_page(page);
980 #endif
981                         put_page(page);
982                         /* Because we don't have big zone->lock. we should
983                            check this again here. */
984                         if (page_count(page)) {
985                                 not_managed++;
986                                 ret = -EBUSY;
987                                 break;
988                         }
989                 }
990         }
991         if (!list_empty(&source)) {
992                 if (not_managed) {
993                         putback_lru_pages(&source);
994                         goto out;
995                 }
996
997                 /*
998                  * alloc_migrate_target should be improooooved!!
999                  * migrate_pages returns # of failed pages.
1000                  */
1001                 ret = migrate_pages(&source, alloc_migrate_target, 0,
1002                                                         true, MIGRATE_SYNC);
1003                 if (ret)
1004                         putback_lru_pages(&source);
1005         }
1006 out:
1007         return ret;
1008 }
1009
1010 /*
1011  * remove from free_area[] and mark all as Reserved.
1012  */
1013 static int
1014 offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
1015                         void *data)
1016 {
1017         __offline_isolated_pages(start, start + nr_pages);
1018         return 0;
1019 }
1020
1021 static void
1022 offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
1023 {
1024         walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
1025                                 offline_isolated_pages_cb);
1026 }
1027
1028 /*
1029  * Check all pages in range, recoreded as memory resource, are isolated.
1030  */
1031 static int
1032 check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
1033                         void *data)
1034 {
1035         int ret;
1036         long offlined = *(long *)data;
1037         ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
1038         offlined = nr_pages;
1039         if (!ret)
1040                 *(long *)data += offlined;
1041         return ret;
1042 }
1043
1044 static long
1045 check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
1046 {
1047         long offlined = 0;
1048         int ret;
1049
1050         ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
1051                         check_pages_isolated_cb);
1052         if (ret < 0)
1053                 offlined = (long)ret;
1054         return offlined;
1055 }
1056
1057 /* check which state of node_states will be changed when offline memory */
1058 static void node_states_check_changes_offline(unsigned long nr_pages,
1059                 struct zone *zone, struct memory_notify *arg)
1060 {
1061         struct pglist_data *pgdat = zone->zone_pgdat;
1062         unsigned long present_pages = 0;
1063         enum zone_type zt, zone_last = ZONE_NORMAL;
1064
1065         /*
1066          * If we have HIGHMEM, node_states[N_NORMAL_MEMORY] contains nodes
1067          * which have 0...ZONE_NORMAL, set zone_last to ZONE_NORMAL.
1068          *
1069          * If we don't have HIGHMEM, node_states[N_NORMAL_MEMORY] contains nodes
1070          * which have 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
1071          */
1072         if (N_HIGH_MEMORY == N_NORMAL_MEMORY)
1073                 zone_last = ZONE_MOVABLE;
1074
1075         /*
1076          * check whether node_states[N_NORMAL_MEMORY] will be changed.
1077          * If the memory to be offline is in a zone of 0...zone_last,
1078          * and it is the last present memory, 0...zone_last will
1079          * become empty after offline , thus we can determind we will
1080          * need to clear the node from node_states[N_NORMAL_MEMORY].
1081          */
1082         for (zt = 0; zt <= zone_last; zt++)
1083                 present_pages += pgdat->node_zones[zt].present_pages;
1084         if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
1085                 arg->status_change_nid_normal = zone_to_nid(zone);
1086         else
1087                 arg->status_change_nid_normal = -1;
1088
1089         /*
1090          * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
1091          */
1092         zone_last = ZONE_MOVABLE;
1093
1094         /*
1095          * check whether node_states[N_HIGH_MEMORY] will be changed
1096          * If we try to offline the last present @nr_pages from the node,
1097          * we can determind we will need to clear the node from
1098          * node_states[N_HIGH_MEMORY].
1099          */
1100         for (; zt <= zone_last; zt++)
1101                 present_pages += pgdat->node_zones[zt].present_pages;
1102         if (nr_pages >= present_pages)
1103                 arg->status_change_nid = zone_to_nid(zone);
1104         else
1105                 arg->status_change_nid = -1;
1106 }
1107
1108 static void node_states_clear_node(int node, struct memory_notify *arg)
1109 {
1110         if (arg->status_change_nid_normal >= 0)
1111                 node_clear_state(node, N_NORMAL_MEMORY);
1112
1113         if ((N_HIGH_MEMORY != N_NORMAL_MEMORY) &&
1114             (arg->status_change_nid >= 0))
1115                 node_clear_state(node, N_HIGH_MEMORY);
1116 }
1117
1118 static int __ref __offline_pages(unsigned long start_pfn,
1119                   unsigned long end_pfn, unsigned long timeout)
1120 {
1121         unsigned long pfn, nr_pages, expire;
1122         long offlined_pages;
1123         int ret, drain, retry_max, node;
1124         struct zone *zone;
1125         struct memory_notify arg;
1126
1127         BUG_ON(start_pfn >= end_pfn);
1128         /* at least, alignment against pageblock is necessary */
1129         if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
1130                 return -EINVAL;
1131         if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
1132                 return -EINVAL;
1133         /* This makes hotplug much easier...and readable.
1134            we assume this for now. .*/
1135         if (!test_pages_in_a_zone(start_pfn, end_pfn))
1136                 return -EINVAL;
1137
1138         lock_memory_hotplug();
1139
1140         zone = page_zone(pfn_to_page(start_pfn));
1141         node = zone_to_nid(zone);
1142         nr_pages = end_pfn - start_pfn;
1143
1144         /* set above range as isolated */
1145         ret = start_isolate_page_range(start_pfn, end_pfn,
1146                                        MIGRATE_MOVABLE, true);
1147         if (ret)
1148                 goto out;
1149
1150         arg.start_pfn = start_pfn;
1151         arg.nr_pages = nr_pages;
1152         node_states_check_changes_offline(nr_pages, zone, &arg);
1153
1154         ret = memory_notify(MEM_GOING_OFFLINE, &arg);
1155         ret = notifier_to_errno(ret);
1156         if (ret)
1157                 goto failed_removal;
1158
1159         pfn = start_pfn;
1160         expire = jiffies + timeout;
1161         drain = 0;
1162         retry_max = 5;
1163 repeat:
1164         /* start memory hot removal */
1165         ret = -EAGAIN;
1166         if (time_after(jiffies, expire))
1167                 goto failed_removal;
1168         ret = -EINTR;
1169         if (signal_pending(current))
1170                 goto failed_removal;
1171         ret = 0;
1172         if (drain) {
1173                 lru_add_drain_all();
1174                 cond_resched();
1175                 drain_all_pages();
1176         }
1177
1178         pfn = scan_lru_pages(start_pfn, end_pfn);
1179         if (pfn) { /* We have page on LRU */
1180                 ret = do_migrate_range(pfn, end_pfn);
1181                 if (!ret) {
1182                         drain = 1;
1183                         goto repeat;
1184                 } else {
1185                         if (ret < 0)
1186                                 if (--retry_max == 0)
1187                                         goto failed_removal;
1188                         yield();
1189                         drain = 1;
1190                         goto repeat;
1191                 }
1192         }
1193         /* drain all zone's lru pagevec, this is asyncronous... */
1194         lru_add_drain_all();
1195         yield();
1196         /* drain pcp pages , this is synchrouns. */
1197         drain_all_pages();
1198         /* check again */
1199         offlined_pages = check_pages_isolated(start_pfn, end_pfn);
1200         if (offlined_pages < 0) {
1201                 ret = -EBUSY;
1202                 goto failed_removal;
1203         }
1204         printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
1205         /* Ok, all of our target is islaoted.
1206            We cannot do rollback at this point. */
1207         offline_isolated_pages(start_pfn, end_pfn);
1208         /* reset pagetype flags and makes migrate type to be MOVABLE */
1209         undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1210         /* removal success */
1211         zone->present_pages -= offlined_pages;
1212         zone->zone_pgdat->node_present_pages -= offlined_pages;
1213         totalram_pages -= offlined_pages;
1214
1215         init_per_zone_wmark_min();
1216
1217         if (!populated_zone(zone)) {
1218                 zone_pcp_reset(zone);
1219                 mutex_lock(&zonelists_mutex);
1220                 build_all_zonelists(NULL, NULL);
1221                 mutex_unlock(&zonelists_mutex);
1222         } else
1223                 zone_pcp_update(zone);
1224
1225         node_states_clear_node(node, &arg);
1226         if (arg.status_change_nid >= 0)
1227                 kswapd_stop(node);
1228
1229         vm_total_pages = nr_free_pagecache_pages();
1230         writeback_set_ratelimit();
1231
1232         memory_notify(MEM_OFFLINE, &arg);
1233         unlock_memory_hotplug();
1234         return 0;
1235
1236 failed_removal:
1237         printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
1238                (unsigned long long) start_pfn << PAGE_SHIFT,
1239                ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
1240         memory_notify(MEM_CANCEL_OFFLINE, &arg);
1241         /* pushback to free area */
1242         undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
1243
1244 out:
1245         unlock_memory_hotplug();
1246         return ret;
1247 }
1248
1249 int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1250 {
1251         return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
1252 }
1253
1254 int remove_memory(u64 start, u64 size)
1255 {
1256         struct memory_block *mem = NULL;
1257         struct mem_section *section;
1258         unsigned long start_pfn, end_pfn;
1259         unsigned long pfn, section_nr;
1260         int ret;
1261
1262         start_pfn = PFN_DOWN(start);
1263         end_pfn = start_pfn + PFN_DOWN(size);
1264
1265         for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1266                 section_nr = pfn_to_section_nr(pfn);
1267                 if (!present_section_nr(section_nr))
1268                         continue;
1269
1270                 section = __nr_to_section(section_nr);
1271                 /* same memblock? */
1272                 if (mem)
1273                         if ((section_nr >= mem->start_section_nr) &&
1274                             (section_nr <= mem->end_section_nr))
1275                                 continue;
1276
1277                 mem = find_memory_block_hinted(section, mem);
1278                 if (!mem)
1279                         continue;
1280
1281                 ret = offline_memory_block(mem);
1282                 if (ret) {
1283                         kobject_put(&mem->dev.kobj);
1284                         return ret;
1285                 }
1286         }
1287
1288         if (mem)
1289                 kobject_put(&mem->dev.kobj);
1290
1291         return 0;
1292 }
1293 #else
1294 int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
1295 {
1296         return -EINVAL;
1297 }
1298 int remove_memory(u64 start, u64 size)
1299 {
1300         return -EINVAL;
1301 }
1302 #endif /* CONFIG_MEMORY_HOTREMOVE */
1303 EXPORT_SYMBOL_GPL(remove_memory);