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