memory_hotplug.c 44 KB

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