memory_hotplug.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * linux/mm/memory_hotplug.c
  3. *
  4. * Copyright (C)
  5. */
  6. #include <linux/stddef.h>
  7. #include <linux/mm.h>
  8. #include <linux/swap.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/bootmem.h>
  12. #include <linux/compiler.h>
  13. #include <linux/module.h>
  14. #include <linux/pagevec.h>
  15. #include <linux/writeback.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 <linux/cpuset.h>
  25. #include <asm/tlbflush.h>
  26. /* add this memory to iomem resource */
  27. static struct resource *register_memory_resource(u64 start, u64 size)
  28. {
  29. struct resource *res;
  30. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  31. BUG_ON(!res);
  32. res->name = "System RAM";
  33. res->start = start;
  34. res->end = start + size - 1;
  35. res->flags = IORESOURCE_MEM;
  36. if (request_resource(&iomem_resource, res) < 0) {
  37. printk("System RAM resource %llx - %llx cannot be added\n",
  38. (unsigned long long)res->start, (unsigned long long)res->end);
  39. kfree(res);
  40. res = NULL;
  41. }
  42. return res;
  43. }
  44. static void release_memory_resource(struct resource *res)
  45. {
  46. if (!res)
  47. return;
  48. release_resource(res);
  49. kfree(res);
  50. return;
  51. }
  52. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  53. static int __add_zone(struct zone *zone, unsigned long phys_start_pfn)
  54. {
  55. struct pglist_data *pgdat = zone->zone_pgdat;
  56. int nr_pages = PAGES_PER_SECTION;
  57. int nid = pgdat->node_id;
  58. int zone_type;
  59. zone_type = zone - pgdat->node_zones;
  60. if (!populated_zone(zone)) {
  61. int ret = 0;
  62. ret = init_currently_empty_zone(zone, phys_start_pfn,
  63. nr_pages, MEMMAP_HOTPLUG);
  64. if (ret < 0)
  65. return ret;
  66. }
  67. memmap_init_zone(nr_pages, nid, zone_type,
  68. phys_start_pfn, MEMMAP_HOTPLUG);
  69. return 0;
  70. }
  71. static int __add_section(struct zone *zone, unsigned long phys_start_pfn)
  72. {
  73. int nr_pages = PAGES_PER_SECTION;
  74. int ret;
  75. if (pfn_valid(phys_start_pfn))
  76. return -EEXIST;
  77. ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
  78. if (ret < 0)
  79. return ret;
  80. ret = __add_zone(zone, phys_start_pfn);
  81. if (ret < 0)
  82. return ret;
  83. return register_new_memory(__pfn_to_section(phys_start_pfn));
  84. }
  85. /*
  86. * Reasonably generic function for adding memory. It is
  87. * expected that archs that support memory hotplug will
  88. * call this function after deciding the zone to which to
  89. * add the new pages.
  90. */
  91. int __add_pages(struct zone *zone, unsigned long phys_start_pfn,
  92. unsigned long nr_pages)
  93. {
  94. unsigned long i;
  95. int err = 0;
  96. int start_sec, end_sec;
  97. /* during initialize mem_map, align hot-added range to section */
  98. start_sec = pfn_to_section_nr(phys_start_pfn);
  99. end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
  100. for (i = start_sec; i <= end_sec; i++) {
  101. err = __add_section(zone, i << PFN_SECTION_SHIFT);
  102. /*
  103. * EEXIST is finally dealed with by ioresource collision
  104. * check. see add_memory() => register_memory_resource()
  105. * Warning will be printed if there is collision.
  106. */
  107. if (err && (err != -EEXIST))
  108. break;
  109. err = 0;
  110. }
  111. return err;
  112. }
  113. EXPORT_SYMBOL_GPL(__add_pages);
  114. static void grow_zone_span(struct zone *zone,
  115. unsigned long start_pfn, unsigned long end_pfn)
  116. {
  117. unsigned long old_zone_end_pfn;
  118. zone_span_writelock(zone);
  119. old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
  120. if (start_pfn < zone->zone_start_pfn)
  121. zone->zone_start_pfn = start_pfn;
  122. zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
  123. zone->zone_start_pfn;
  124. zone_span_writeunlock(zone);
  125. }
  126. static void grow_pgdat_span(struct pglist_data *pgdat,
  127. unsigned long start_pfn, unsigned long end_pfn)
  128. {
  129. unsigned long old_pgdat_end_pfn =
  130. pgdat->node_start_pfn + pgdat->node_spanned_pages;
  131. if (start_pfn < pgdat->node_start_pfn)
  132. pgdat->node_start_pfn = start_pfn;
  133. pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
  134. pgdat->node_start_pfn;
  135. }
  136. int online_pages(unsigned long pfn, unsigned long nr_pages)
  137. {
  138. unsigned long i;
  139. unsigned long flags;
  140. unsigned long onlined_pages = 0;
  141. struct resource res;
  142. u64 section_end;
  143. unsigned long start_pfn;
  144. struct zone *zone;
  145. int need_zonelists_rebuild = 0;
  146. /*
  147. * This doesn't need a lock to do pfn_to_page().
  148. * The section can't be removed here because of the
  149. * memory_block->state_sem.
  150. */
  151. zone = page_zone(pfn_to_page(pfn));
  152. pgdat_resize_lock(zone->zone_pgdat, &flags);
  153. grow_zone_span(zone, pfn, pfn + nr_pages);
  154. grow_pgdat_span(zone->zone_pgdat, pfn, pfn + nr_pages);
  155. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  156. /*
  157. * If this zone is not populated, then it is not in zonelist.
  158. * This means the page allocator ignores this zone.
  159. * So, zonelist must be updated after online.
  160. */
  161. if (!populated_zone(zone))
  162. need_zonelists_rebuild = 1;
  163. res.start = (u64)pfn << PAGE_SHIFT;
  164. res.end = res.start + ((u64)nr_pages << PAGE_SHIFT) - 1;
  165. res.flags = IORESOURCE_MEM; /* we just need system ram */
  166. section_end = res.end;
  167. while ((res.start < res.end) && (find_next_system_ram(&res) >= 0)) {
  168. start_pfn = (unsigned long)(res.start >> PAGE_SHIFT);
  169. nr_pages = (unsigned long)
  170. ((res.end + 1 - res.start) >> PAGE_SHIFT);
  171. if (PageReserved(pfn_to_page(start_pfn))) {
  172. /* this region's page is not onlined now */
  173. for (i = 0; i < nr_pages; i++) {
  174. struct page *page = pfn_to_page(start_pfn + i);
  175. online_page(page);
  176. onlined_pages++;
  177. }
  178. }
  179. res.start = res.end + 1;
  180. res.end = section_end;
  181. }
  182. zone->present_pages += onlined_pages;
  183. zone->zone_pgdat->node_present_pages += onlined_pages;
  184. setup_per_zone_pages_min();
  185. if (need_zonelists_rebuild)
  186. build_all_zonelists();
  187. vm_total_pages = nr_free_pagecache_pages();
  188. writeback_set_ratelimit();
  189. return 0;
  190. }
  191. #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
  192. static pg_data_t *hotadd_new_pgdat(int nid, u64 start)
  193. {
  194. struct pglist_data *pgdat;
  195. unsigned long zones_size[MAX_NR_ZONES] = {0};
  196. unsigned long zholes_size[MAX_NR_ZONES] = {0};
  197. unsigned long start_pfn = start >> PAGE_SHIFT;
  198. pgdat = arch_alloc_nodedata(nid);
  199. if (!pgdat)
  200. return NULL;
  201. arch_refresh_nodedata(nid, pgdat);
  202. /* we can use NODE_DATA(nid) from here */
  203. /* init node's zones as empty zones, we don't have any present pages.*/
  204. free_area_init_node(nid, pgdat, zones_size, start_pfn, zholes_size);
  205. return pgdat;
  206. }
  207. static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
  208. {
  209. arch_refresh_nodedata(nid, NULL);
  210. arch_free_nodedata(pgdat);
  211. return;
  212. }
  213. int add_memory(int nid, u64 start, u64 size)
  214. {
  215. pg_data_t *pgdat = NULL;
  216. int new_pgdat = 0;
  217. struct resource *res;
  218. int ret;
  219. res = register_memory_resource(start, size);
  220. if (!res)
  221. return -EEXIST;
  222. if (!node_online(nid)) {
  223. pgdat = hotadd_new_pgdat(nid, start);
  224. if (!pgdat)
  225. return -ENOMEM;
  226. new_pgdat = 1;
  227. ret = kswapd_run(nid);
  228. if (ret)
  229. goto error;
  230. }
  231. /* call arch's memory hotadd */
  232. ret = arch_add_memory(nid, start, size);
  233. if (ret < 0)
  234. goto error;
  235. /* we online node here. we can't roll back from here. */
  236. node_set_online(nid);
  237. cpuset_track_online_nodes();
  238. if (new_pgdat) {
  239. ret = register_one_node(nid);
  240. /*
  241. * If sysfs file of new node can't create, cpu on the node
  242. * can't be hot-added. There is no rollback way now.
  243. * So, check by BUG_ON() to catch it reluctantly..
  244. */
  245. BUG_ON(ret);
  246. }
  247. return ret;
  248. error:
  249. /* rollback pgdat allocation and others */
  250. if (new_pgdat)
  251. rollback_node_hotadd(nid, pgdat);
  252. if (res)
  253. release_memory_resource(res);
  254. return ret;
  255. }
  256. EXPORT_SYMBOL_GPL(add_memory);