memory_hotplug.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. struct pglist_data *pgdat = zone->zone_pgdat;
  41. int nr_pages = PAGES_PER_SECTION;
  42. int ret;
  43. ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
  44. if (ret < 0)
  45. return ret;
  46. __add_zone(zone, phys_start_pfn);
  47. return register_new_memory(__pfn_to_section(phys_start_pfn));
  48. }
  49. /*
  50. * Reasonably generic function for adding memory. It is
  51. * expected that archs that support memory hotplug will
  52. * call this function after deciding the zone to which to
  53. * add the new pages.
  54. */
  55. int __add_pages(struct zone *zone, unsigned long phys_start_pfn,
  56. unsigned long nr_pages)
  57. {
  58. unsigned long i;
  59. int err = 0;
  60. for (i = 0; i < nr_pages; i += PAGES_PER_SECTION) {
  61. err = __add_section(zone, phys_start_pfn + i);
  62. if (err)
  63. break;
  64. }
  65. return err;
  66. }
  67. static void grow_zone_span(struct zone *zone,
  68. unsigned long start_pfn, unsigned long end_pfn)
  69. {
  70. unsigned long old_zone_end_pfn;
  71. zone_span_writelock(zone);
  72. old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
  73. if (start_pfn < zone->zone_start_pfn)
  74. zone->zone_start_pfn = start_pfn;
  75. if (end_pfn > old_zone_end_pfn)
  76. zone->spanned_pages = end_pfn - zone->zone_start_pfn;
  77. zone_span_writeunlock(zone);
  78. }
  79. static void grow_pgdat_span(struct pglist_data *pgdat,
  80. unsigned long start_pfn, unsigned long end_pfn)
  81. {
  82. unsigned long old_pgdat_end_pfn =
  83. pgdat->node_start_pfn + pgdat->node_spanned_pages;
  84. if (start_pfn < pgdat->node_start_pfn)
  85. pgdat->node_start_pfn = start_pfn;
  86. if (end_pfn > old_pgdat_end_pfn)
  87. pgdat->node_spanned_pages = end_pfn - pgdat->node_start_pfn;
  88. }
  89. int online_pages(unsigned long pfn, unsigned long nr_pages)
  90. {
  91. unsigned long i;
  92. unsigned long flags;
  93. unsigned long onlined_pages = 0;
  94. struct zone *zone;
  95. /*
  96. * This doesn't need a lock to do pfn_to_page().
  97. * The section can't be removed here because of the
  98. * memory_block->state_sem.
  99. */
  100. zone = page_zone(pfn_to_page(pfn));
  101. pgdat_resize_lock(zone->zone_pgdat, &flags);
  102. grow_zone_span(zone, pfn, pfn + nr_pages);
  103. grow_pgdat_span(zone->zone_pgdat, pfn, pfn + nr_pages);
  104. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  105. for (i = 0; i < nr_pages; i++) {
  106. struct page *page = pfn_to_page(pfn + i);
  107. online_page(page);
  108. onlined_pages++;
  109. }
  110. zone->present_pages += onlined_pages;
  111. setup_per_zone_pages_min();
  112. return 0;
  113. }