memory_hotplug.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024
  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. 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. 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, pfn;
  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. /* online_page_range is called later and expects pages reserved */
  382. for (pfn = phys_start_pfn; pfn < phys_start_pfn + nr_pages; pfn++) {
  383. if (!pfn_valid(pfn))
  384. continue;
  385. SetPageReserved(pfn_to_page(pfn));
  386. }
  387. return 0;
  388. }
  389. static int __meminit __add_section(int nid, struct zone *zone,
  390. unsigned long phys_start_pfn)
  391. {
  392. int ret;
  393. if (pfn_valid(phys_start_pfn))
  394. return -EEXIST;
  395. ret = sparse_add_one_section(zone, phys_start_pfn);
  396. if (ret < 0)
  397. return ret;
  398. ret = __add_zone(zone, phys_start_pfn);
  399. if (ret < 0)
  400. return ret;
  401. return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
  402. }
  403. /*
  404. * Reasonably generic function for adding memory. It is
  405. * expected that archs that support memory hotplug will
  406. * call this function after deciding the zone to which to
  407. * add the new pages.
  408. */
  409. int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
  410. unsigned long nr_pages)
  411. {
  412. unsigned long i;
  413. int err = 0;
  414. int start_sec, end_sec;
  415. /* during initialize mem_map, align hot-added range to section */
  416. start_sec = pfn_to_section_nr(phys_start_pfn);
  417. end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
  418. for (i = start_sec; i <= end_sec; i++) {
  419. err = __add_section(nid, zone, section_nr_to_pfn(i));
  420. /*
  421. * EEXIST is finally dealt with by ioresource collision
  422. * check. see add_memory() => register_memory_resource()
  423. * Warning will be printed if there is collision.
  424. */
  425. if (err && (err != -EEXIST))
  426. break;
  427. err = 0;
  428. }
  429. vmemmap_populate_print_last();
  430. return err;
  431. }
  432. EXPORT_SYMBOL_GPL(__add_pages);
  433. #ifdef CONFIG_MEMORY_HOTREMOVE
  434. /* find the smallest valid pfn in the range [start_pfn, end_pfn) */
  435. static int find_smallest_section_pfn(int nid, struct zone *zone,
  436. unsigned long start_pfn,
  437. unsigned long end_pfn)
  438. {
  439. struct mem_section *ms;
  440. for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
  441. ms = __pfn_to_section(start_pfn);
  442. if (unlikely(!valid_section(ms)))
  443. continue;
  444. if (unlikely(pfn_to_nid(start_pfn) != nid))
  445. continue;
  446. if (zone && zone != page_zone(pfn_to_page(start_pfn)))
  447. continue;
  448. return start_pfn;
  449. }
  450. return 0;
  451. }
  452. /* find the biggest valid pfn in the range [start_pfn, end_pfn). */
  453. static int find_biggest_section_pfn(int nid, struct zone *zone,
  454. unsigned long start_pfn,
  455. unsigned long end_pfn)
  456. {
  457. struct mem_section *ms;
  458. unsigned long pfn;
  459. /* pfn is the end pfn of a memory section. */
  460. pfn = end_pfn - 1;
  461. for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
  462. ms = __pfn_to_section(pfn);
  463. if (unlikely(!valid_section(ms)))
  464. continue;
  465. if (unlikely(pfn_to_nid(pfn) != nid))
  466. continue;
  467. if (zone && zone != page_zone(pfn_to_page(pfn)))
  468. continue;
  469. return pfn;
  470. }
  471. return 0;
  472. }
  473. static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
  474. unsigned long end_pfn)
  475. {
  476. unsigned long zone_start_pfn = zone->zone_start_pfn;
  477. unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
  478. unsigned long zone_end_pfn = z;
  479. unsigned long pfn;
  480. struct mem_section *ms;
  481. int nid = zone_to_nid(zone);
  482. zone_span_writelock(zone);
  483. if (zone_start_pfn == start_pfn) {
  484. /*
  485. * If the section is smallest section in the zone, it need
  486. * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
  487. * In this case, we find second smallest valid mem_section
  488. * for shrinking zone.
  489. */
  490. pfn = find_smallest_section_pfn(nid, zone, end_pfn,
  491. zone_end_pfn);
  492. if (pfn) {
  493. zone->zone_start_pfn = pfn;
  494. zone->spanned_pages = zone_end_pfn - pfn;
  495. }
  496. } else if (zone_end_pfn == end_pfn) {
  497. /*
  498. * If the section is biggest section in the zone, it need
  499. * shrink zone->spanned_pages.
  500. * In this case, we find second biggest valid mem_section for
  501. * shrinking zone.
  502. */
  503. pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
  504. start_pfn);
  505. if (pfn)
  506. zone->spanned_pages = pfn - zone_start_pfn + 1;
  507. }
  508. /*
  509. * The section is not biggest or smallest mem_section in the zone, it
  510. * only creates a hole in the zone. So in this case, we need not
  511. * change the zone. But perhaps, the zone has only hole data. Thus
  512. * it check the zone has only hole or not.
  513. */
  514. pfn = zone_start_pfn;
  515. for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
  516. ms = __pfn_to_section(pfn);
  517. if (unlikely(!valid_section(ms)))
  518. continue;
  519. if (page_zone(pfn_to_page(pfn)) != zone)
  520. continue;
  521. /* If the section is current section, it continues the loop */
  522. if (start_pfn == pfn)
  523. continue;
  524. /* If we find valid section, we have nothing to do */
  525. zone_span_writeunlock(zone);
  526. return;
  527. }
  528. /* The zone has no valid section */
  529. zone->zone_start_pfn = 0;
  530. zone->spanned_pages = 0;
  531. zone_span_writeunlock(zone);
  532. }
  533. static void shrink_pgdat_span(struct pglist_data *pgdat,
  534. unsigned long start_pfn, unsigned long end_pfn)
  535. {
  536. unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
  537. unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */
  538. unsigned long pgdat_end_pfn = p;
  539. unsigned long pfn;
  540. struct mem_section *ms;
  541. int nid = pgdat->node_id;
  542. if (pgdat_start_pfn == start_pfn) {
  543. /*
  544. * If the section is smallest section in the pgdat, it need
  545. * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
  546. * In this case, we find second smallest valid mem_section
  547. * for shrinking zone.
  548. */
  549. pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
  550. pgdat_end_pfn);
  551. if (pfn) {
  552. pgdat->node_start_pfn = pfn;
  553. pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
  554. }
  555. } else if (pgdat_end_pfn == end_pfn) {
  556. /*
  557. * If the section is biggest section in the pgdat, it need
  558. * shrink pgdat->node_spanned_pages.
  559. * In this case, we find second biggest valid mem_section for
  560. * shrinking zone.
  561. */
  562. pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
  563. start_pfn);
  564. if (pfn)
  565. pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
  566. }
  567. /*
  568. * If the section is not biggest or smallest mem_section in the pgdat,
  569. * it only creates a hole in the pgdat. So in this case, we need not
  570. * change the pgdat.
  571. * But perhaps, the pgdat has only hole data. Thus it check the pgdat
  572. * has only hole or not.
  573. */
  574. pfn = pgdat_start_pfn;
  575. for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
  576. ms = __pfn_to_section(pfn);
  577. if (unlikely(!valid_section(ms)))
  578. continue;
  579. if (pfn_to_nid(pfn) != nid)
  580. continue;
  581. /* If the section is current section, it continues the loop */
  582. if (start_pfn == pfn)
  583. continue;
  584. /* If we find valid section, we have nothing to do */
  585. return;
  586. }
  587. /* The pgdat has no valid section */
  588. pgdat->node_start_pfn = 0;
  589. pgdat->node_spanned_pages = 0;
  590. }
  591. static void __remove_zone(struct zone *zone, unsigned long start_pfn)
  592. {
  593. struct pglist_data *pgdat = zone->zone_pgdat;
  594. int nr_pages = PAGES_PER_SECTION;
  595. int zone_type;
  596. unsigned long flags;
  597. zone_type = zone - pgdat->node_zones;
  598. pgdat_resize_lock(zone->zone_pgdat, &flags);
  599. shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
  600. shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
  601. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  602. }
  603. static int __remove_section(struct zone *zone, struct mem_section *ms)
  604. {
  605. unsigned long start_pfn;
  606. int scn_nr;
  607. int ret = -EINVAL;
  608. if (!valid_section(ms))
  609. return ret;
  610. ret = unregister_memory_section(ms);
  611. if (ret)
  612. return ret;
  613. scn_nr = __section_nr(ms);
  614. start_pfn = section_nr_to_pfn(scn_nr);
  615. __remove_zone(zone, start_pfn);
  616. sparse_remove_one_section(zone, ms);
  617. return 0;
  618. }
  619. /**
  620. * __remove_pages() - remove sections of pages from a zone
  621. * @zone: zone from which pages need to be removed
  622. * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
  623. * @nr_pages: number of pages to remove (must be multiple of section size)
  624. *
  625. * Generic helper function to remove section mappings and sysfs entries
  626. * for the section of the memory we are removing. Caller needs to make
  627. * sure that pages are marked reserved and zones are adjust properly by
  628. * calling offline_pages().
  629. */
  630. int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
  631. unsigned long nr_pages)
  632. {
  633. unsigned long i;
  634. int sections_to_remove;
  635. resource_size_t start, size;
  636. int ret = 0;
  637. /*
  638. * We can only remove entire sections
  639. */
  640. BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
  641. BUG_ON(nr_pages % PAGES_PER_SECTION);
  642. start = phys_start_pfn << PAGE_SHIFT;
  643. size = nr_pages * PAGE_SIZE;
  644. ret = release_mem_region_adjustable(&iomem_resource, start, size);
  645. if (ret) {
  646. resource_size_t endres = start + size - 1;
  647. pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
  648. &start, &endres, ret);
  649. }
  650. sections_to_remove = nr_pages / PAGES_PER_SECTION;
  651. for (i = 0; i < sections_to_remove; i++) {
  652. unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
  653. ret = __remove_section(zone, __pfn_to_section(pfn));
  654. if (ret)
  655. break;
  656. }
  657. return ret;
  658. }
  659. EXPORT_SYMBOL_GPL(__remove_pages);
  660. #endif /* CONFIG_MEMORY_HOTREMOVE */
  661. int set_online_page_callback(online_page_callback_t callback)
  662. {
  663. int rc = -EINVAL;
  664. get_online_mems();
  665. mutex_lock(&online_page_callback_lock);
  666. if (online_page_callback == generic_online_page) {
  667. online_page_callback = callback;
  668. rc = 0;
  669. }
  670. mutex_unlock(&online_page_callback_lock);
  671. put_online_mems();
  672. return rc;
  673. }
  674. EXPORT_SYMBOL_GPL(set_online_page_callback);
  675. int restore_online_page_callback(online_page_callback_t callback)
  676. {
  677. int rc = -EINVAL;
  678. get_online_mems();
  679. mutex_lock(&online_page_callback_lock);
  680. if (online_page_callback == callback) {
  681. online_page_callback = generic_online_page;
  682. rc = 0;
  683. }
  684. mutex_unlock(&online_page_callback_lock);
  685. put_online_mems();
  686. return rc;
  687. }
  688. EXPORT_SYMBOL_GPL(restore_online_page_callback);
  689. void __online_page_set_limits(struct page *page)
  690. {
  691. }
  692. EXPORT_SYMBOL_GPL(__online_page_set_limits);
  693. void __online_page_increment_counters(struct page *page)
  694. {
  695. adjust_managed_page_count(page, 1);
  696. }
  697. EXPORT_SYMBOL_GPL(__online_page_increment_counters);
  698. void __online_page_free(struct page *page)
  699. {
  700. __free_reserved_page(page);
  701. }
  702. EXPORT_SYMBOL_GPL(__online_page_free);
  703. static void generic_online_page(struct page *page)
  704. {
  705. __online_page_set_limits(page);
  706. __online_page_increment_counters(page);
  707. __online_page_free(page);
  708. }
  709. static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
  710. void *arg)
  711. {
  712. unsigned long i;
  713. unsigned long onlined_pages = *(unsigned long *)arg;
  714. struct page *page;
  715. if (PageReserved(pfn_to_page(start_pfn)))
  716. for (i = 0; i < nr_pages; i++) {
  717. page = pfn_to_page(start_pfn + i);
  718. (*online_page_callback)(page);
  719. onlined_pages++;
  720. }
  721. *(unsigned long *)arg = onlined_pages;
  722. return 0;
  723. }
  724. #ifdef CONFIG_MOVABLE_NODE
  725. /*
  726. * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
  727. * normal memory.
  728. */
  729. static bool can_online_high_movable(struct zone *zone)
  730. {
  731. return true;
  732. }
  733. #else /* CONFIG_MOVABLE_NODE */
  734. /* ensure every online node has NORMAL memory */
  735. static bool can_online_high_movable(struct zone *zone)
  736. {
  737. return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
  738. }
  739. #endif /* CONFIG_MOVABLE_NODE */
  740. /* check which state of node_states will be changed when online memory */
  741. static void node_states_check_changes_online(unsigned long nr_pages,
  742. struct zone *zone, struct memory_notify *arg)
  743. {
  744. int nid = zone_to_nid(zone);
  745. enum zone_type zone_last = ZONE_NORMAL;
  746. /*
  747. * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
  748. * contains nodes which have zones of 0...ZONE_NORMAL,
  749. * set zone_last to ZONE_NORMAL.
  750. *
  751. * If we don't have HIGHMEM nor movable node,
  752. * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
  753. * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
  754. */
  755. if (N_MEMORY == N_NORMAL_MEMORY)
  756. zone_last = ZONE_MOVABLE;
  757. /*
  758. * if the memory to be online is in a zone of 0...zone_last, and
  759. * the zones of 0...zone_last don't have memory before online, we will
  760. * need to set the node to node_states[N_NORMAL_MEMORY] after
  761. * the memory is online.
  762. */
  763. if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
  764. arg->status_change_nid_normal = nid;
  765. else
  766. arg->status_change_nid_normal = -1;
  767. #ifdef CONFIG_HIGHMEM
  768. /*
  769. * If we have movable node, node_states[N_HIGH_MEMORY]
  770. * contains nodes which have zones of 0...ZONE_HIGHMEM,
  771. * set zone_last to ZONE_HIGHMEM.
  772. *
  773. * If we don't have movable node, node_states[N_NORMAL_MEMORY]
  774. * contains nodes which have zones of 0...ZONE_MOVABLE,
  775. * set zone_last to ZONE_MOVABLE.
  776. */
  777. zone_last = ZONE_HIGHMEM;
  778. if (N_MEMORY == N_HIGH_MEMORY)
  779. zone_last = ZONE_MOVABLE;
  780. if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
  781. arg->status_change_nid_high = nid;
  782. else
  783. arg->status_change_nid_high = -1;
  784. #else
  785. arg->status_change_nid_high = arg->status_change_nid_normal;
  786. #endif
  787. /*
  788. * if the node don't have memory befor online, we will need to
  789. * set the node to node_states[N_MEMORY] after the memory
  790. * is online.
  791. */
  792. if (!node_state(nid, N_MEMORY))
  793. arg->status_change_nid = nid;
  794. else
  795. arg->status_change_nid = -1;
  796. }
  797. static void node_states_set_node(int node, struct memory_notify *arg)
  798. {
  799. if (arg->status_change_nid_normal >= 0)
  800. node_set_state(node, N_NORMAL_MEMORY);
  801. if (arg->status_change_nid_high >= 0)
  802. node_set_state(node, N_HIGH_MEMORY);
  803. node_set_state(node, N_MEMORY);
  804. }
  805. /* Must be protected by mem_hotplug_begin() */
  806. int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
  807. {
  808. unsigned long flags;
  809. unsigned long onlined_pages = 0;
  810. struct zone *zone;
  811. int need_zonelists_rebuild = 0;
  812. int nid;
  813. int ret;
  814. struct memory_notify arg;
  815. /*
  816. * This doesn't need a lock to do pfn_to_page().
  817. * The section can't be removed here because of the
  818. * memory_block->state_mutex.
  819. */
  820. zone = page_zone(pfn_to_page(pfn));
  821. if ((zone_idx(zone) > ZONE_NORMAL ||
  822. online_type == MMOP_ONLINE_MOVABLE) &&
  823. !can_online_high_movable(zone))
  824. return -EINVAL;
  825. if (online_type == MMOP_ONLINE_KERNEL &&
  826. zone_idx(zone) == ZONE_MOVABLE) {
  827. if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages))
  828. return -EINVAL;
  829. }
  830. if (online_type == MMOP_ONLINE_MOVABLE &&
  831. zone_idx(zone) == ZONE_MOVABLE - 1) {
  832. if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages))
  833. return -EINVAL;
  834. }
  835. /* Previous code may changed the zone of the pfn range */
  836. zone = page_zone(pfn_to_page(pfn));
  837. arg.start_pfn = pfn;
  838. arg.nr_pages = nr_pages;
  839. node_states_check_changes_online(nr_pages, zone, &arg);
  840. nid = pfn_to_nid(pfn);
  841. ret = memory_notify(MEM_GOING_ONLINE, &arg);
  842. ret = notifier_to_errno(ret);
  843. if (ret) {
  844. memory_notify(MEM_CANCEL_ONLINE, &arg);
  845. return ret;
  846. }
  847. /*
  848. * If this zone is not populated, then it is not in zonelist.
  849. * This means the page allocator ignores this zone.
  850. * So, zonelist must be updated after online.
  851. */
  852. mutex_lock(&zonelists_mutex);
  853. if (!populated_zone(zone)) {
  854. need_zonelists_rebuild = 1;
  855. build_all_zonelists(NULL, zone);
  856. }
  857. ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
  858. online_pages_range);
  859. if (ret) {
  860. if (need_zonelists_rebuild)
  861. zone_pcp_reset(zone);
  862. mutex_unlock(&zonelists_mutex);
  863. printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
  864. (unsigned long long) pfn << PAGE_SHIFT,
  865. (((unsigned long long) pfn + nr_pages)
  866. << PAGE_SHIFT) - 1);
  867. memory_notify(MEM_CANCEL_ONLINE, &arg);
  868. return ret;
  869. }
  870. zone->present_pages += onlined_pages;
  871. pgdat_resize_lock(zone->zone_pgdat, &flags);
  872. zone->zone_pgdat->node_present_pages += onlined_pages;
  873. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  874. if (onlined_pages) {
  875. node_states_set_node(zone_to_nid(zone), &arg);
  876. if (need_zonelists_rebuild)
  877. build_all_zonelists(NULL, NULL);
  878. else
  879. zone_pcp_update(zone);
  880. }
  881. mutex_unlock(&zonelists_mutex);
  882. init_per_zone_wmark_min();
  883. if (onlined_pages)
  884. kswapd_run(zone_to_nid(zone));
  885. vm_total_pages = nr_free_pagecache_pages();
  886. writeback_set_ratelimit();
  887. if (onlined_pages)
  888. memory_notify(MEM_ONLINE, &arg);
  889. return 0;
  890. }
  891. #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
  892. static void reset_node_present_pages(pg_data_t *pgdat)
  893. {
  894. struct zone *z;
  895. for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
  896. z->present_pages = 0;
  897. pgdat->node_present_pages = 0;
  898. }
  899. /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
  900. static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
  901. {
  902. struct pglist_data *pgdat;
  903. unsigned long zones_size[MAX_NR_ZONES] = {0};
  904. unsigned long zholes_size[MAX_NR_ZONES] = {0};
  905. unsigned long start_pfn = PFN_DOWN(start);
  906. pgdat = NODE_DATA(nid);
  907. if (!pgdat) {
  908. pgdat = arch_alloc_nodedata(nid);
  909. if (!pgdat)
  910. return NULL;
  911. arch_refresh_nodedata(nid, pgdat);
  912. } else {
  913. /* Reset the nr_zones and classzone_idx to 0 before reuse */
  914. pgdat->nr_zones = 0;
  915. pgdat->classzone_idx = 0;
  916. }
  917. /* we can use NODE_DATA(nid) from here */
  918. /* init node's zones as empty zones, we don't have any present pages.*/
  919. free_area_init_node(nid, zones_size, start_pfn, zholes_size);
  920. /*
  921. * The node we allocated has no zone fallback lists. For avoiding
  922. * to access not-initialized zonelist, build here.
  923. */
  924. mutex_lock(&zonelists_mutex);
  925. build_all_zonelists(pgdat, NULL);
  926. mutex_unlock(&zonelists_mutex);
  927. /*
  928. * zone->managed_pages is set to an approximate value in
  929. * free_area_init_core(), which will cause
  930. * /sys/device/system/node/nodeX/meminfo has wrong data.
  931. * So reset it to 0 before any memory is onlined.
  932. */
  933. reset_node_managed_pages(pgdat);
  934. /*
  935. * When memory is hot-added, all the memory is in offline state. So
  936. * clear all zones' present_pages because they will be updated in
  937. * online_pages() and offline_pages().
  938. */
  939. reset_node_present_pages(pgdat);
  940. return pgdat;
  941. }
  942. static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
  943. {
  944. arch_refresh_nodedata(nid, NULL);
  945. arch_free_nodedata(pgdat);
  946. return;
  947. }
  948. /**
  949. * try_online_node - online a node if offlined
  950. *
  951. * called by cpu_up() to online a node without onlined memory.
  952. */
  953. int try_online_node(int nid)
  954. {
  955. pg_data_t *pgdat;
  956. int ret;
  957. if (node_online(nid))
  958. return 0;
  959. mem_hotplug_begin();
  960. pgdat = hotadd_new_pgdat(nid, 0);
  961. if (!pgdat) {
  962. pr_err("Cannot online node %d due to NULL pgdat\n", nid);
  963. ret = -ENOMEM;
  964. goto out;
  965. }
  966. node_set_online(nid);
  967. ret = register_one_node(nid);
  968. BUG_ON(ret);
  969. if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
  970. mutex_lock(&zonelists_mutex);
  971. build_all_zonelists(NULL, NULL);
  972. mutex_unlock(&zonelists_mutex);
  973. }
  974. out:
  975. mem_hotplug_done();
  976. return ret;
  977. }
  978. static int check_hotplug_memory_range(u64 start, u64 size)
  979. {
  980. u64 start_pfn = PFN_DOWN(start);
  981. u64 nr_pages = size >> PAGE_SHIFT;
  982. /* Memory range must be aligned with section */
  983. if ((start_pfn & ~PAGE_SECTION_MASK) ||
  984. (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
  985. pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
  986. (unsigned long long)start,
  987. (unsigned long long)size);
  988. return -EINVAL;
  989. }
  990. return 0;
  991. }
  992. /*
  993. * If movable zone has already been setup, newly added memory should be check.
  994. * If its address is higher than movable zone, it should be added as movable.
  995. * Without this check, movable zone may overlap with other zone.
  996. */
  997. static int should_add_memory_movable(int nid, u64 start, u64 size)
  998. {
  999. unsigned long start_pfn = start >> PAGE_SHIFT;
  1000. pg_data_t *pgdat = NODE_DATA(nid);
  1001. struct zone *movable_zone = pgdat->node_zones + ZONE_MOVABLE;
  1002. if (zone_is_empty(movable_zone))
  1003. return 0;
  1004. if (movable_zone->zone_start_pfn <= start_pfn)
  1005. return 1;
  1006. return 0;
  1007. }
  1008. int zone_for_memory(int nid, u64 start, u64 size, int zone_default)
  1009. {
  1010. if (should_add_memory_movable(nid, start, size))
  1011. return ZONE_MOVABLE;
  1012. return zone_default;
  1013. }
  1014. /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
  1015. int __ref add_memory(int nid, u64 start, u64 size)
  1016. {
  1017. pg_data_t *pgdat = NULL;
  1018. bool new_pgdat;
  1019. bool new_node;
  1020. struct resource *res;
  1021. int ret;
  1022. ret = check_hotplug_memory_range(start, size);
  1023. if (ret)
  1024. return ret;
  1025. res = register_memory_resource(start, size);
  1026. ret = -EEXIST;
  1027. if (!res)
  1028. return ret;
  1029. { /* Stupid hack to suppress address-never-null warning */
  1030. void *p = NODE_DATA(nid);
  1031. new_pgdat = !p;
  1032. }
  1033. mem_hotplug_begin();
  1034. new_node = !node_online(nid);
  1035. if (new_node) {
  1036. pgdat = hotadd_new_pgdat(nid, start);
  1037. ret = -ENOMEM;
  1038. if (!pgdat)
  1039. goto error;
  1040. }
  1041. /* call arch's memory hotadd */
  1042. ret = arch_add_memory(nid, start, size);
  1043. if (ret < 0)
  1044. goto error;
  1045. /* we online node here. we can't roll back from here. */
  1046. node_set_online(nid);
  1047. if (new_node) {
  1048. ret = register_one_node(nid);
  1049. /*
  1050. * If sysfs file of new node can't create, cpu on the node
  1051. * can't be hot-added. There is no rollback way now.
  1052. * So, check by BUG_ON() to catch it reluctantly..
  1053. */
  1054. BUG_ON(ret);
  1055. }
  1056. /* create new memmap entry */
  1057. firmware_map_add_hotplug(start, start + size, "System RAM");
  1058. goto out;
  1059. error:
  1060. /* rollback pgdat allocation and others */
  1061. if (new_pgdat)
  1062. rollback_node_hotadd(nid, pgdat);
  1063. release_memory_resource(res);
  1064. out:
  1065. mem_hotplug_done();
  1066. return ret;
  1067. }
  1068. EXPORT_SYMBOL_GPL(add_memory);
  1069. #ifdef CONFIG_MEMORY_HOTREMOVE
  1070. /*
  1071. * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
  1072. * set and the size of the free page is given by page_order(). Using this,
  1073. * the function determines if the pageblock contains only free pages.
  1074. * Due to buddy contraints, a free page at least the size of a pageblock will
  1075. * be located at the start of the pageblock
  1076. */
  1077. static inline int pageblock_free(struct page *page)
  1078. {
  1079. return PageBuddy(page) && page_order(page) >= pageblock_order;
  1080. }
  1081. /* Return the start of the next active pageblock after a given page */
  1082. static struct page *next_active_pageblock(struct page *page)
  1083. {
  1084. /* Ensure the starting page is pageblock-aligned */
  1085. BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
  1086. /* If the entire pageblock is free, move to the end of free page */
  1087. if (pageblock_free(page)) {
  1088. int order;
  1089. /* be careful. we don't have locks, page_order can be changed.*/
  1090. order = page_order(page);
  1091. if ((order < MAX_ORDER) && (order >= pageblock_order))
  1092. return page + (1 << order);
  1093. }
  1094. return page + pageblock_nr_pages;
  1095. }
  1096. /* Checks if this range of memory is likely to be hot-removable. */
  1097. int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
  1098. {
  1099. struct page *page = pfn_to_page(start_pfn);
  1100. struct page *end_page = page + nr_pages;
  1101. /* Check the starting page of each pageblock within the range */
  1102. for (; page < end_page; page = next_active_pageblock(page)) {
  1103. if (!is_pageblock_removable_nolock(page))
  1104. return 0;
  1105. cond_resched();
  1106. }
  1107. /* All pageblocks in the memory block are likely to be hot-removable */
  1108. return 1;
  1109. }
  1110. /*
  1111. * Confirm all pages in a range [start, end) is belongs to the same zone.
  1112. */
  1113. int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
  1114. {
  1115. unsigned long pfn;
  1116. struct zone *zone = NULL;
  1117. struct page *page;
  1118. int i;
  1119. for (pfn = start_pfn;
  1120. pfn < end_pfn;
  1121. pfn += MAX_ORDER_NR_PAGES) {
  1122. i = 0;
  1123. /* This is just a CONFIG_HOLES_IN_ZONE check.*/
  1124. while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
  1125. i++;
  1126. if (i == MAX_ORDER_NR_PAGES)
  1127. continue;
  1128. page = pfn_to_page(pfn + i);
  1129. if (zone && page_zone(page) != zone)
  1130. return 0;
  1131. zone = page_zone(page);
  1132. }
  1133. return 1;
  1134. }
  1135. /*
  1136. * Scan pfn range [start,end) to find movable/migratable pages (LRU pages
  1137. * and hugepages). We scan pfn because it's much easier than scanning over
  1138. * linked list. This function returns the pfn of the first found movable
  1139. * page if it's found, otherwise 0.
  1140. */
  1141. static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
  1142. {
  1143. unsigned long pfn;
  1144. struct page *page;
  1145. for (pfn = start; pfn < end; pfn++) {
  1146. if (pfn_valid(pfn)) {
  1147. page = pfn_to_page(pfn);
  1148. if (PageLRU(page))
  1149. return pfn;
  1150. if (PageHuge(page)) {
  1151. if (page_huge_active(page))
  1152. return pfn;
  1153. else
  1154. pfn = round_up(pfn + 1,
  1155. 1 << compound_order(page)) - 1;
  1156. }
  1157. }
  1158. }
  1159. return 0;
  1160. }
  1161. #define NR_OFFLINE_AT_ONCE_PAGES (256)
  1162. static int
  1163. do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
  1164. {
  1165. unsigned long pfn;
  1166. struct page *page;
  1167. int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
  1168. int not_managed = 0;
  1169. int ret = 0;
  1170. LIST_HEAD(source);
  1171. for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
  1172. if (!pfn_valid(pfn))
  1173. continue;
  1174. page = pfn_to_page(pfn);
  1175. if (PageHuge(page)) {
  1176. struct page *head = compound_head(page);
  1177. pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
  1178. if (compound_order(head) > PFN_SECTION_SHIFT) {
  1179. ret = -EBUSY;
  1180. break;
  1181. }
  1182. if (isolate_huge_page(page, &source))
  1183. move_pages -= 1 << compound_order(head);
  1184. continue;
  1185. }
  1186. if (!get_page_unless_zero(page))
  1187. continue;
  1188. /*
  1189. * We can skip free pages. And we can only deal with pages on
  1190. * LRU.
  1191. */
  1192. ret = isolate_lru_page(page);
  1193. if (!ret) { /* Success */
  1194. put_page(page);
  1195. list_add_tail(&page->lru, &source);
  1196. move_pages--;
  1197. inc_zone_page_state(page, NR_ISOLATED_ANON +
  1198. page_is_file_cache(page));
  1199. } else {
  1200. #ifdef CONFIG_DEBUG_VM
  1201. printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
  1202. pfn);
  1203. dump_page(page, "failed to remove from LRU");
  1204. #endif
  1205. put_page(page);
  1206. /* Because we don't have big zone->lock. we should
  1207. check this again here. */
  1208. if (page_count(page)) {
  1209. not_managed++;
  1210. ret = -EBUSY;
  1211. break;
  1212. }
  1213. }
  1214. }
  1215. if (!list_empty(&source)) {
  1216. if (not_managed) {
  1217. putback_movable_pages(&source);
  1218. goto out;
  1219. }
  1220. /*
  1221. * alloc_migrate_target should be improooooved!!
  1222. * migrate_pages returns # of failed pages.
  1223. */
  1224. ret = migrate_pages(&source, alloc_migrate_target, NULL, 0,
  1225. MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
  1226. if (ret)
  1227. putback_movable_pages(&source);
  1228. }
  1229. out:
  1230. return ret;
  1231. }
  1232. /*
  1233. * remove from free_area[] and mark all as Reserved.
  1234. */
  1235. static int
  1236. offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
  1237. void *data)
  1238. {
  1239. __offline_isolated_pages(start, start + nr_pages);
  1240. return 0;
  1241. }
  1242. static void
  1243. offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
  1244. {
  1245. walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
  1246. offline_isolated_pages_cb);
  1247. }
  1248. /*
  1249. * Check all pages in range, recoreded as memory resource, are isolated.
  1250. */
  1251. static int
  1252. check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
  1253. void *data)
  1254. {
  1255. int ret;
  1256. long offlined = *(long *)data;
  1257. ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
  1258. offlined = nr_pages;
  1259. if (!ret)
  1260. *(long *)data += offlined;
  1261. return ret;
  1262. }
  1263. static long
  1264. check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
  1265. {
  1266. long offlined = 0;
  1267. int ret;
  1268. ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
  1269. check_pages_isolated_cb);
  1270. if (ret < 0)
  1271. offlined = (long)ret;
  1272. return offlined;
  1273. }
  1274. #ifdef CONFIG_MOVABLE_NODE
  1275. /*
  1276. * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
  1277. * normal memory.
  1278. */
  1279. static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
  1280. {
  1281. return true;
  1282. }
  1283. #else /* CONFIG_MOVABLE_NODE */
  1284. /* ensure the node has NORMAL memory if it is still online */
  1285. static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
  1286. {
  1287. struct pglist_data *pgdat = zone->zone_pgdat;
  1288. unsigned long present_pages = 0;
  1289. enum zone_type zt;
  1290. for (zt = 0; zt <= ZONE_NORMAL; zt++)
  1291. present_pages += pgdat->node_zones[zt].present_pages;
  1292. if (present_pages > nr_pages)
  1293. return true;
  1294. present_pages = 0;
  1295. for (; zt <= ZONE_MOVABLE; zt++)
  1296. present_pages += pgdat->node_zones[zt].present_pages;
  1297. /*
  1298. * we can't offline the last normal memory until all
  1299. * higher memory is offlined.
  1300. */
  1301. return present_pages == 0;
  1302. }
  1303. #endif /* CONFIG_MOVABLE_NODE */
  1304. static int __init cmdline_parse_movable_node(char *p)
  1305. {
  1306. #ifdef CONFIG_MOVABLE_NODE
  1307. /*
  1308. * Memory used by the kernel cannot be hot-removed because Linux
  1309. * cannot migrate the kernel pages. When memory hotplug is
  1310. * enabled, we should prevent memblock from allocating memory
  1311. * for the kernel.
  1312. *
  1313. * ACPI SRAT records all hotpluggable memory ranges. But before
  1314. * SRAT is parsed, we don't know about it.
  1315. *
  1316. * The kernel image is loaded into memory at very early time. We
  1317. * cannot prevent this anyway. So on NUMA system, we set any
  1318. * node the kernel resides in as un-hotpluggable.
  1319. *
  1320. * Since on modern servers, one node could have double-digit
  1321. * gigabytes memory, we can assume the memory around the kernel
  1322. * image is also un-hotpluggable. So before SRAT is parsed, just
  1323. * allocate memory near the kernel image to try the best to keep
  1324. * the kernel away from hotpluggable memory.
  1325. */
  1326. memblock_set_bottom_up(true);
  1327. movable_node_enabled = true;
  1328. #else
  1329. pr_warn("movable_node option not supported\n");
  1330. #endif
  1331. return 0;
  1332. }
  1333. early_param("movable_node", cmdline_parse_movable_node);
  1334. /* check which state of node_states will be changed when offline memory */
  1335. static void node_states_check_changes_offline(unsigned long nr_pages,
  1336. struct zone *zone, struct memory_notify *arg)
  1337. {
  1338. struct pglist_data *pgdat = zone->zone_pgdat;
  1339. unsigned long present_pages = 0;
  1340. enum zone_type zt, zone_last = ZONE_NORMAL;
  1341. /*
  1342. * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
  1343. * contains nodes which have zones of 0...ZONE_NORMAL,
  1344. * set zone_last to ZONE_NORMAL.
  1345. *
  1346. * If we don't have HIGHMEM nor movable node,
  1347. * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
  1348. * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
  1349. */
  1350. if (N_MEMORY == N_NORMAL_MEMORY)
  1351. zone_last = ZONE_MOVABLE;
  1352. /*
  1353. * check whether node_states[N_NORMAL_MEMORY] will be changed.
  1354. * If the memory to be offline is in a zone of 0...zone_last,
  1355. * and it is the last present memory, 0...zone_last will
  1356. * become empty after offline , thus we can determind we will
  1357. * need to clear the node from node_states[N_NORMAL_MEMORY].
  1358. */
  1359. for (zt = 0; zt <= zone_last; zt++)
  1360. present_pages += pgdat->node_zones[zt].present_pages;
  1361. if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
  1362. arg->status_change_nid_normal = zone_to_nid(zone);
  1363. else
  1364. arg->status_change_nid_normal = -1;
  1365. #ifdef CONFIG_HIGHMEM
  1366. /*
  1367. * If we have movable node, node_states[N_HIGH_MEMORY]
  1368. * contains nodes which have zones of 0...ZONE_HIGHMEM,
  1369. * set zone_last to ZONE_HIGHMEM.
  1370. *
  1371. * If we don't have movable node, node_states[N_NORMAL_MEMORY]
  1372. * contains nodes which have zones of 0...ZONE_MOVABLE,
  1373. * set zone_last to ZONE_MOVABLE.
  1374. */
  1375. zone_last = ZONE_HIGHMEM;
  1376. if (N_MEMORY == N_HIGH_MEMORY)
  1377. zone_last = ZONE_MOVABLE;
  1378. for (; zt <= zone_last; zt++)
  1379. present_pages += pgdat->node_zones[zt].present_pages;
  1380. if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
  1381. arg->status_change_nid_high = zone_to_nid(zone);
  1382. else
  1383. arg->status_change_nid_high = -1;
  1384. #else
  1385. arg->status_change_nid_high = arg->status_change_nid_normal;
  1386. #endif
  1387. /*
  1388. * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
  1389. */
  1390. zone_last = ZONE_MOVABLE;
  1391. /*
  1392. * check whether node_states[N_HIGH_MEMORY] will be changed
  1393. * If we try to offline the last present @nr_pages from the node,
  1394. * we can determind we will need to clear the node from
  1395. * node_states[N_HIGH_MEMORY].
  1396. */
  1397. for (; zt <= zone_last; zt++)
  1398. present_pages += pgdat->node_zones[zt].present_pages;
  1399. if (nr_pages >= present_pages)
  1400. arg->status_change_nid = zone_to_nid(zone);
  1401. else
  1402. arg->status_change_nid = -1;
  1403. }
  1404. static void node_states_clear_node(int node, struct memory_notify *arg)
  1405. {
  1406. if (arg->status_change_nid_normal >= 0)
  1407. node_clear_state(node, N_NORMAL_MEMORY);
  1408. if ((N_MEMORY != N_NORMAL_MEMORY) &&
  1409. (arg->status_change_nid_high >= 0))
  1410. node_clear_state(node, N_HIGH_MEMORY);
  1411. if ((N_MEMORY != N_HIGH_MEMORY) &&
  1412. (arg->status_change_nid >= 0))
  1413. node_clear_state(node, N_MEMORY);
  1414. }
  1415. static int __ref __offline_pages(unsigned long start_pfn,
  1416. unsigned long end_pfn, unsigned long timeout)
  1417. {
  1418. unsigned long pfn, nr_pages, expire;
  1419. long offlined_pages;
  1420. int ret, drain, retry_max, node;
  1421. unsigned long flags;
  1422. struct zone *zone;
  1423. struct memory_notify arg;
  1424. /* at least, alignment against pageblock is necessary */
  1425. if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
  1426. return -EINVAL;
  1427. if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
  1428. return -EINVAL;
  1429. /* This makes hotplug much easier...and readable.
  1430. we assume this for now. .*/
  1431. if (!test_pages_in_a_zone(start_pfn, end_pfn))
  1432. return -EINVAL;
  1433. zone = page_zone(pfn_to_page(start_pfn));
  1434. node = zone_to_nid(zone);
  1435. nr_pages = end_pfn - start_pfn;
  1436. if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
  1437. return -EINVAL;
  1438. /* set above range as isolated */
  1439. ret = start_isolate_page_range(start_pfn, end_pfn,
  1440. MIGRATE_MOVABLE, true);
  1441. if (ret)
  1442. return ret;
  1443. arg.start_pfn = start_pfn;
  1444. arg.nr_pages = nr_pages;
  1445. node_states_check_changes_offline(nr_pages, zone, &arg);
  1446. ret = memory_notify(MEM_GOING_OFFLINE, &arg);
  1447. ret = notifier_to_errno(ret);
  1448. if (ret)
  1449. goto failed_removal;
  1450. pfn = start_pfn;
  1451. expire = jiffies + timeout;
  1452. drain = 0;
  1453. retry_max = 5;
  1454. repeat:
  1455. /* start memory hot removal */
  1456. ret = -EAGAIN;
  1457. if (time_after(jiffies, expire))
  1458. goto failed_removal;
  1459. ret = -EINTR;
  1460. if (signal_pending(current))
  1461. goto failed_removal;
  1462. ret = 0;
  1463. if (drain) {
  1464. lru_add_drain_all();
  1465. cond_resched();
  1466. drain_all_pages(zone);
  1467. }
  1468. pfn = scan_movable_pages(start_pfn, end_pfn);
  1469. if (pfn) { /* We have movable pages */
  1470. ret = do_migrate_range(pfn, end_pfn);
  1471. if (!ret) {
  1472. drain = 1;
  1473. goto repeat;
  1474. } else {
  1475. if (ret < 0)
  1476. if (--retry_max == 0)
  1477. goto failed_removal;
  1478. yield();
  1479. drain = 1;
  1480. goto repeat;
  1481. }
  1482. }
  1483. /* drain all zone's lru pagevec, this is asynchronous... */
  1484. lru_add_drain_all();
  1485. yield();
  1486. /* drain pcp pages, this is synchronous. */
  1487. drain_all_pages(zone);
  1488. /*
  1489. * dissolve free hugepages in the memory block before doing offlining
  1490. * actually in order to make hugetlbfs's object counting consistent.
  1491. */
  1492. dissolve_free_huge_pages(start_pfn, end_pfn);
  1493. /* check again */
  1494. offlined_pages = check_pages_isolated(start_pfn, end_pfn);
  1495. if (offlined_pages < 0) {
  1496. ret = -EBUSY;
  1497. goto failed_removal;
  1498. }
  1499. printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
  1500. /* Ok, all of our target is isolated.
  1501. We cannot do rollback at this point. */
  1502. offline_isolated_pages(start_pfn, end_pfn);
  1503. /* reset pagetype flags and makes migrate type to be MOVABLE */
  1504. undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
  1505. /* removal success */
  1506. adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
  1507. zone->present_pages -= offlined_pages;
  1508. pgdat_resize_lock(zone->zone_pgdat, &flags);
  1509. zone->zone_pgdat->node_present_pages -= offlined_pages;
  1510. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  1511. init_per_zone_wmark_min();
  1512. if (!populated_zone(zone)) {
  1513. zone_pcp_reset(zone);
  1514. mutex_lock(&zonelists_mutex);
  1515. build_all_zonelists(NULL, NULL);
  1516. mutex_unlock(&zonelists_mutex);
  1517. } else
  1518. zone_pcp_update(zone);
  1519. node_states_clear_node(node, &arg);
  1520. if (arg.status_change_nid >= 0)
  1521. kswapd_stop(node);
  1522. vm_total_pages = nr_free_pagecache_pages();
  1523. writeback_set_ratelimit();
  1524. memory_notify(MEM_OFFLINE, &arg);
  1525. return 0;
  1526. failed_removal:
  1527. printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
  1528. (unsigned long long) start_pfn << PAGE_SHIFT,
  1529. ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
  1530. memory_notify(MEM_CANCEL_OFFLINE, &arg);
  1531. /* pushback to free area */
  1532. undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
  1533. return ret;
  1534. }
  1535. /* Must be protected by mem_hotplug_begin() */
  1536. int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
  1537. {
  1538. return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
  1539. }
  1540. #endif /* CONFIG_MEMORY_HOTREMOVE */
  1541. /**
  1542. * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
  1543. * @start_pfn: start pfn of the memory range
  1544. * @end_pfn: end pfn of the memory range
  1545. * @arg: argument passed to func
  1546. * @func: callback for each memory section walked
  1547. *
  1548. * This function walks through all present mem sections in range
  1549. * [start_pfn, end_pfn) and call func on each mem section.
  1550. *
  1551. * Returns the return value of func.
  1552. */
  1553. int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
  1554. void *arg, int (*func)(struct memory_block *, void *))
  1555. {
  1556. struct memory_block *mem = NULL;
  1557. struct mem_section *section;
  1558. unsigned long pfn, section_nr;
  1559. int ret;
  1560. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  1561. section_nr = pfn_to_section_nr(pfn);
  1562. if (!present_section_nr(section_nr))
  1563. continue;
  1564. section = __nr_to_section(section_nr);
  1565. /* same memblock? */
  1566. if (mem)
  1567. if ((section_nr >= mem->start_section_nr) &&
  1568. (section_nr <= mem->end_section_nr))
  1569. continue;
  1570. mem = find_memory_block_hinted(section, mem);
  1571. if (!mem)
  1572. continue;
  1573. ret = func(mem, arg);
  1574. if (ret) {
  1575. kobject_put(&mem->dev.kobj);
  1576. return ret;
  1577. }
  1578. }
  1579. if (mem)
  1580. kobject_put(&mem->dev.kobj);
  1581. return 0;
  1582. }
  1583. #ifdef CONFIG_MEMORY_HOTREMOVE
  1584. static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
  1585. {
  1586. int ret = !is_memblock_offlined(mem);
  1587. if (unlikely(ret)) {
  1588. phys_addr_t beginpa, endpa;
  1589. beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
  1590. endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
  1591. pr_warn("removing memory fails, because memory "
  1592. "[%pa-%pa] is onlined\n",
  1593. &beginpa, &endpa);
  1594. }
  1595. return ret;
  1596. }
  1597. static int check_cpu_on_node(pg_data_t *pgdat)
  1598. {
  1599. int cpu;
  1600. for_each_present_cpu(cpu) {
  1601. if (cpu_to_node(cpu) == pgdat->node_id)
  1602. /*
  1603. * the cpu on this node isn't removed, and we can't
  1604. * offline this node.
  1605. */
  1606. return -EBUSY;
  1607. }
  1608. return 0;
  1609. }
  1610. static void unmap_cpu_on_node(pg_data_t *pgdat)
  1611. {
  1612. #ifdef CONFIG_ACPI_NUMA
  1613. int cpu;
  1614. for_each_possible_cpu(cpu)
  1615. if (cpu_to_node(cpu) == pgdat->node_id)
  1616. numa_clear_node(cpu);
  1617. #endif
  1618. }
  1619. static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
  1620. {
  1621. int ret;
  1622. ret = check_cpu_on_node(pgdat);
  1623. if (ret)
  1624. return ret;
  1625. /*
  1626. * the node will be offlined when we come here, so we can clear
  1627. * the cpu_to_node() now.
  1628. */
  1629. unmap_cpu_on_node(pgdat);
  1630. return 0;
  1631. }
  1632. /**
  1633. * try_offline_node
  1634. *
  1635. * Offline a node if all memory sections and cpus of the node are removed.
  1636. *
  1637. * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
  1638. * and online/offline operations before this call.
  1639. */
  1640. void try_offline_node(int nid)
  1641. {
  1642. pg_data_t *pgdat = NODE_DATA(nid);
  1643. unsigned long start_pfn = pgdat->node_start_pfn;
  1644. unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
  1645. unsigned long pfn;
  1646. int i;
  1647. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  1648. unsigned long section_nr = pfn_to_section_nr(pfn);
  1649. if (!present_section_nr(section_nr))
  1650. continue;
  1651. if (pfn_to_nid(pfn) != nid)
  1652. continue;
  1653. /*
  1654. * some memory sections of this node are not removed, and we
  1655. * can't offline node now.
  1656. */
  1657. return;
  1658. }
  1659. if (check_and_unmap_cpu_on_node(pgdat))
  1660. return;
  1661. /*
  1662. * all memory/cpu of this node are removed, we can offline this
  1663. * node now.
  1664. */
  1665. node_set_offline(nid);
  1666. unregister_one_node(nid);
  1667. /* free waittable in each zone */
  1668. for (i = 0; i < MAX_NR_ZONES; i++) {
  1669. struct zone *zone = pgdat->node_zones + i;
  1670. /*
  1671. * wait_table may be allocated from boot memory,
  1672. * here only free if it's allocated by vmalloc.
  1673. */
  1674. if (is_vmalloc_addr(zone->wait_table)) {
  1675. vfree(zone->wait_table);
  1676. zone->wait_table = NULL;
  1677. }
  1678. }
  1679. }
  1680. EXPORT_SYMBOL(try_offline_node);
  1681. /**
  1682. * remove_memory
  1683. *
  1684. * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
  1685. * and online/offline operations before this call, as required by
  1686. * try_offline_node().
  1687. */
  1688. void __ref remove_memory(int nid, u64 start, u64 size)
  1689. {
  1690. int ret;
  1691. BUG_ON(check_hotplug_memory_range(start, size));
  1692. mem_hotplug_begin();
  1693. /*
  1694. * All memory blocks must be offlined before removing memory. Check
  1695. * whether all memory blocks in question are offline and trigger a BUG()
  1696. * if this is not the case.
  1697. */
  1698. ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
  1699. check_memblock_offlined_cb);
  1700. if (ret)
  1701. BUG();
  1702. /* remove memmap entry */
  1703. firmware_map_remove(start, start + size, "System RAM");
  1704. arch_remove_memory(start, size);
  1705. try_offline_node(nid);
  1706. mem_hotplug_done();
  1707. }
  1708. EXPORT_SYMBOL_GPL(remove_memory);
  1709. #endif /* CONFIG_MEMORY_HOTREMOVE */