memory_hotplug.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120
  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/compiler.h>
  12. #include <linux/export.h>
  13. #include <linux/pagevec.h>
  14. #include <linux/writeback.h>
  15. #include <linux/slab.h>
  16. #include <linux/sysctl.h>
  17. #include <linux/cpu.h>
  18. #include <linux/memory.h>
  19. #include <linux/memremap.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/delay.h>
  25. #include <linux/migrate.h>
  26. #include <linux/page-isolation.h>
  27. #include <linux/pfn.h>
  28. #include <linux/suspend.h>
  29. #include <linux/mm_inline.h>
  30. #include <linux/firmware-map.h>
  31. #include <linux/stop_machine.h>
  32. #include <linux/hugetlb.h>
  33. #include <linux/memblock.h>
  34. #include <linux/bootmem.h>
  35. #include <linux/compaction.h>
  36. #include <asm/tlbflush.h>
  37. #include "internal.h"
  38. /*
  39. * online_page_callback contains pointer to current page onlining function.
  40. * Initially it is generic_online_page(). If it is required it could be
  41. * changed by calling set_online_page_callback() for callback registration
  42. * and restore_online_page_callback() for generic callback restore.
  43. */
  44. static void generic_online_page(struct page *page);
  45. static online_page_callback_t online_page_callback = generic_online_page;
  46. static DEFINE_MUTEX(online_page_callback_lock);
  47. /* The same as the cpu_hotplug lock, but for memory hotplug. */
  48. static struct {
  49. struct task_struct *active_writer;
  50. struct mutex lock; /* Synchronizes accesses to refcount, */
  51. /*
  52. * Also blocks the new readers during
  53. * an ongoing mem hotplug operation.
  54. */
  55. int refcount;
  56. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  57. struct lockdep_map dep_map;
  58. #endif
  59. } mem_hotplug = {
  60. .active_writer = NULL,
  61. .lock = __MUTEX_INITIALIZER(mem_hotplug.lock),
  62. .refcount = 0,
  63. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  64. .dep_map = {.name = "mem_hotplug.lock" },
  65. #endif
  66. };
  67. /* Lockdep annotations for get/put_online_mems() and mem_hotplug_begin/end() */
  68. #define memhp_lock_acquire_read() lock_map_acquire_read(&mem_hotplug.dep_map)
  69. #define memhp_lock_acquire() lock_map_acquire(&mem_hotplug.dep_map)
  70. #define memhp_lock_release() lock_map_release(&mem_hotplug.dep_map)
  71. bool memhp_auto_online;
  72. EXPORT_SYMBOL_GPL(memhp_auto_online);
  73. void get_online_mems(void)
  74. {
  75. might_sleep();
  76. if (mem_hotplug.active_writer == current)
  77. return;
  78. memhp_lock_acquire_read();
  79. mutex_lock(&mem_hotplug.lock);
  80. mem_hotplug.refcount++;
  81. mutex_unlock(&mem_hotplug.lock);
  82. }
  83. void put_online_mems(void)
  84. {
  85. if (mem_hotplug.active_writer == current)
  86. return;
  87. mutex_lock(&mem_hotplug.lock);
  88. if (WARN_ON(!mem_hotplug.refcount))
  89. mem_hotplug.refcount++; /* try to fix things up */
  90. if (!--mem_hotplug.refcount && unlikely(mem_hotplug.active_writer))
  91. wake_up_process(mem_hotplug.active_writer);
  92. mutex_unlock(&mem_hotplug.lock);
  93. memhp_lock_release();
  94. }
  95. void mem_hotplug_begin(void)
  96. {
  97. mem_hotplug.active_writer = current;
  98. memhp_lock_acquire();
  99. for (;;) {
  100. mutex_lock(&mem_hotplug.lock);
  101. if (likely(!mem_hotplug.refcount))
  102. break;
  103. __set_current_state(TASK_UNINTERRUPTIBLE);
  104. mutex_unlock(&mem_hotplug.lock);
  105. schedule();
  106. }
  107. }
  108. void mem_hotplug_done(void)
  109. {
  110. mem_hotplug.active_writer = NULL;
  111. mutex_unlock(&mem_hotplug.lock);
  112. memhp_lock_release();
  113. }
  114. /* add this memory to iomem resource */
  115. static struct resource *register_memory_resource(u64 start, u64 size)
  116. {
  117. struct resource *res;
  118. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  119. if (!res)
  120. return ERR_PTR(-ENOMEM);
  121. res->name = "System RAM";
  122. res->start = start;
  123. res->end = start + size - 1;
  124. res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  125. if (request_resource(&iomem_resource, res) < 0) {
  126. pr_debug("System RAM resource %pR cannot be added\n", res);
  127. kfree(res);
  128. return ERR_PTR(-EEXIST);
  129. }
  130. return res;
  131. }
  132. static void release_memory_resource(struct resource *res)
  133. {
  134. if (!res)
  135. return;
  136. release_resource(res);
  137. kfree(res);
  138. return;
  139. }
  140. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  141. void get_page_bootmem(unsigned long info, struct page *page,
  142. unsigned long type)
  143. {
  144. page->lru.next = (struct list_head *) type;
  145. SetPagePrivate(page);
  146. set_page_private(page, info);
  147. page_ref_inc(page);
  148. }
  149. void put_page_bootmem(struct page *page)
  150. {
  151. unsigned long type;
  152. type = (unsigned long) page->lru.next;
  153. BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
  154. type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
  155. if (page_ref_dec_return(page) == 1) {
  156. ClearPagePrivate(page);
  157. set_page_private(page, 0);
  158. INIT_LIST_HEAD(&page->lru);
  159. free_reserved_page(page);
  160. }
  161. }
  162. #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
  163. #ifndef CONFIG_SPARSEMEM_VMEMMAP
  164. static void register_page_bootmem_info_section(unsigned long start_pfn)
  165. {
  166. unsigned long *usemap, mapsize, section_nr, i;
  167. struct mem_section *ms;
  168. struct page *page, *memmap;
  169. section_nr = pfn_to_section_nr(start_pfn);
  170. ms = __nr_to_section(section_nr);
  171. /* Get section's memmap address */
  172. memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
  173. /*
  174. * Get page for the memmap's phys address
  175. * XXX: need more consideration for sparse_vmemmap...
  176. */
  177. page = virt_to_page(memmap);
  178. mapsize = sizeof(struct page) * PAGES_PER_SECTION;
  179. mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
  180. /* remember memmap's page */
  181. for (i = 0; i < mapsize; i++, page++)
  182. get_page_bootmem(section_nr, page, SECTION_INFO);
  183. usemap = __nr_to_section(section_nr)->pageblock_flags;
  184. page = virt_to_page(usemap);
  185. mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
  186. for (i = 0; i < mapsize; i++, page++)
  187. get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
  188. }
  189. #else /* CONFIG_SPARSEMEM_VMEMMAP */
  190. static void register_page_bootmem_info_section(unsigned long start_pfn)
  191. {
  192. unsigned long *usemap, mapsize, section_nr, i;
  193. struct mem_section *ms;
  194. struct page *page, *memmap;
  195. if (!pfn_valid(start_pfn))
  196. return;
  197. section_nr = pfn_to_section_nr(start_pfn);
  198. ms = __nr_to_section(section_nr);
  199. memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
  200. register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
  201. usemap = __nr_to_section(section_nr)->pageblock_flags;
  202. page = virt_to_page(usemap);
  203. mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
  204. for (i = 0; i < mapsize; i++, page++)
  205. get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
  206. }
  207. #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
  208. void register_page_bootmem_info_node(struct pglist_data *pgdat)
  209. {
  210. unsigned long i, pfn, end_pfn, nr_pages;
  211. int node = pgdat->node_id;
  212. struct page *page;
  213. struct zone *zone;
  214. nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
  215. page = virt_to_page(pgdat);
  216. for (i = 0; i < nr_pages; i++, page++)
  217. get_page_bootmem(node, page, NODE_INFO);
  218. zone = &pgdat->node_zones[0];
  219. for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
  220. if (zone_is_initialized(zone)) {
  221. nr_pages = zone->wait_table_hash_nr_entries
  222. * sizeof(wait_queue_head_t);
  223. nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
  224. page = virt_to_page(zone->wait_table);
  225. for (i = 0; i < nr_pages; i++, page++)
  226. get_page_bootmem(node, page, NODE_INFO);
  227. }
  228. }
  229. pfn = pgdat->node_start_pfn;
  230. end_pfn = pgdat_end_pfn(pgdat);
  231. /* register section info */
  232. for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  233. /*
  234. * Some platforms can assign the same pfn to multiple nodes - on
  235. * node0 as well as nodeN. To avoid registering a pfn against
  236. * multiple nodes we check that this pfn does not already
  237. * reside in some other nodes.
  238. */
  239. if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
  240. register_page_bootmem_info_section(pfn);
  241. }
  242. }
  243. #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
  244. static void __meminit grow_zone_span(struct zone *zone, unsigned long start_pfn,
  245. unsigned long end_pfn)
  246. {
  247. unsigned long old_zone_end_pfn;
  248. zone_span_writelock(zone);
  249. old_zone_end_pfn = zone_end_pfn(zone);
  250. if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
  251. zone->zone_start_pfn = start_pfn;
  252. zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
  253. zone->zone_start_pfn;
  254. zone_span_writeunlock(zone);
  255. }
  256. static void resize_zone(struct zone *zone, unsigned long start_pfn,
  257. unsigned long end_pfn)
  258. {
  259. zone_span_writelock(zone);
  260. if (end_pfn - start_pfn) {
  261. zone->zone_start_pfn = start_pfn;
  262. zone->spanned_pages = end_pfn - start_pfn;
  263. } else {
  264. /*
  265. * make it consist as free_area_init_core(),
  266. * if spanned_pages = 0, then keep start_pfn = 0
  267. */
  268. zone->zone_start_pfn = 0;
  269. zone->spanned_pages = 0;
  270. }
  271. zone_span_writeunlock(zone);
  272. }
  273. static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
  274. unsigned long end_pfn)
  275. {
  276. enum zone_type zid = zone_idx(zone);
  277. int nid = zone->zone_pgdat->node_id;
  278. unsigned long pfn;
  279. for (pfn = start_pfn; pfn < end_pfn; pfn++)
  280. set_page_links(pfn_to_page(pfn), zid, nid, pfn);
  281. }
  282. /* Can fail with -ENOMEM from allocating a wait table with vmalloc() or
  283. * alloc_bootmem_node_nopanic()/memblock_virt_alloc_node_nopanic() */
  284. static int __ref ensure_zone_is_initialized(struct zone *zone,
  285. unsigned long start_pfn, unsigned long num_pages)
  286. {
  287. if (!zone_is_initialized(zone))
  288. return init_currently_empty_zone(zone, start_pfn, num_pages);
  289. return 0;
  290. }
  291. static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
  292. unsigned long start_pfn, unsigned long end_pfn)
  293. {
  294. int ret;
  295. unsigned long flags;
  296. unsigned long z1_start_pfn;
  297. ret = ensure_zone_is_initialized(z1, start_pfn, end_pfn - start_pfn);
  298. if (ret)
  299. return ret;
  300. pgdat_resize_lock(z1->zone_pgdat, &flags);
  301. /* can't move pfns which are higher than @z2 */
  302. if (end_pfn > zone_end_pfn(z2))
  303. goto out_fail;
  304. /* the move out part must be at the left most of @z2 */
  305. if (start_pfn > z2->zone_start_pfn)
  306. goto out_fail;
  307. /* must included/overlap */
  308. if (end_pfn <= z2->zone_start_pfn)
  309. goto out_fail;
  310. /* use start_pfn for z1's start_pfn if z1 is empty */
  311. if (!zone_is_empty(z1))
  312. z1_start_pfn = z1->zone_start_pfn;
  313. else
  314. z1_start_pfn = start_pfn;
  315. resize_zone(z1, z1_start_pfn, end_pfn);
  316. resize_zone(z2, end_pfn, zone_end_pfn(z2));
  317. pgdat_resize_unlock(z1->zone_pgdat, &flags);
  318. fix_zone_id(z1, start_pfn, end_pfn);
  319. return 0;
  320. out_fail:
  321. pgdat_resize_unlock(z1->zone_pgdat, &flags);
  322. return -1;
  323. }
  324. static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
  325. unsigned long start_pfn, unsigned long end_pfn)
  326. {
  327. int ret;
  328. unsigned long flags;
  329. unsigned long z2_end_pfn;
  330. ret = ensure_zone_is_initialized(z2, start_pfn, end_pfn - start_pfn);
  331. if (ret)
  332. return ret;
  333. pgdat_resize_lock(z1->zone_pgdat, &flags);
  334. /* can't move pfns which are lower than @z1 */
  335. if (z1->zone_start_pfn > start_pfn)
  336. goto out_fail;
  337. /* the move out part mast at the right most of @z1 */
  338. if (zone_end_pfn(z1) > end_pfn)
  339. goto out_fail;
  340. /* must included/overlap */
  341. if (start_pfn >= zone_end_pfn(z1))
  342. goto out_fail;
  343. /* use end_pfn for z2's end_pfn if z2 is empty */
  344. if (!zone_is_empty(z2))
  345. z2_end_pfn = zone_end_pfn(z2);
  346. else
  347. z2_end_pfn = end_pfn;
  348. resize_zone(z1, z1->zone_start_pfn, start_pfn);
  349. resize_zone(z2, start_pfn, z2_end_pfn);
  350. pgdat_resize_unlock(z1->zone_pgdat, &flags);
  351. fix_zone_id(z2, start_pfn, end_pfn);
  352. return 0;
  353. out_fail:
  354. pgdat_resize_unlock(z1->zone_pgdat, &flags);
  355. return -1;
  356. }
  357. static void __meminit grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
  358. unsigned long end_pfn)
  359. {
  360. unsigned long old_pgdat_end_pfn = pgdat_end_pfn(pgdat);
  361. if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
  362. pgdat->node_start_pfn = start_pfn;
  363. pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
  364. pgdat->node_start_pfn;
  365. }
  366. static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
  367. {
  368. struct pglist_data *pgdat = zone->zone_pgdat;
  369. int nr_pages = PAGES_PER_SECTION;
  370. int nid = pgdat->node_id;
  371. int zone_type;
  372. unsigned long flags, pfn;
  373. int ret;
  374. zone_type = zone - pgdat->node_zones;
  375. ret = ensure_zone_is_initialized(zone, phys_start_pfn, nr_pages);
  376. if (ret)
  377. return ret;
  378. pgdat_resize_lock(zone->zone_pgdat, &flags);
  379. grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
  380. grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
  381. phys_start_pfn + nr_pages);
  382. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  383. memmap_init_zone(nr_pages, nid, zone_type,
  384. phys_start_pfn, MEMMAP_HOTPLUG);
  385. /* online_page_range is called later and expects pages reserved */
  386. for (pfn = phys_start_pfn; pfn < phys_start_pfn + nr_pages; pfn++) {
  387. if (!pfn_valid(pfn))
  388. continue;
  389. SetPageReserved(pfn_to_page(pfn));
  390. }
  391. return 0;
  392. }
  393. static int __meminit __add_section(int nid, struct zone *zone,
  394. unsigned long phys_start_pfn)
  395. {
  396. int ret;
  397. if (pfn_valid(phys_start_pfn))
  398. return -EEXIST;
  399. ret = sparse_add_one_section(zone, phys_start_pfn);
  400. if (ret < 0)
  401. return ret;
  402. ret = __add_zone(zone, phys_start_pfn);
  403. if (ret < 0)
  404. return ret;
  405. return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
  406. }
  407. /*
  408. * Reasonably generic function for adding memory. It is
  409. * expected that archs that support memory hotplug will
  410. * call this function after deciding the zone to which to
  411. * add the new pages.
  412. */
  413. int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
  414. unsigned long nr_pages)
  415. {
  416. unsigned long i;
  417. int err = 0;
  418. int start_sec, end_sec;
  419. struct vmem_altmap *altmap;
  420. clear_zone_contiguous(zone);
  421. /* during initialize mem_map, align hot-added range to section */
  422. start_sec = pfn_to_section_nr(phys_start_pfn);
  423. end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
  424. altmap = to_vmem_altmap((unsigned long) pfn_to_page(phys_start_pfn));
  425. if (altmap) {
  426. /*
  427. * Validate altmap is within bounds of the total request
  428. */
  429. if (altmap->base_pfn != phys_start_pfn
  430. || vmem_altmap_offset(altmap) > nr_pages) {
  431. pr_warn_once("memory add fail, invalid altmap\n");
  432. err = -EINVAL;
  433. goto out;
  434. }
  435. altmap->alloc = 0;
  436. }
  437. for (i = start_sec; i <= end_sec; i++) {
  438. err = __add_section(nid, zone, section_nr_to_pfn(i));
  439. /*
  440. * EEXIST is finally dealt with by ioresource collision
  441. * check. see add_memory() => register_memory_resource()
  442. * Warning will be printed if there is collision.
  443. */
  444. if (err && (err != -EEXIST))
  445. break;
  446. err = 0;
  447. }
  448. vmemmap_populate_print_last();
  449. out:
  450. set_zone_contiguous(zone);
  451. return err;
  452. }
  453. EXPORT_SYMBOL_GPL(__add_pages);
  454. #ifdef CONFIG_MEMORY_HOTREMOVE
  455. /* find the smallest valid pfn in the range [start_pfn, end_pfn) */
  456. static int find_smallest_section_pfn(int nid, struct zone *zone,
  457. unsigned long start_pfn,
  458. unsigned long end_pfn)
  459. {
  460. struct mem_section *ms;
  461. for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
  462. ms = __pfn_to_section(start_pfn);
  463. if (unlikely(!valid_section(ms)))
  464. continue;
  465. if (unlikely(pfn_to_nid(start_pfn) != nid))
  466. continue;
  467. if (zone && zone != page_zone(pfn_to_page(start_pfn)))
  468. continue;
  469. return start_pfn;
  470. }
  471. return 0;
  472. }
  473. /* find the biggest valid pfn in the range [start_pfn, end_pfn). */
  474. static int find_biggest_section_pfn(int nid, struct zone *zone,
  475. unsigned long start_pfn,
  476. unsigned long end_pfn)
  477. {
  478. struct mem_section *ms;
  479. unsigned long pfn;
  480. /* pfn is the end pfn of a memory section. */
  481. pfn = end_pfn - 1;
  482. for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
  483. ms = __pfn_to_section(pfn);
  484. if (unlikely(!valid_section(ms)))
  485. continue;
  486. if (unlikely(pfn_to_nid(pfn) != nid))
  487. continue;
  488. if (zone && zone != page_zone(pfn_to_page(pfn)))
  489. continue;
  490. return pfn;
  491. }
  492. return 0;
  493. }
  494. static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
  495. unsigned long end_pfn)
  496. {
  497. unsigned long zone_start_pfn = zone->zone_start_pfn;
  498. unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
  499. unsigned long zone_end_pfn = z;
  500. unsigned long pfn;
  501. struct mem_section *ms;
  502. int nid = zone_to_nid(zone);
  503. zone_span_writelock(zone);
  504. if (zone_start_pfn == start_pfn) {
  505. /*
  506. * If the section is smallest section in the zone, it need
  507. * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
  508. * In this case, we find second smallest valid mem_section
  509. * for shrinking zone.
  510. */
  511. pfn = find_smallest_section_pfn(nid, zone, end_pfn,
  512. zone_end_pfn);
  513. if (pfn) {
  514. zone->zone_start_pfn = pfn;
  515. zone->spanned_pages = zone_end_pfn - pfn;
  516. }
  517. } else if (zone_end_pfn == end_pfn) {
  518. /*
  519. * If the section is biggest section in the zone, it need
  520. * shrink zone->spanned_pages.
  521. * In this case, we find second biggest valid mem_section for
  522. * shrinking zone.
  523. */
  524. pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
  525. start_pfn);
  526. if (pfn)
  527. zone->spanned_pages = pfn - zone_start_pfn + 1;
  528. }
  529. /*
  530. * The section is not biggest or smallest mem_section in the zone, it
  531. * only creates a hole in the zone. So in this case, we need not
  532. * change the zone. But perhaps, the zone has only hole data. Thus
  533. * it check the zone has only hole or not.
  534. */
  535. pfn = zone_start_pfn;
  536. for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
  537. ms = __pfn_to_section(pfn);
  538. if (unlikely(!valid_section(ms)))
  539. continue;
  540. if (page_zone(pfn_to_page(pfn)) != zone)
  541. continue;
  542. /* If the section is current section, it continues the loop */
  543. if (start_pfn == pfn)
  544. continue;
  545. /* If we find valid section, we have nothing to do */
  546. zone_span_writeunlock(zone);
  547. return;
  548. }
  549. /* The zone has no valid section */
  550. zone->zone_start_pfn = 0;
  551. zone->spanned_pages = 0;
  552. zone_span_writeunlock(zone);
  553. }
  554. static void shrink_pgdat_span(struct pglist_data *pgdat,
  555. unsigned long start_pfn, unsigned long end_pfn)
  556. {
  557. unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
  558. unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */
  559. unsigned long pgdat_end_pfn = p;
  560. unsigned long pfn;
  561. struct mem_section *ms;
  562. int nid = pgdat->node_id;
  563. if (pgdat_start_pfn == start_pfn) {
  564. /*
  565. * If the section is smallest section in the pgdat, it need
  566. * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
  567. * In this case, we find second smallest valid mem_section
  568. * for shrinking zone.
  569. */
  570. pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
  571. pgdat_end_pfn);
  572. if (pfn) {
  573. pgdat->node_start_pfn = pfn;
  574. pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
  575. }
  576. } else if (pgdat_end_pfn == end_pfn) {
  577. /*
  578. * If the section is biggest section in the pgdat, it need
  579. * shrink pgdat->node_spanned_pages.
  580. * In this case, we find second biggest valid mem_section for
  581. * shrinking zone.
  582. */
  583. pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
  584. start_pfn);
  585. if (pfn)
  586. pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
  587. }
  588. /*
  589. * If the section is not biggest or smallest mem_section in the pgdat,
  590. * it only creates a hole in the pgdat. So in this case, we need not
  591. * change the pgdat.
  592. * But perhaps, the pgdat has only hole data. Thus it check the pgdat
  593. * has only hole or not.
  594. */
  595. pfn = pgdat_start_pfn;
  596. for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
  597. ms = __pfn_to_section(pfn);
  598. if (unlikely(!valid_section(ms)))
  599. continue;
  600. if (pfn_to_nid(pfn) != nid)
  601. continue;
  602. /* If the section is current section, it continues the loop */
  603. if (start_pfn == pfn)
  604. continue;
  605. /* If we find valid section, we have nothing to do */
  606. return;
  607. }
  608. /* The pgdat has no valid section */
  609. pgdat->node_start_pfn = 0;
  610. pgdat->node_spanned_pages = 0;
  611. }
  612. static void __remove_zone(struct zone *zone, unsigned long start_pfn)
  613. {
  614. struct pglist_data *pgdat = zone->zone_pgdat;
  615. int nr_pages = PAGES_PER_SECTION;
  616. int zone_type;
  617. unsigned long flags;
  618. zone_type = zone - pgdat->node_zones;
  619. pgdat_resize_lock(zone->zone_pgdat, &flags);
  620. shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
  621. shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
  622. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  623. }
  624. static int __remove_section(struct zone *zone, struct mem_section *ms,
  625. unsigned long map_offset)
  626. {
  627. unsigned long start_pfn;
  628. int scn_nr;
  629. int ret = -EINVAL;
  630. if (!valid_section(ms))
  631. return ret;
  632. ret = unregister_memory_section(ms);
  633. if (ret)
  634. return ret;
  635. scn_nr = __section_nr(ms);
  636. start_pfn = section_nr_to_pfn(scn_nr);
  637. __remove_zone(zone, start_pfn);
  638. sparse_remove_one_section(zone, ms, map_offset);
  639. return 0;
  640. }
  641. /**
  642. * __remove_pages() - remove sections of pages from a zone
  643. * @zone: zone from which pages need to be removed
  644. * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
  645. * @nr_pages: number of pages to remove (must be multiple of section size)
  646. *
  647. * Generic helper function to remove section mappings and sysfs entries
  648. * for the section of the memory we are removing. Caller needs to make
  649. * sure that pages are marked reserved and zones are adjust properly by
  650. * calling offline_pages().
  651. */
  652. int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
  653. unsigned long nr_pages)
  654. {
  655. unsigned long i;
  656. unsigned long map_offset = 0;
  657. int sections_to_remove, ret = 0;
  658. /* In the ZONE_DEVICE case device driver owns the memory region */
  659. if (is_dev_zone(zone)) {
  660. struct page *page = pfn_to_page(phys_start_pfn);
  661. struct vmem_altmap *altmap;
  662. altmap = to_vmem_altmap((unsigned long) page);
  663. if (altmap)
  664. map_offset = vmem_altmap_offset(altmap);
  665. } else {
  666. resource_size_t start, size;
  667. start = phys_start_pfn << PAGE_SHIFT;
  668. size = nr_pages * PAGE_SIZE;
  669. ret = release_mem_region_adjustable(&iomem_resource, start,
  670. size);
  671. if (ret) {
  672. resource_size_t endres = start + size - 1;
  673. pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
  674. &start, &endres, ret);
  675. }
  676. }
  677. clear_zone_contiguous(zone);
  678. /*
  679. * We can only remove entire sections
  680. */
  681. BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
  682. BUG_ON(nr_pages % PAGES_PER_SECTION);
  683. sections_to_remove = nr_pages / PAGES_PER_SECTION;
  684. for (i = 0; i < sections_to_remove; i++) {
  685. unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
  686. ret = __remove_section(zone, __pfn_to_section(pfn), map_offset);
  687. map_offset = 0;
  688. if (ret)
  689. break;
  690. }
  691. set_zone_contiguous(zone);
  692. return ret;
  693. }
  694. EXPORT_SYMBOL_GPL(__remove_pages);
  695. #endif /* CONFIG_MEMORY_HOTREMOVE */
  696. int set_online_page_callback(online_page_callback_t callback)
  697. {
  698. int rc = -EINVAL;
  699. get_online_mems();
  700. mutex_lock(&online_page_callback_lock);
  701. if (online_page_callback == generic_online_page) {
  702. online_page_callback = callback;
  703. rc = 0;
  704. }
  705. mutex_unlock(&online_page_callback_lock);
  706. put_online_mems();
  707. return rc;
  708. }
  709. EXPORT_SYMBOL_GPL(set_online_page_callback);
  710. int restore_online_page_callback(online_page_callback_t callback)
  711. {
  712. int rc = -EINVAL;
  713. get_online_mems();
  714. mutex_lock(&online_page_callback_lock);
  715. if (online_page_callback == callback) {
  716. online_page_callback = generic_online_page;
  717. rc = 0;
  718. }
  719. mutex_unlock(&online_page_callback_lock);
  720. put_online_mems();
  721. return rc;
  722. }
  723. EXPORT_SYMBOL_GPL(restore_online_page_callback);
  724. void __online_page_set_limits(struct page *page)
  725. {
  726. }
  727. EXPORT_SYMBOL_GPL(__online_page_set_limits);
  728. void __online_page_increment_counters(struct page *page)
  729. {
  730. adjust_managed_page_count(page, 1);
  731. }
  732. EXPORT_SYMBOL_GPL(__online_page_increment_counters);
  733. void __online_page_free(struct page *page)
  734. {
  735. __free_reserved_page(page);
  736. }
  737. EXPORT_SYMBOL_GPL(__online_page_free);
  738. static void generic_online_page(struct page *page)
  739. {
  740. __online_page_set_limits(page);
  741. __online_page_increment_counters(page);
  742. __online_page_free(page);
  743. }
  744. static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
  745. void *arg)
  746. {
  747. unsigned long i;
  748. unsigned long onlined_pages = *(unsigned long *)arg;
  749. struct page *page;
  750. if (PageReserved(pfn_to_page(start_pfn)))
  751. for (i = 0; i < nr_pages; i++) {
  752. page = pfn_to_page(start_pfn + i);
  753. (*online_page_callback)(page);
  754. onlined_pages++;
  755. }
  756. *(unsigned long *)arg = onlined_pages;
  757. return 0;
  758. }
  759. #ifdef CONFIG_MOVABLE_NODE
  760. /*
  761. * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
  762. * normal memory.
  763. */
  764. static bool can_online_high_movable(struct zone *zone)
  765. {
  766. return true;
  767. }
  768. #else /* CONFIG_MOVABLE_NODE */
  769. /* ensure every online node has NORMAL memory */
  770. static bool can_online_high_movable(struct zone *zone)
  771. {
  772. return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
  773. }
  774. #endif /* CONFIG_MOVABLE_NODE */
  775. /* check which state of node_states will be changed when online memory */
  776. static void node_states_check_changes_online(unsigned long nr_pages,
  777. struct zone *zone, struct memory_notify *arg)
  778. {
  779. int nid = zone_to_nid(zone);
  780. enum zone_type zone_last = ZONE_NORMAL;
  781. /*
  782. * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
  783. * contains nodes which have zones of 0...ZONE_NORMAL,
  784. * set zone_last to ZONE_NORMAL.
  785. *
  786. * If we don't have HIGHMEM nor movable node,
  787. * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
  788. * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
  789. */
  790. if (N_MEMORY == N_NORMAL_MEMORY)
  791. zone_last = ZONE_MOVABLE;
  792. /*
  793. * if the memory to be online is in a zone of 0...zone_last, and
  794. * the zones of 0...zone_last don't have memory before online, we will
  795. * need to set the node to node_states[N_NORMAL_MEMORY] after
  796. * the memory is online.
  797. */
  798. if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
  799. arg->status_change_nid_normal = nid;
  800. else
  801. arg->status_change_nid_normal = -1;
  802. #ifdef CONFIG_HIGHMEM
  803. /*
  804. * If we have movable node, node_states[N_HIGH_MEMORY]
  805. * contains nodes which have zones of 0...ZONE_HIGHMEM,
  806. * set zone_last to ZONE_HIGHMEM.
  807. *
  808. * If we don't have movable node, node_states[N_NORMAL_MEMORY]
  809. * contains nodes which have zones of 0...ZONE_MOVABLE,
  810. * set zone_last to ZONE_MOVABLE.
  811. */
  812. zone_last = ZONE_HIGHMEM;
  813. if (N_MEMORY == N_HIGH_MEMORY)
  814. zone_last = ZONE_MOVABLE;
  815. if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
  816. arg->status_change_nid_high = nid;
  817. else
  818. arg->status_change_nid_high = -1;
  819. #else
  820. arg->status_change_nid_high = arg->status_change_nid_normal;
  821. #endif
  822. /*
  823. * if the node don't have memory befor online, we will need to
  824. * set the node to node_states[N_MEMORY] after the memory
  825. * is online.
  826. */
  827. if (!node_state(nid, N_MEMORY))
  828. arg->status_change_nid = nid;
  829. else
  830. arg->status_change_nid = -1;
  831. }
  832. static void node_states_set_node(int node, struct memory_notify *arg)
  833. {
  834. if (arg->status_change_nid_normal >= 0)
  835. node_set_state(node, N_NORMAL_MEMORY);
  836. if (arg->status_change_nid_high >= 0)
  837. node_set_state(node, N_HIGH_MEMORY);
  838. node_set_state(node, N_MEMORY);
  839. }
  840. /* Must be protected by mem_hotplug_begin() */
  841. int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
  842. {
  843. unsigned long flags;
  844. unsigned long onlined_pages = 0;
  845. struct zone *zone;
  846. int need_zonelists_rebuild = 0;
  847. int nid;
  848. int ret;
  849. struct memory_notify arg;
  850. /*
  851. * This doesn't need a lock to do pfn_to_page().
  852. * The section can't be removed here because of the
  853. * memory_block->state_mutex.
  854. */
  855. zone = page_zone(pfn_to_page(pfn));
  856. if ((zone_idx(zone) > ZONE_NORMAL ||
  857. online_type == MMOP_ONLINE_MOVABLE) &&
  858. !can_online_high_movable(zone))
  859. return -EINVAL;
  860. if (online_type == MMOP_ONLINE_KERNEL &&
  861. zone_idx(zone) == ZONE_MOVABLE) {
  862. if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages))
  863. return -EINVAL;
  864. }
  865. if (online_type == MMOP_ONLINE_MOVABLE &&
  866. zone_idx(zone) == ZONE_MOVABLE - 1) {
  867. if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages))
  868. return -EINVAL;
  869. }
  870. /* Previous code may changed the zone of the pfn range */
  871. zone = page_zone(pfn_to_page(pfn));
  872. arg.start_pfn = pfn;
  873. arg.nr_pages = nr_pages;
  874. node_states_check_changes_online(nr_pages, zone, &arg);
  875. nid = zone_to_nid(zone);
  876. ret = memory_notify(MEM_GOING_ONLINE, &arg);
  877. ret = notifier_to_errno(ret);
  878. if (ret) {
  879. memory_notify(MEM_CANCEL_ONLINE, &arg);
  880. return ret;
  881. }
  882. /*
  883. * If this zone is not populated, then it is not in zonelist.
  884. * This means the page allocator ignores this zone.
  885. * So, zonelist must be updated after online.
  886. */
  887. mutex_lock(&zonelists_mutex);
  888. if (!populated_zone(zone)) {
  889. need_zonelists_rebuild = 1;
  890. build_all_zonelists(NULL, zone);
  891. }
  892. ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
  893. online_pages_range);
  894. if (ret) {
  895. if (need_zonelists_rebuild)
  896. zone_pcp_reset(zone);
  897. mutex_unlock(&zonelists_mutex);
  898. printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
  899. (unsigned long long) pfn << PAGE_SHIFT,
  900. (((unsigned long long) pfn + nr_pages)
  901. << PAGE_SHIFT) - 1);
  902. memory_notify(MEM_CANCEL_ONLINE, &arg);
  903. return ret;
  904. }
  905. zone->present_pages += onlined_pages;
  906. pgdat_resize_lock(zone->zone_pgdat, &flags);
  907. zone->zone_pgdat->node_present_pages += onlined_pages;
  908. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  909. if (onlined_pages) {
  910. node_states_set_node(nid, &arg);
  911. if (need_zonelists_rebuild)
  912. build_all_zonelists(NULL, NULL);
  913. else
  914. zone_pcp_update(zone);
  915. }
  916. mutex_unlock(&zonelists_mutex);
  917. init_per_zone_wmark_min();
  918. if (onlined_pages) {
  919. kswapd_run(nid);
  920. kcompactd_run(nid);
  921. }
  922. vm_total_pages = nr_free_pagecache_pages();
  923. writeback_set_ratelimit();
  924. if (onlined_pages)
  925. memory_notify(MEM_ONLINE, &arg);
  926. return 0;
  927. }
  928. #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
  929. static void reset_node_present_pages(pg_data_t *pgdat)
  930. {
  931. struct zone *z;
  932. for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
  933. z->present_pages = 0;
  934. pgdat->node_present_pages = 0;
  935. }
  936. /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
  937. static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
  938. {
  939. struct pglist_data *pgdat;
  940. unsigned long zones_size[MAX_NR_ZONES] = {0};
  941. unsigned long zholes_size[MAX_NR_ZONES] = {0};
  942. unsigned long start_pfn = PFN_DOWN(start);
  943. pgdat = NODE_DATA(nid);
  944. if (!pgdat) {
  945. pgdat = arch_alloc_nodedata(nid);
  946. if (!pgdat)
  947. return NULL;
  948. arch_refresh_nodedata(nid, pgdat);
  949. } else {
  950. /* Reset the nr_zones and classzone_idx to 0 before reuse */
  951. pgdat->nr_zones = 0;
  952. pgdat->classzone_idx = 0;
  953. }
  954. /* we can use NODE_DATA(nid) from here */
  955. /* init node's zones as empty zones, we don't have any present pages.*/
  956. free_area_init_node(nid, zones_size, start_pfn, zholes_size);
  957. /*
  958. * The node we allocated has no zone fallback lists. For avoiding
  959. * to access not-initialized zonelist, build here.
  960. */
  961. mutex_lock(&zonelists_mutex);
  962. build_all_zonelists(pgdat, NULL);
  963. mutex_unlock(&zonelists_mutex);
  964. /*
  965. * zone->managed_pages is set to an approximate value in
  966. * free_area_init_core(), which will cause
  967. * /sys/device/system/node/nodeX/meminfo has wrong data.
  968. * So reset it to 0 before any memory is onlined.
  969. */
  970. reset_node_managed_pages(pgdat);
  971. /*
  972. * When memory is hot-added, all the memory is in offline state. So
  973. * clear all zones' present_pages because they will be updated in
  974. * online_pages() and offline_pages().
  975. */
  976. reset_node_present_pages(pgdat);
  977. return pgdat;
  978. }
  979. static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
  980. {
  981. arch_refresh_nodedata(nid, NULL);
  982. arch_free_nodedata(pgdat);
  983. return;
  984. }
  985. /**
  986. * try_online_node - online a node if offlined
  987. *
  988. * called by cpu_up() to online a node without onlined memory.
  989. */
  990. int try_online_node(int nid)
  991. {
  992. pg_data_t *pgdat;
  993. int ret;
  994. if (node_online(nid))
  995. return 0;
  996. mem_hotplug_begin();
  997. pgdat = hotadd_new_pgdat(nid, 0);
  998. if (!pgdat) {
  999. pr_err("Cannot online node %d due to NULL pgdat\n", nid);
  1000. ret = -ENOMEM;
  1001. goto out;
  1002. }
  1003. node_set_online(nid);
  1004. ret = register_one_node(nid);
  1005. BUG_ON(ret);
  1006. if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
  1007. mutex_lock(&zonelists_mutex);
  1008. build_all_zonelists(NULL, NULL);
  1009. mutex_unlock(&zonelists_mutex);
  1010. }
  1011. out:
  1012. mem_hotplug_done();
  1013. return ret;
  1014. }
  1015. static int check_hotplug_memory_range(u64 start, u64 size)
  1016. {
  1017. u64 start_pfn = PFN_DOWN(start);
  1018. u64 nr_pages = size >> PAGE_SHIFT;
  1019. /* Memory range must be aligned with section */
  1020. if ((start_pfn & ~PAGE_SECTION_MASK) ||
  1021. (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
  1022. pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
  1023. (unsigned long long)start,
  1024. (unsigned long long)size);
  1025. return -EINVAL;
  1026. }
  1027. return 0;
  1028. }
  1029. /*
  1030. * If movable zone has already been setup, newly added memory should be check.
  1031. * If its address is higher than movable zone, it should be added as movable.
  1032. * Without this check, movable zone may overlap with other zone.
  1033. */
  1034. static int should_add_memory_movable(int nid, u64 start, u64 size)
  1035. {
  1036. unsigned long start_pfn = start >> PAGE_SHIFT;
  1037. pg_data_t *pgdat = NODE_DATA(nid);
  1038. struct zone *movable_zone = pgdat->node_zones + ZONE_MOVABLE;
  1039. if (zone_is_empty(movable_zone))
  1040. return 0;
  1041. if (movable_zone->zone_start_pfn <= start_pfn)
  1042. return 1;
  1043. return 0;
  1044. }
  1045. int zone_for_memory(int nid, u64 start, u64 size, int zone_default,
  1046. bool for_device)
  1047. {
  1048. #ifdef CONFIG_ZONE_DEVICE
  1049. if (for_device)
  1050. return ZONE_DEVICE;
  1051. #endif
  1052. if (should_add_memory_movable(nid, start, size))
  1053. return ZONE_MOVABLE;
  1054. return zone_default;
  1055. }
  1056. static int online_memory_block(struct memory_block *mem, void *arg)
  1057. {
  1058. return memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
  1059. }
  1060. /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
  1061. int __ref add_memory_resource(int nid, struct resource *res, bool online)
  1062. {
  1063. u64 start, size;
  1064. pg_data_t *pgdat = NULL;
  1065. bool new_pgdat;
  1066. bool new_node;
  1067. int ret;
  1068. start = res->start;
  1069. size = resource_size(res);
  1070. ret = check_hotplug_memory_range(start, size);
  1071. if (ret)
  1072. return ret;
  1073. { /* Stupid hack to suppress address-never-null warning */
  1074. void *p = NODE_DATA(nid);
  1075. new_pgdat = !p;
  1076. }
  1077. mem_hotplug_begin();
  1078. /*
  1079. * Add new range to memblock so that when hotadd_new_pgdat() is called
  1080. * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
  1081. * this new range and calculate total pages correctly. The range will
  1082. * be removed at hot-remove time.
  1083. */
  1084. memblock_add_node(start, size, nid);
  1085. new_node = !node_online(nid);
  1086. if (new_node) {
  1087. pgdat = hotadd_new_pgdat(nid, start);
  1088. ret = -ENOMEM;
  1089. if (!pgdat)
  1090. goto error;
  1091. }
  1092. /* call arch's memory hotadd */
  1093. ret = arch_add_memory(nid, start, size, false);
  1094. if (ret < 0)
  1095. goto error;
  1096. /* we online node here. we can't roll back from here. */
  1097. node_set_online(nid);
  1098. if (new_node) {
  1099. ret = register_one_node(nid);
  1100. /*
  1101. * If sysfs file of new node can't create, cpu on the node
  1102. * can't be hot-added. There is no rollback way now.
  1103. * So, check by BUG_ON() to catch it reluctantly..
  1104. */
  1105. BUG_ON(ret);
  1106. }
  1107. /* create new memmap entry */
  1108. firmware_map_add_hotplug(start, start + size, "System RAM");
  1109. /* online pages if requested */
  1110. if (online)
  1111. walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1),
  1112. NULL, online_memory_block);
  1113. goto out;
  1114. error:
  1115. /* rollback pgdat allocation and others */
  1116. if (new_pgdat)
  1117. rollback_node_hotadd(nid, pgdat);
  1118. memblock_remove(start, size);
  1119. out:
  1120. mem_hotplug_done();
  1121. return ret;
  1122. }
  1123. EXPORT_SYMBOL_GPL(add_memory_resource);
  1124. int __ref add_memory(int nid, u64 start, u64 size)
  1125. {
  1126. struct resource *res;
  1127. int ret;
  1128. res = register_memory_resource(start, size);
  1129. if (IS_ERR(res))
  1130. return PTR_ERR(res);
  1131. ret = add_memory_resource(nid, res, memhp_auto_online);
  1132. if (ret < 0)
  1133. release_memory_resource(res);
  1134. return ret;
  1135. }
  1136. EXPORT_SYMBOL_GPL(add_memory);
  1137. #ifdef CONFIG_MEMORY_HOTREMOVE
  1138. /*
  1139. * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
  1140. * set and the size of the free page is given by page_order(). Using this,
  1141. * the function determines if the pageblock contains only free pages.
  1142. * Due to buddy contraints, a free page at least the size of a pageblock will
  1143. * be located at the start of the pageblock
  1144. */
  1145. static inline int pageblock_free(struct page *page)
  1146. {
  1147. return PageBuddy(page) && page_order(page) >= pageblock_order;
  1148. }
  1149. /* Return the start of the next active pageblock after a given page */
  1150. static struct page *next_active_pageblock(struct page *page)
  1151. {
  1152. /* Ensure the starting page is pageblock-aligned */
  1153. BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
  1154. /* If the entire pageblock is free, move to the end of free page */
  1155. if (pageblock_free(page)) {
  1156. int order;
  1157. /* be careful. we don't have locks, page_order can be changed.*/
  1158. order = page_order(page);
  1159. if ((order < MAX_ORDER) && (order >= pageblock_order))
  1160. return page + (1 << order);
  1161. }
  1162. return page + pageblock_nr_pages;
  1163. }
  1164. /* Checks if this range of memory is likely to be hot-removable. */
  1165. int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
  1166. {
  1167. struct page *page = pfn_to_page(start_pfn);
  1168. struct page *end_page = page + nr_pages;
  1169. /* Check the starting page of each pageblock within the range */
  1170. for (; page < end_page; page = next_active_pageblock(page)) {
  1171. if (!is_pageblock_removable_nolock(page))
  1172. return 0;
  1173. cond_resched();
  1174. }
  1175. /* All pageblocks in the memory block are likely to be hot-removable */
  1176. return 1;
  1177. }
  1178. /*
  1179. * Confirm all pages in a range [start, end) is belongs to the same zone.
  1180. */
  1181. int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
  1182. {
  1183. unsigned long pfn, sec_end_pfn;
  1184. struct zone *zone = NULL;
  1185. struct page *page;
  1186. int i;
  1187. for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn);
  1188. pfn < end_pfn;
  1189. pfn = sec_end_pfn + 1, sec_end_pfn += PAGES_PER_SECTION) {
  1190. /* Make sure the memory section is present first */
  1191. if (!present_section_nr(pfn_to_section_nr(pfn)))
  1192. continue;
  1193. for (; pfn < sec_end_pfn && pfn < end_pfn;
  1194. pfn += MAX_ORDER_NR_PAGES) {
  1195. i = 0;
  1196. /* This is just a CONFIG_HOLES_IN_ZONE check.*/
  1197. while ((i < MAX_ORDER_NR_PAGES) &&
  1198. !pfn_valid_within(pfn + i))
  1199. i++;
  1200. if (i == MAX_ORDER_NR_PAGES)
  1201. continue;
  1202. page = pfn_to_page(pfn + i);
  1203. if (zone && page_zone(page) != zone)
  1204. return 0;
  1205. zone = page_zone(page);
  1206. }
  1207. }
  1208. return 1;
  1209. }
  1210. /*
  1211. * Scan pfn range [start,end) to find movable/migratable pages (LRU pages
  1212. * and hugepages). We scan pfn because it's much easier than scanning over
  1213. * linked list. This function returns the pfn of the first found movable
  1214. * page if it's found, otherwise 0.
  1215. */
  1216. static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
  1217. {
  1218. unsigned long pfn;
  1219. struct page *page;
  1220. for (pfn = start; pfn < end; pfn++) {
  1221. if (pfn_valid(pfn)) {
  1222. page = pfn_to_page(pfn);
  1223. if (PageLRU(page))
  1224. return pfn;
  1225. if (PageHuge(page)) {
  1226. if (page_huge_active(page))
  1227. return pfn;
  1228. else
  1229. pfn = round_up(pfn + 1,
  1230. 1 << compound_order(page)) - 1;
  1231. }
  1232. }
  1233. }
  1234. return 0;
  1235. }
  1236. #define NR_OFFLINE_AT_ONCE_PAGES (256)
  1237. static int
  1238. do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
  1239. {
  1240. unsigned long pfn;
  1241. struct page *page;
  1242. int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
  1243. int not_managed = 0;
  1244. int ret = 0;
  1245. LIST_HEAD(source);
  1246. for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
  1247. if (!pfn_valid(pfn))
  1248. continue;
  1249. page = pfn_to_page(pfn);
  1250. if (PageHuge(page)) {
  1251. struct page *head = compound_head(page);
  1252. pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
  1253. if (compound_order(head) > PFN_SECTION_SHIFT) {
  1254. ret = -EBUSY;
  1255. break;
  1256. }
  1257. if (isolate_huge_page(page, &source))
  1258. move_pages -= 1 << compound_order(head);
  1259. continue;
  1260. }
  1261. if (!get_page_unless_zero(page))
  1262. continue;
  1263. /*
  1264. * We can skip free pages. And we can only deal with pages on
  1265. * LRU.
  1266. */
  1267. ret = isolate_lru_page(page);
  1268. if (!ret) { /* Success */
  1269. put_page(page);
  1270. list_add_tail(&page->lru, &source);
  1271. move_pages--;
  1272. inc_zone_page_state(page, NR_ISOLATED_ANON +
  1273. page_is_file_cache(page));
  1274. } else {
  1275. #ifdef CONFIG_DEBUG_VM
  1276. printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
  1277. pfn);
  1278. dump_page(page, "failed to remove from LRU");
  1279. #endif
  1280. put_page(page);
  1281. /* Because we don't have big zone->lock. we should
  1282. check this again here. */
  1283. if (page_count(page)) {
  1284. not_managed++;
  1285. ret = -EBUSY;
  1286. break;
  1287. }
  1288. }
  1289. }
  1290. if (!list_empty(&source)) {
  1291. if (not_managed) {
  1292. putback_movable_pages(&source);
  1293. goto out;
  1294. }
  1295. /*
  1296. * alloc_migrate_target should be improooooved!!
  1297. * migrate_pages returns # of failed pages.
  1298. */
  1299. ret = migrate_pages(&source, alloc_migrate_target, NULL, 0,
  1300. MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
  1301. if (ret)
  1302. putback_movable_pages(&source);
  1303. }
  1304. out:
  1305. return ret;
  1306. }
  1307. /*
  1308. * remove from free_area[] and mark all as Reserved.
  1309. */
  1310. static int
  1311. offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
  1312. void *data)
  1313. {
  1314. __offline_isolated_pages(start, start + nr_pages);
  1315. return 0;
  1316. }
  1317. static void
  1318. offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
  1319. {
  1320. walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
  1321. offline_isolated_pages_cb);
  1322. }
  1323. /*
  1324. * Check all pages in range, recoreded as memory resource, are isolated.
  1325. */
  1326. static int
  1327. check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
  1328. void *data)
  1329. {
  1330. int ret;
  1331. long offlined = *(long *)data;
  1332. ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
  1333. offlined = nr_pages;
  1334. if (!ret)
  1335. *(long *)data += offlined;
  1336. return ret;
  1337. }
  1338. static long
  1339. check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
  1340. {
  1341. long offlined = 0;
  1342. int ret;
  1343. ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
  1344. check_pages_isolated_cb);
  1345. if (ret < 0)
  1346. offlined = (long)ret;
  1347. return offlined;
  1348. }
  1349. #ifdef CONFIG_MOVABLE_NODE
  1350. /*
  1351. * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
  1352. * normal memory.
  1353. */
  1354. static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
  1355. {
  1356. return true;
  1357. }
  1358. #else /* CONFIG_MOVABLE_NODE */
  1359. /* ensure the node has NORMAL memory if it is still online */
  1360. static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
  1361. {
  1362. struct pglist_data *pgdat = zone->zone_pgdat;
  1363. unsigned long present_pages = 0;
  1364. enum zone_type zt;
  1365. for (zt = 0; zt <= ZONE_NORMAL; zt++)
  1366. present_pages += pgdat->node_zones[zt].present_pages;
  1367. if (present_pages > nr_pages)
  1368. return true;
  1369. present_pages = 0;
  1370. for (; zt <= ZONE_MOVABLE; zt++)
  1371. present_pages += pgdat->node_zones[zt].present_pages;
  1372. /*
  1373. * we can't offline the last normal memory until all
  1374. * higher memory is offlined.
  1375. */
  1376. return present_pages == 0;
  1377. }
  1378. #endif /* CONFIG_MOVABLE_NODE */
  1379. static int __init cmdline_parse_movable_node(char *p)
  1380. {
  1381. #ifdef CONFIG_MOVABLE_NODE
  1382. /*
  1383. * Memory used by the kernel cannot be hot-removed because Linux
  1384. * cannot migrate the kernel pages. When memory hotplug is
  1385. * enabled, we should prevent memblock from allocating memory
  1386. * for the kernel.
  1387. *
  1388. * ACPI SRAT records all hotpluggable memory ranges. But before
  1389. * SRAT is parsed, we don't know about it.
  1390. *
  1391. * The kernel image is loaded into memory at very early time. We
  1392. * cannot prevent this anyway. So on NUMA system, we set any
  1393. * node the kernel resides in as un-hotpluggable.
  1394. *
  1395. * Since on modern servers, one node could have double-digit
  1396. * gigabytes memory, we can assume the memory around the kernel
  1397. * image is also un-hotpluggable. So before SRAT is parsed, just
  1398. * allocate memory near the kernel image to try the best to keep
  1399. * the kernel away from hotpluggable memory.
  1400. */
  1401. memblock_set_bottom_up(true);
  1402. movable_node_enabled = true;
  1403. #else
  1404. pr_warn("movable_node option not supported\n");
  1405. #endif
  1406. return 0;
  1407. }
  1408. early_param("movable_node", cmdline_parse_movable_node);
  1409. /* check which state of node_states will be changed when offline memory */
  1410. static void node_states_check_changes_offline(unsigned long nr_pages,
  1411. struct zone *zone, struct memory_notify *arg)
  1412. {
  1413. struct pglist_data *pgdat = zone->zone_pgdat;
  1414. unsigned long present_pages = 0;
  1415. enum zone_type zt, zone_last = ZONE_NORMAL;
  1416. /*
  1417. * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
  1418. * contains nodes which have zones of 0...ZONE_NORMAL,
  1419. * set zone_last to ZONE_NORMAL.
  1420. *
  1421. * If we don't have HIGHMEM nor movable node,
  1422. * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
  1423. * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
  1424. */
  1425. if (N_MEMORY == N_NORMAL_MEMORY)
  1426. zone_last = ZONE_MOVABLE;
  1427. /*
  1428. * check whether node_states[N_NORMAL_MEMORY] will be changed.
  1429. * If the memory to be offline is in a zone of 0...zone_last,
  1430. * and it is the last present memory, 0...zone_last will
  1431. * become empty after offline , thus we can determind we will
  1432. * need to clear the node from node_states[N_NORMAL_MEMORY].
  1433. */
  1434. for (zt = 0; zt <= zone_last; zt++)
  1435. present_pages += pgdat->node_zones[zt].present_pages;
  1436. if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
  1437. arg->status_change_nid_normal = zone_to_nid(zone);
  1438. else
  1439. arg->status_change_nid_normal = -1;
  1440. #ifdef CONFIG_HIGHMEM
  1441. /*
  1442. * If we have movable node, node_states[N_HIGH_MEMORY]
  1443. * contains nodes which have zones of 0...ZONE_HIGHMEM,
  1444. * set zone_last to ZONE_HIGHMEM.
  1445. *
  1446. * If we don't have movable node, node_states[N_NORMAL_MEMORY]
  1447. * contains nodes which have zones of 0...ZONE_MOVABLE,
  1448. * set zone_last to ZONE_MOVABLE.
  1449. */
  1450. zone_last = ZONE_HIGHMEM;
  1451. if (N_MEMORY == N_HIGH_MEMORY)
  1452. zone_last = ZONE_MOVABLE;
  1453. for (; zt <= zone_last; zt++)
  1454. present_pages += pgdat->node_zones[zt].present_pages;
  1455. if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
  1456. arg->status_change_nid_high = zone_to_nid(zone);
  1457. else
  1458. arg->status_change_nid_high = -1;
  1459. #else
  1460. arg->status_change_nid_high = arg->status_change_nid_normal;
  1461. #endif
  1462. /*
  1463. * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
  1464. */
  1465. zone_last = ZONE_MOVABLE;
  1466. /*
  1467. * check whether node_states[N_HIGH_MEMORY] will be changed
  1468. * If we try to offline the last present @nr_pages from the node,
  1469. * we can determind we will need to clear the node from
  1470. * node_states[N_HIGH_MEMORY].
  1471. */
  1472. for (; zt <= zone_last; zt++)
  1473. present_pages += pgdat->node_zones[zt].present_pages;
  1474. if (nr_pages >= present_pages)
  1475. arg->status_change_nid = zone_to_nid(zone);
  1476. else
  1477. arg->status_change_nid = -1;
  1478. }
  1479. static void node_states_clear_node(int node, struct memory_notify *arg)
  1480. {
  1481. if (arg->status_change_nid_normal >= 0)
  1482. node_clear_state(node, N_NORMAL_MEMORY);
  1483. if ((N_MEMORY != N_NORMAL_MEMORY) &&
  1484. (arg->status_change_nid_high >= 0))
  1485. node_clear_state(node, N_HIGH_MEMORY);
  1486. if ((N_MEMORY != N_HIGH_MEMORY) &&
  1487. (arg->status_change_nid >= 0))
  1488. node_clear_state(node, N_MEMORY);
  1489. }
  1490. static int __ref __offline_pages(unsigned long start_pfn,
  1491. unsigned long end_pfn, unsigned long timeout)
  1492. {
  1493. unsigned long pfn, nr_pages, expire;
  1494. long offlined_pages;
  1495. int ret, drain, retry_max, node;
  1496. unsigned long flags;
  1497. struct zone *zone;
  1498. struct memory_notify arg;
  1499. /* at least, alignment against pageblock is necessary */
  1500. if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
  1501. return -EINVAL;
  1502. if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
  1503. return -EINVAL;
  1504. /* This makes hotplug much easier...and readable.
  1505. we assume this for now. .*/
  1506. if (!test_pages_in_a_zone(start_pfn, end_pfn))
  1507. return -EINVAL;
  1508. zone = page_zone(pfn_to_page(start_pfn));
  1509. node = zone_to_nid(zone);
  1510. nr_pages = end_pfn - start_pfn;
  1511. if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
  1512. return -EINVAL;
  1513. /* set above range as isolated */
  1514. ret = start_isolate_page_range(start_pfn, end_pfn,
  1515. MIGRATE_MOVABLE, true);
  1516. if (ret)
  1517. return ret;
  1518. arg.start_pfn = start_pfn;
  1519. arg.nr_pages = nr_pages;
  1520. node_states_check_changes_offline(nr_pages, zone, &arg);
  1521. ret = memory_notify(MEM_GOING_OFFLINE, &arg);
  1522. ret = notifier_to_errno(ret);
  1523. if (ret)
  1524. goto failed_removal;
  1525. pfn = start_pfn;
  1526. expire = jiffies + timeout;
  1527. drain = 0;
  1528. retry_max = 5;
  1529. repeat:
  1530. /* start memory hot removal */
  1531. ret = -EAGAIN;
  1532. if (time_after(jiffies, expire))
  1533. goto failed_removal;
  1534. ret = -EINTR;
  1535. if (signal_pending(current))
  1536. goto failed_removal;
  1537. ret = 0;
  1538. if (drain) {
  1539. lru_add_drain_all();
  1540. cond_resched();
  1541. drain_all_pages(zone);
  1542. }
  1543. pfn = scan_movable_pages(start_pfn, end_pfn);
  1544. if (pfn) { /* We have movable pages */
  1545. ret = do_migrate_range(pfn, end_pfn);
  1546. if (!ret) {
  1547. drain = 1;
  1548. goto repeat;
  1549. } else {
  1550. if (ret < 0)
  1551. if (--retry_max == 0)
  1552. goto failed_removal;
  1553. yield();
  1554. drain = 1;
  1555. goto repeat;
  1556. }
  1557. }
  1558. /* drain all zone's lru pagevec, this is asynchronous... */
  1559. lru_add_drain_all();
  1560. yield();
  1561. /* drain pcp pages, this is synchronous. */
  1562. drain_all_pages(zone);
  1563. /*
  1564. * dissolve free hugepages in the memory block before doing offlining
  1565. * actually in order to make hugetlbfs's object counting consistent.
  1566. */
  1567. dissolve_free_huge_pages(start_pfn, end_pfn);
  1568. /* check again */
  1569. offlined_pages = check_pages_isolated(start_pfn, end_pfn);
  1570. if (offlined_pages < 0) {
  1571. ret = -EBUSY;
  1572. goto failed_removal;
  1573. }
  1574. printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
  1575. /* Ok, all of our target is isolated.
  1576. We cannot do rollback at this point. */
  1577. offline_isolated_pages(start_pfn, end_pfn);
  1578. /* reset pagetype flags and makes migrate type to be MOVABLE */
  1579. undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
  1580. /* removal success */
  1581. adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
  1582. zone->present_pages -= offlined_pages;
  1583. pgdat_resize_lock(zone->zone_pgdat, &flags);
  1584. zone->zone_pgdat->node_present_pages -= offlined_pages;
  1585. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  1586. init_per_zone_wmark_min();
  1587. if (!populated_zone(zone)) {
  1588. zone_pcp_reset(zone);
  1589. mutex_lock(&zonelists_mutex);
  1590. build_all_zonelists(NULL, NULL);
  1591. mutex_unlock(&zonelists_mutex);
  1592. } else
  1593. zone_pcp_update(zone);
  1594. node_states_clear_node(node, &arg);
  1595. if (arg.status_change_nid >= 0) {
  1596. kswapd_stop(node);
  1597. kcompactd_stop(node);
  1598. }
  1599. vm_total_pages = nr_free_pagecache_pages();
  1600. writeback_set_ratelimit();
  1601. memory_notify(MEM_OFFLINE, &arg);
  1602. return 0;
  1603. failed_removal:
  1604. printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
  1605. (unsigned long long) start_pfn << PAGE_SHIFT,
  1606. ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
  1607. memory_notify(MEM_CANCEL_OFFLINE, &arg);
  1608. /* pushback to free area */
  1609. undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
  1610. return ret;
  1611. }
  1612. /* Must be protected by mem_hotplug_begin() */
  1613. int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
  1614. {
  1615. return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
  1616. }
  1617. #endif /* CONFIG_MEMORY_HOTREMOVE */
  1618. /**
  1619. * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
  1620. * @start_pfn: start pfn of the memory range
  1621. * @end_pfn: end pfn of the memory range
  1622. * @arg: argument passed to func
  1623. * @func: callback for each memory section walked
  1624. *
  1625. * This function walks through all present mem sections in range
  1626. * [start_pfn, end_pfn) and call func on each mem section.
  1627. *
  1628. * Returns the return value of func.
  1629. */
  1630. int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
  1631. void *arg, int (*func)(struct memory_block *, void *))
  1632. {
  1633. struct memory_block *mem = NULL;
  1634. struct mem_section *section;
  1635. unsigned long pfn, section_nr;
  1636. int ret;
  1637. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  1638. section_nr = pfn_to_section_nr(pfn);
  1639. if (!present_section_nr(section_nr))
  1640. continue;
  1641. section = __nr_to_section(section_nr);
  1642. /* same memblock? */
  1643. if (mem)
  1644. if ((section_nr >= mem->start_section_nr) &&
  1645. (section_nr <= mem->end_section_nr))
  1646. continue;
  1647. mem = find_memory_block_hinted(section, mem);
  1648. if (!mem)
  1649. continue;
  1650. ret = func(mem, arg);
  1651. if (ret) {
  1652. kobject_put(&mem->dev.kobj);
  1653. return ret;
  1654. }
  1655. }
  1656. if (mem)
  1657. kobject_put(&mem->dev.kobj);
  1658. return 0;
  1659. }
  1660. #ifdef CONFIG_MEMORY_HOTREMOVE
  1661. static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
  1662. {
  1663. int ret = !is_memblock_offlined(mem);
  1664. if (unlikely(ret)) {
  1665. phys_addr_t beginpa, endpa;
  1666. beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
  1667. endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
  1668. pr_warn("removing memory fails, because memory "
  1669. "[%pa-%pa] is onlined\n",
  1670. &beginpa, &endpa);
  1671. }
  1672. return ret;
  1673. }
  1674. static int check_cpu_on_node(pg_data_t *pgdat)
  1675. {
  1676. int cpu;
  1677. for_each_present_cpu(cpu) {
  1678. if (cpu_to_node(cpu) == pgdat->node_id)
  1679. /*
  1680. * the cpu on this node isn't removed, and we can't
  1681. * offline this node.
  1682. */
  1683. return -EBUSY;
  1684. }
  1685. return 0;
  1686. }
  1687. static void unmap_cpu_on_node(pg_data_t *pgdat)
  1688. {
  1689. #ifdef CONFIG_ACPI_NUMA
  1690. int cpu;
  1691. for_each_possible_cpu(cpu)
  1692. if (cpu_to_node(cpu) == pgdat->node_id)
  1693. numa_clear_node(cpu);
  1694. #endif
  1695. }
  1696. static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
  1697. {
  1698. int ret;
  1699. ret = check_cpu_on_node(pgdat);
  1700. if (ret)
  1701. return ret;
  1702. /*
  1703. * the node will be offlined when we come here, so we can clear
  1704. * the cpu_to_node() now.
  1705. */
  1706. unmap_cpu_on_node(pgdat);
  1707. return 0;
  1708. }
  1709. /**
  1710. * try_offline_node
  1711. *
  1712. * Offline a node if all memory sections and cpus of the node are removed.
  1713. *
  1714. * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
  1715. * and online/offline operations before this call.
  1716. */
  1717. void try_offline_node(int nid)
  1718. {
  1719. pg_data_t *pgdat = NODE_DATA(nid);
  1720. unsigned long start_pfn = pgdat->node_start_pfn;
  1721. unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
  1722. unsigned long pfn;
  1723. int i;
  1724. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  1725. unsigned long section_nr = pfn_to_section_nr(pfn);
  1726. if (!present_section_nr(section_nr))
  1727. continue;
  1728. if (pfn_to_nid(pfn) != nid)
  1729. continue;
  1730. /*
  1731. * some memory sections of this node are not removed, and we
  1732. * can't offline node now.
  1733. */
  1734. return;
  1735. }
  1736. if (check_and_unmap_cpu_on_node(pgdat))
  1737. return;
  1738. /*
  1739. * all memory/cpu of this node are removed, we can offline this
  1740. * node now.
  1741. */
  1742. node_set_offline(nid);
  1743. unregister_one_node(nid);
  1744. /* free waittable in each zone */
  1745. for (i = 0; i < MAX_NR_ZONES; i++) {
  1746. struct zone *zone = pgdat->node_zones + i;
  1747. /*
  1748. * wait_table may be allocated from boot memory,
  1749. * here only free if it's allocated by vmalloc.
  1750. */
  1751. if (is_vmalloc_addr(zone->wait_table)) {
  1752. vfree(zone->wait_table);
  1753. zone->wait_table = NULL;
  1754. }
  1755. }
  1756. }
  1757. EXPORT_SYMBOL(try_offline_node);
  1758. /**
  1759. * remove_memory
  1760. *
  1761. * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
  1762. * and online/offline operations before this call, as required by
  1763. * try_offline_node().
  1764. */
  1765. void __ref remove_memory(int nid, u64 start, u64 size)
  1766. {
  1767. int ret;
  1768. BUG_ON(check_hotplug_memory_range(start, size));
  1769. mem_hotplug_begin();
  1770. /*
  1771. * All memory blocks must be offlined before removing memory. Check
  1772. * whether all memory blocks in question are offline and trigger a BUG()
  1773. * if this is not the case.
  1774. */
  1775. ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
  1776. check_memblock_offlined_cb);
  1777. if (ret)
  1778. BUG();
  1779. /* remove memmap entry */
  1780. firmware_map_remove(start, start + size, "System RAM");
  1781. memblock_free(start, size);
  1782. memblock_remove(start, size);
  1783. arch_remove_memory(start, size);
  1784. try_offline_node(nid);
  1785. mem_hotplug_done();
  1786. }
  1787. EXPORT_SYMBOL_GPL(remove_memory);
  1788. #endif /* CONFIG_MEMORY_HOTREMOVE */