memory_hotplug.c 7.4 KB

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