memory_hotplug.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 <linux/ioport.h>
  24. #include <asm/tlbflush.h>
  25. extern void zonetable_add(struct zone *zone, int nid, int zid, unsigned long pfn,
  26. unsigned long size);
  27. static int __add_zone(struct zone *zone, unsigned long phys_start_pfn)
  28. {
  29. struct pglist_data *pgdat = zone->zone_pgdat;
  30. int nr_pages = PAGES_PER_SECTION;
  31. int nid = pgdat->node_id;
  32. int zone_type;
  33. zone_type = zone - pgdat->node_zones;
  34. if (!populated_zone(zone)) {
  35. int ret = 0;
  36. ret = init_currently_empty_zone(zone, phys_start_pfn, nr_pages);
  37. if (ret < 0)
  38. return ret;
  39. }
  40. memmap_init_zone(nr_pages, nid, zone_type, phys_start_pfn);
  41. zonetable_add(zone, nid, zone_type, phys_start_pfn, nr_pages);
  42. return 0;
  43. }
  44. extern int sparse_add_one_section(struct zone *zone, unsigned long start_pfn,
  45. int nr_pages);
  46. static int __add_section(struct zone *zone, unsigned long phys_start_pfn)
  47. {
  48. int nr_pages = PAGES_PER_SECTION;
  49. int ret;
  50. ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
  51. if (ret < 0)
  52. return ret;
  53. ret = __add_zone(zone, phys_start_pfn);
  54. if (ret < 0)
  55. return ret;
  56. return register_new_memory(__pfn_to_section(phys_start_pfn));
  57. }
  58. /*
  59. * Reasonably generic function for adding memory. It is
  60. * expected that archs that support memory hotplug will
  61. * call this function after deciding the zone to which to
  62. * add the new pages.
  63. */
  64. int __add_pages(struct zone *zone, unsigned long phys_start_pfn,
  65. unsigned long nr_pages)
  66. {
  67. unsigned long i;
  68. int err = 0;
  69. for (i = 0; i < nr_pages; i += PAGES_PER_SECTION) {
  70. err = __add_section(zone, phys_start_pfn + i);
  71. /* We want to keep adding the rest of the
  72. * sections if the first ones already exist
  73. */
  74. if (err && (err != -EEXIST))
  75. break;
  76. }
  77. return err;
  78. }
  79. EXPORT_SYMBOL_GPL(__add_pages);
  80. static void grow_zone_span(struct zone *zone,
  81. unsigned long start_pfn, unsigned long end_pfn)
  82. {
  83. unsigned long old_zone_end_pfn;
  84. zone_span_writelock(zone);
  85. old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
  86. if (start_pfn < zone->zone_start_pfn)
  87. zone->zone_start_pfn = start_pfn;
  88. zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
  89. zone->zone_start_pfn;
  90. zone_span_writeunlock(zone);
  91. }
  92. static void grow_pgdat_span(struct pglist_data *pgdat,
  93. unsigned long start_pfn, unsigned long end_pfn)
  94. {
  95. unsigned long old_pgdat_end_pfn =
  96. pgdat->node_start_pfn + pgdat->node_spanned_pages;
  97. if (start_pfn < pgdat->node_start_pfn)
  98. pgdat->node_start_pfn = start_pfn;
  99. pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
  100. pgdat->node_start_pfn;
  101. }
  102. int online_pages(unsigned long pfn, unsigned long nr_pages)
  103. {
  104. unsigned long i;
  105. unsigned long flags;
  106. unsigned long onlined_pages = 0;
  107. struct resource res;
  108. u64 section_end;
  109. unsigned long start_pfn;
  110. struct zone *zone;
  111. int need_zonelists_rebuild = 0;
  112. /*
  113. * This doesn't need a lock to do pfn_to_page().
  114. * The section can't be removed here because of the
  115. * memory_block->state_sem.
  116. */
  117. zone = page_zone(pfn_to_page(pfn));
  118. pgdat_resize_lock(zone->zone_pgdat, &flags);
  119. grow_zone_span(zone, pfn, pfn + nr_pages);
  120. grow_pgdat_span(zone->zone_pgdat, pfn, pfn + nr_pages);
  121. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  122. /*
  123. * If this zone is not populated, then it is not in zonelist.
  124. * This means the page allocator ignores this zone.
  125. * So, zonelist must be updated after online.
  126. */
  127. if (!populated_zone(zone))
  128. need_zonelists_rebuild = 1;
  129. res.start = (u64)pfn << PAGE_SHIFT;
  130. res.end = res.start + ((u64)nr_pages << PAGE_SHIFT) - 1;
  131. res.flags = IORESOURCE_MEM; /* we just need system ram */
  132. section_end = res.end;
  133. while (find_next_system_ram(&res) >= 0) {
  134. start_pfn = (unsigned long)(res.start >> PAGE_SHIFT);
  135. nr_pages = (unsigned long)
  136. ((res.end + 1 - res.start) >> PAGE_SHIFT);
  137. if (PageReserved(pfn_to_page(start_pfn))) {
  138. /* this region's page is not onlined now */
  139. for (i = 0; i < nr_pages; i++) {
  140. struct page *page = pfn_to_page(start_pfn + i);
  141. online_page(page);
  142. onlined_pages++;
  143. }
  144. }
  145. res.start = res.end + 1;
  146. res.end = section_end;
  147. }
  148. zone->present_pages += onlined_pages;
  149. zone->zone_pgdat->node_present_pages += onlined_pages;
  150. setup_per_zone_pages_min();
  151. if (need_zonelists_rebuild)
  152. build_all_zonelists();
  153. vm_total_pages = nr_free_pagecache_pages();
  154. return 0;
  155. }
  156. static pg_data_t *hotadd_new_pgdat(int nid, u64 start)
  157. {
  158. struct pglist_data *pgdat;
  159. unsigned long zones_size[MAX_NR_ZONES] = {0};
  160. unsigned long zholes_size[MAX_NR_ZONES] = {0};
  161. unsigned long start_pfn = start >> PAGE_SHIFT;
  162. pgdat = arch_alloc_nodedata(nid);
  163. if (!pgdat)
  164. return NULL;
  165. arch_refresh_nodedata(nid, pgdat);
  166. /* we can use NODE_DATA(nid) from here */
  167. /* init node's zones as empty zones, we don't have any present pages.*/
  168. free_area_init_node(nid, pgdat, zones_size, start_pfn, zholes_size);
  169. return pgdat;
  170. }
  171. static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
  172. {
  173. arch_refresh_nodedata(nid, NULL);
  174. arch_free_nodedata(pgdat);
  175. return;
  176. }
  177. /* add this memory to iomem resource */
  178. static void register_memory_resource(u64 start, u64 size)
  179. {
  180. struct resource *res;
  181. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  182. BUG_ON(!res);
  183. res->name = "System RAM";
  184. res->start = start;
  185. res->end = start + size - 1;
  186. res->flags = IORESOURCE_MEM;
  187. if (request_resource(&iomem_resource, res) < 0) {
  188. printk("System RAM resource %llx - %llx cannot be added\n",
  189. (unsigned long long)res->start, (unsigned long long)res->end);
  190. kfree(res);
  191. }
  192. }
  193. int add_memory(int nid, u64 start, u64 size)
  194. {
  195. pg_data_t *pgdat = NULL;
  196. int new_pgdat = 0;
  197. int ret;
  198. if (!node_online(nid)) {
  199. pgdat = hotadd_new_pgdat(nid, start);
  200. if (!pgdat)
  201. return -ENOMEM;
  202. new_pgdat = 1;
  203. ret = kswapd_run(nid);
  204. if (ret)
  205. goto error;
  206. }
  207. /* call arch's memory hotadd */
  208. ret = arch_add_memory(nid, start, size);
  209. if (ret < 0)
  210. goto error;
  211. /* we online node here. we have no error path from here. */
  212. node_set_online(nid);
  213. /* register this memory as resource */
  214. register_memory_resource(start, size);
  215. return ret;
  216. error:
  217. /* rollback pgdat allocation and others */
  218. if (new_pgdat)
  219. rollback_node_hotadd(nid, pgdat);
  220. return ret;
  221. }
  222. EXPORT_SYMBOL_GPL(add_memory);