memory_hotplug.c 7.4 KB

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