memory_hotplug.c 53 KB

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