memory_hotplug.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * linux/mm/memory_hotplug.c
  3. *
  4. * Copyright (C)
  5. */
  6. #include <linux/config.h>
  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>
  16. #include <linux/slab.h>
  17. #include <linux/sysctl.h>
  18. #include <linux/cpu.h>
  19. #include <linux/memory.h>
  20. #include <linux/memory_hotplug.h>
  21. #include <linux/highmem.h>
  22. #include <linux/vmalloc.h>
  23. #include <asm/tlbflush.h>
  24. extern void zonetable_add(struct zone *zone, int nid, int zid, unsigned long pfn,
  25. unsigned long size);
  26. static void __add_zone(struct zone *zone, unsigned long phys_start_pfn)
  27. {
  28. struct pglist_data *pgdat = zone->zone_pgdat;
  29. int nr_pages = PAGES_PER_SECTION;
  30. int nid = pgdat->node_id;
  31. int zone_type;
  32. zone_type = zone - pgdat->node_zones;
  33. memmap_init_zone(nr_pages, nid, zone_type, phys_start_pfn);
  34. zonetable_add(zone, nid, zone_type, phys_start_pfn, nr_pages);
  35. }
  36. extern int sparse_add_one_section(struct zone *zone, unsigned long start_pfn,
  37. int nr_pages);
  38. static int __add_section(struct zone *zone, unsigned long phys_start_pfn)
  39. {
  40. int nr_pages = PAGES_PER_SECTION;
  41. int ret;
  42. ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
  43. if (ret < 0)
  44. return ret;
  45. __add_zone(zone, phys_start_pfn);
  46. return register_new_memory(__pfn_to_section(phys_start_pfn));
  47. }
  48. /*
  49. * Reasonably generic function for adding memory. It is
  50. * expected that archs that support memory hotplug will
  51. * call this function after deciding the zone to which to
  52. * add the new pages.
  53. */
  54. int __add_pages(struct zone *zone, unsigned long phys_start_pfn,
  55. unsigned long nr_pages)
  56. {
  57. unsigned long i;
  58. int err = 0;
  59. for (i = 0; i < nr_pages; i += PAGES_PER_SECTION) {
  60. err = __add_section(zone, phys_start_pfn + i);
  61. if (err)
  62. break;
  63. }
  64. return err;
  65. }
  66. static void grow_zone_span(struct zone *zone,
  67. unsigned long start_pfn, unsigned long end_pfn)
  68. {
  69. unsigned long old_zone_end_pfn;
  70. zone_span_writelock(zone);
  71. old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
  72. if (start_pfn < zone->zone_start_pfn)
  73. zone->zone_start_pfn = start_pfn;
  74. if (end_pfn > old_zone_end_pfn)
  75. zone->spanned_pages = end_pfn - zone->zone_start_pfn;
  76. zone_span_writeunlock(zone);
  77. }
  78. static void grow_pgdat_span(struct pglist_data *pgdat,
  79. unsigned long start_pfn, unsigned long end_pfn)
  80. {
  81. unsigned long old_pgdat_end_pfn =
  82. pgdat->node_start_pfn + pgdat->node_spanned_pages;
  83. if (start_pfn < pgdat->node_start_pfn)
  84. pgdat->node_start_pfn = start_pfn;
  85. if (end_pfn > old_pgdat_end_pfn)
  86. pgdat->node_spanned_pages = end_pfn - pgdat->node_start_pfn;
  87. }
  88. int online_pages(unsigned long pfn, unsigned long nr_pages)
  89. {
  90. unsigned long i;
  91. unsigned long flags;
  92. unsigned long onlined_pages = 0;
  93. struct zone *zone;
  94. /*
  95. * This doesn't need a lock to do pfn_to_page().
  96. * The section can't be removed here because of the
  97. * memory_block->state_sem.
  98. */
  99. zone = page_zone(pfn_to_page(pfn));
  100. pgdat_resize_lock(zone->zone_pgdat, &flags);
  101. grow_zone_span(zone, pfn, pfn + nr_pages);
  102. grow_pgdat_span(zone->zone_pgdat, pfn, pfn + nr_pages);
  103. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  104. for (i = 0; i < nr_pages; i++) {
  105. struct page *page = pfn_to_page(pfn + i);
  106. online_page(page);
  107. onlined_pages++;
  108. }
  109. zone->present_pages += onlined_pages;
  110. setup_per_zone_pages_min();
  111. return 0;
  112. }