memory_hotplug.c 47 KB

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