memory_hotplug.c 47 KB

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