memory_hotplug.c 51 KB

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