memory_hotplug.c 7.2 KB

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