memory_hotplug.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427
  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. #ifndef CONFIG_SPARSEMEM_VMEMMAP
  80. static 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. }
  109. }
  110. static void register_page_bootmem_info_section(unsigned long start_pfn)
  111. {
  112. unsigned long *usemap, mapsize, section_nr, i;
  113. struct mem_section *ms;
  114. struct page *page, *memmap;
  115. section_nr = pfn_to_section_nr(start_pfn);
  116. ms = __nr_to_section(section_nr);
  117. /* Get section's memmap address */
  118. memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
  119. /*
  120. * Get page for the memmap's phys address
  121. * XXX: need more consideration for sparse_vmemmap...
  122. */
  123. page = virt_to_page(memmap);
  124. mapsize = sizeof(struct page) * PAGES_PER_SECTION;
  125. mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
  126. /* remember memmap's page */
  127. for (i = 0; i < mapsize; i++, page++)
  128. get_page_bootmem(section_nr, page, SECTION_INFO);
  129. usemap = __nr_to_section(section_nr)->pageblock_flags;
  130. page = virt_to_page(usemap);
  131. mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
  132. for (i = 0; i < mapsize; i++, page++)
  133. get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
  134. }
  135. void register_page_bootmem_info_node(struct pglist_data *pgdat)
  136. {
  137. unsigned long i, pfn, end_pfn, nr_pages;
  138. int node = pgdat->node_id;
  139. struct page *page;
  140. struct zone *zone;
  141. nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
  142. page = virt_to_page(pgdat);
  143. for (i = 0; i < nr_pages; i++, page++)
  144. get_page_bootmem(node, page, NODE_INFO);
  145. zone = &pgdat->node_zones[0];
  146. for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
  147. if (zone->wait_table) {
  148. nr_pages = zone->wait_table_hash_nr_entries
  149. * sizeof(wait_queue_head_t);
  150. nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
  151. page = virt_to_page(zone->wait_table);
  152. for (i = 0; i < nr_pages; i++, page++)
  153. get_page_bootmem(node, page, NODE_INFO);
  154. }
  155. }
  156. pfn = pgdat->node_start_pfn;
  157. end_pfn = pfn + pgdat->node_spanned_pages;
  158. /* register_section info */
  159. for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  160. /*
  161. * Some platforms can assign the same pfn to multiple nodes - on
  162. * node0 as well as nodeN. To avoid registering a pfn against
  163. * multiple nodes we check that this pfn does not already
  164. * reside in some other node.
  165. */
  166. if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
  167. register_page_bootmem_info_section(pfn);
  168. }
  169. }
  170. #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
  171. static void grow_zone_span(struct zone *zone, unsigned long start_pfn,
  172. unsigned long end_pfn)
  173. {
  174. unsigned long old_zone_end_pfn;
  175. zone_span_writelock(zone);
  176. old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
  177. if (!zone->spanned_pages || start_pfn < zone->zone_start_pfn)
  178. zone->zone_start_pfn = start_pfn;
  179. zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
  180. zone->zone_start_pfn;
  181. zone_span_writeunlock(zone);
  182. }
  183. static void resize_zone(struct zone *zone, unsigned long start_pfn,
  184. unsigned long end_pfn)
  185. {
  186. zone_span_writelock(zone);
  187. if (end_pfn - start_pfn) {
  188. zone->zone_start_pfn = start_pfn;
  189. zone->spanned_pages = end_pfn - start_pfn;
  190. } else {
  191. /*
  192. * make it consist as free_area_init_core(),
  193. * if spanned_pages = 0, then keep start_pfn = 0
  194. */
  195. zone->zone_start_pfn = 0;
  196. zone->spanned_pages = 0;
  197. }
  198. zone_span_writeunlock(zone);
  199. }
  200. static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
  201. unsigned long end_pfn)
  202. {
  203. enum zone_type zid = zone_idx(zone);
  204. int nid = zone->zone_pgdat->node_id;
  205. unsigned long pfn;
  206. for (pfn = start_pfn; pfn < end_pfn; pfn++)
  207. set_page_links(pfn_to_page(pfn), zid, nid, pfn);
  208. }
  209. static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
  210. unsigned long start_pfn, unsigned long end_pfn)
  211. {
  212. int ret;
  213. unsigned long flags;
  214. unsigned long z1_start_pfn;
  215. if (!z1->wait_table) {
  216. ret = init_currently_empty_zone(z1, start_pfn,
  217. end_pfn - start_pfn, MEMMAP_HOTPLUG);
  218. if (ret)
  219. return ret;
  220. }
  221. pgdat_resize_lock(z1->zone_pgdat, &flags);
  222. /* can't move pfns which are higher than @z2 */
  223. if (end_pfn > z2->zone_start_pfn + z2->spanned_pages)
  224. goto out_fail;
  225. /* the move out part mast at the left most of @z2 */
  226. if (start_pfn > z2->zone_start_pfn)
  227. goto out_fail;
  228. /* must included/overlap */
  229. if (end_pfn <= z2->zone_start_pfn)
  230. goto out_fail;
  231. /* use start_pfn for z1's start_pfn if z1 is empty */
  232. if (z1->spanned_pages)
  233. z1_start_pfn = z1->zone_start_pfn;
  234. else
  235. z1_start_pfn = start_pfn;
  236. resize_zone(z1, z1_start_pfn, end_pfn);
  237. resize_zone(z2, end_pfn, z2->zone_start_pfn + z2->spanned_pages);
  238. pgdat_resize_unlock(z1->zone_pgdat, &flags);
  239. fix_zone_id(z1, start_pfn, end_pfn);
  240. return 0;
  241. out_fail:
  242. pgdat_resize_unlock(z1->zone_pgdat, &flags);
  243. return -1;
  244. }
  245. static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
  246. unsigned long start_pfn, unsigned long end_pfn)
  247. {
  248. int ret;
  249. unsigned long flags;
  250. unsigned long z2_end_pfn;
  251. if (!z2->wait_table) {
  252. ret = init_currently_empty_zone(z2, start_pfn,
  253. end_pfn - start_pfn, MEMMAP_HOTPLUG);
  254. if (ret)
  255. return ret;
  256. }
  257. pgdat_resize_lock(z1->zone_pgdat, &flags);
  258. /* can't move pfns which are lower than @z1 */
  259. if (z1->zone_start_pfn > start_pfn)
  260. goto out_fail;
  261. /* the move out part mast at the right most of @z1 */
  262. if (z1->zone_start_pfn + z1->spanned_pages > end_pfn)
  263. goto out_fail;
  264. /* must included/overlap */
  265. if (start_pfn >= z1->zone_start_pfn + z1->spanned_pages)
  266. goto out_fail;
  267. /* use end_pfn for z2's end_pfn if z2 is empty */
  268. if (z2->spanned_pages)
  269. z2_end_pfn = z2->zone_start_pfn + z2->spanned_pages;
  270. else
  271. z2_end_pfn = end_pfn;
  272. resize_zone(z1, z1->zone_start_pfn, start_pfn);
  273. resize_zone(z2, start_pfn, z2_end_pfn);
  274. pgdat_resize_unlock(z1->zone_pgdat, &flags);
  275. fix_zone_id(z2, start_pfn, end_pfn);
  276. return 0;
  277. out_fail:
  278. pgdat_resize_unlock(z1->zone_pgdat, &flags);
  279. return -1;
  280. }
  281. static void grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
  282. unsigned long end_pfn)
  283. {
  284. unsigned long old_pgdat_end_pfn =
  285. pgdat->node_start_pfn + pgdat->node_spanned_pages;
  286. if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
  287. pgdat->node_start_pfn = start_pfn;
  288. pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
  289. pgdat->node_start_pfn;
  290. }
  291. static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
  292. {
  293. struct pglist_data *pgdat = zone->zone_pgdat;
  294. int nr_pages = PAGES_PER_SECTION;
  295. int nid = pgdat->node_id;
  296. int zone_type;
  297. unsigned long flags;
  298. zone_type = zone - pgdat->node_zones;
  299. if (!zone->wait_table) {
  300. int ret;
  301. ret = init_currently_empty_zone(zone, phys_start_pfn,
  302. nr_pages, MEMMAP_HOTPLUG);
  303. if (ret)
  304. return ret;
  305. }
  306. pgdat_resize_lock(zone->zone_pgdat, &flags);
  307. grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
  308. grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
  309. phys_start_pfn + nr_pages);
  310. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  311. memmap_init_zone(nr_pages, nid, zone_type,
  312. phys_start_pfn, MEMMAP_HOTPLUG);
  313. return 0;
  314. }
  315. static int __meminit __add_section(int nid, struct zone *zone,
  316. unsigned long phys_start_pfn)
  317. {
  318. int nr_pages = PAGES_PER_SECTION;
  319. int ret;
  320. if (pfn_valid(phys_start_pfn))
  321. return -EEXIST;
  322. ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
  323. if (ret < 0)
  324. return ret;
  325. ret = __add_zone(zone, phys_start_pfn);
  326. if (ret < 0)
  327. return ret;
  328. return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
  329. }
  330. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  331. static int __remove_section(struct zone *zone, struct mem_section *ms)
  332. {
  333. /*
  334. * XXX: Freeing memmap with vmemmap is not implement yet.
  335. * This should be removed later.
  336. */
  337. return -EBUSY;
  338. }
  339. #else
  340. static int __remove_section(struct zone *zone, struct mem_section *ms)
  341. {
  342. unsigned long flags;
  343. struct pglist_data *pgdat = zone->zone_pgdat;
  344. int ret = -EINVAL;
  345. if (!valid_section(ms))
  346. return ret;
  347. ret = unregister_memory_section(ms);
  348. if (ret)
  349. return ret;
  350. pgdat_resize_lock(pgdat, &flags);
  351. sparse_remove_one_section(zone, ms);
  352. pgdat_resize_unlock(pgdat, &flags);
  353. return 0;
  354. }
  355. #endif
  356. /*
  357. * Reasonably generic function for adding memory. It is
  358. * expected that archs that support memory hotplug will
  359. * call this function after deciding the zone to which to
  360. * add the new pages.
  361. */
  362. int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
  363. unsigned long nr_pages)
  364. {
  365. unsigned long i;
  366. int err = 0;
  367. int start_sec, end_sec;
  368. /* during initialize mem_map, align hot-added range to section */
  369. start_sec = pfn_to_section_nr(phys_start_pfn);
  370. end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
  371. for (i = start_sec; i <= end_sec; i++) {
  372. err = __add_section(nid, zone, i << PFN_SECTION_SHIFT);
  373. /*
  374. * EEXIST is finally dealt with by ioresource collision
  375. * check. see add_memory() => register_memory_resource()
  376. * Warning will be printed if there is collision.
  377. */
  378. if (err && (err != -EEXIST))
  379. break;
  380. err = 0;
  381. }
  382. return err;
  383. }
  384. EXPORT_SYMBOL_GPL(__add_pages);
  385. /**
  386. * __remove_pages() - remove sections of pages from a zone
  387. * @zone: zone from which pages need to be removed
  388. * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
  389. * @nr_pages: number of pages to remove (must be multiple of section size)
  390. *
  391. * Generic helper function to remove section mappings and sysfs entries
  392. * for the section of the memory we are removing. Caller needs to make
  393. * sure that pages are marked reserved and zones are adjust properly by
  394. * calling offline_pages().
  395. */
  396. int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
  397. unsigned long nr_pages)
  398. {
  399. unsigned long i, ret = 0;
  400. int sections_to_remove;
  401. /*
  402. * We can only remove entire sections
  403. */
  404. BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
  405. BUG_ON(nr_pages % PAGES_PER_SECTION);
  406. release_mem_region(phys_start_pfn << PAGE_SHIFT, nr_pages * PAGE_SIZE);
  407. sections_to_remove = nr_pages / PAGES_PER_SECTION;
  408. for (i = 0; i < sections_to_remove; i++) {
  409. unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
  410. ret = __remove_section(zone, __pfn_to_section(pfn));
  411. if (ret)
  412. break;
  413. }
  414. return ret;
  415. }
  416. EXPORT_SYMBOL_GPL(__remove_pages);
  417. int set_online_page_callback(online_page_callback_t callback)
  418. {
  419. int rc = -EINVAL;
  420. lock_memory_hotplug();
  421. if (online_page_callback == generic_online_page) {
  422. online_page_callback = callback;
  423. rc = 0;
  424. }
  425. unlock_memory_hotplug();
  426. return rc;
  427. }
  428. EXPORT_SYMBOL_GPL(set_online_page_callback);
  429. int restore_online_page_callback(online_page_callback_t callback)
  430. {
  431. int rc = -EINVAL;
  432. lock_memory_hotplug();
  433. if (online_page_callback == callback) {
  434. online_page_callback = generic_online_page;
  435. rc = 0;
  436. }
  437. unlock_memory_hotplug();
  438. return rc;
  439. }
  440. EXPORT_SYMBOL_GPL(restore_online_page_callback);
  441. void __online_page_set_limits(struct page *page)
  442. {
  443. unsigned long pfn = page_to_pfn(page);
  444. if (pfn >= num_physpages)
  445. num_physpages = pfn + 1;
  446. }
  447. EXPORT_SYMBOL_GPL(__online_page_set_limits);
  448. void __online_page_increment_counters(struct page *page)
  449. {
  450. totalram_pages++;
  451. #ifdef CONFIG_HIGHMEM
  452. if (PageHighMem(page))
  453. totalhigh_pages++;
  454. #endif
  455. }
  456. EXPORT_SYMBOL_GPL(__online_page_increment_counters);
  457. void __online_page_free(struct page *page)
  458. {
  459. ClearPageReserved(page);
  460. init_page_count(page);
  461. __free_page(page);
  462. }
  463. EXPORT_SYMBOL_GPL(__online_page_free);
  464. static void generic_online_page(struct page *page)
  465. {
  466. __online_page_set_limits(page);
  467. __online_page_increment_counters(page);
  468. __online_page_free(page);
  469. }
  470. static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
  471. void *arg)
  472. {
  473. unsigned long i;
  474. unsigned long onlined_pages = *(unsigned long *)arg;
  475. struct page *page;
  476. if (PageReserved(pfn_to_page(start_pfn)))
  477. for (i = 0; i < nr_pages; i++) {
  478. page = pfn_to_page(start_pfn + i);
  479. (*online_page_callback)(page);
  480. onlined_pages++;
  481. }
  482. *(unsigned long *)arg = onlined_pages;
  483. return 0;
  484. }
  485. #ifdef CONFIG_MOVABLE_NODE
  486. /* when CONFIG_MOVABLE_NODE, we allow online node don't have normal memory */
  487. static bool can_online_high_movable(struct zone *zone)
  488. {
  489. return true;
  490. }
  491. #else /* #ifdef CONFIG_MOVABLE_NODE */
  492. /* ensure every online node has NORMAL memory */
  493. static bool can_online_high_movable(struct zone *zone)
  494. {
  495. return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
  496. }
  497. #endif /* #ifdef CONFIG_MOVABLE_NODE */
  498. /* check which state of node_states will be changed when online memory */
  499. static void node_states_check_changes_online(unsigned long nr_pages,
  500. struct zone *zone, struct memory_notify *arg)
  501. {
  502. int nid = zone_to_nid(zone);
  503. enum zone_type zone_last = ZONE_NORMAL;
  504. /*
  505. * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
  506. * contains nodes which have zones of 0...ZONE_NORMAL,
  507. * set zone_last to ZONE_NORMAL.
  508. *
  509. * If we don't have HIGHMEM nor movable node,
  510. * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
  511. * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
  512. */
  513. if (N_MEMORY == N_NORMAL_MEMORY)
  514. zone_last = ZONE_MOVABLE;
  515. /*
  516. * if the memory to be online is in a zone of 0...zone_last, and
  517. * the zones of 0...zone_last don't have memory before online, we will
  518. * need to set the node to node_states[N_NORMAL_MEMORY] after
  519. * the memory is online.
  520. */
  521. if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
  522. arg->status_change_nid_normal = nid;
  523. else
  524. arg->status_change_nid_normal = -1;
  525. #ifdef CONFIG_HIGHMEM
  526. /*
  527. * If we have movable node, node_states[N_HIGH_MEMORY]
  528. * contains nodes which have zones of 0...ZONE_HIGHMEM,
  529. * set zone_last to ZONE_HIGHMEM.
  530. *
  531. * If we don't have movable node, node_states[N_NORMAL_MEMORY]
  532. * contains nodes which have zones of 0...ZONE_MOVABLE,
  533. * set zone_last to ZONE_MOVABLE.
  534. */
  535. zone_last = ZONE_HIGHMEM;
  536. if (N_MEMORY == N_HIGH_MEMORY)
  537. zone_last = ZONE_MOVABLE;
  538. if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
  539. arg->status_change_nid_high = nid;
  540. else
  541. arg->status_change_nid_high = -1;
  542. #else
  543. arg->status_change_nid_high = arg->status_change_nid_normal;
  544. #endif
  545. /*
  546. * if the node don't have memory befor online, we will need to
  547. * set the node to node_states[N_MEMORY] after the memory
  548. * is online.
  549. */
  550. if (!node_state(nid, N_MEMORY))
  551. arg->status_change_nid = nid;
  552. else
  553. arg->status_change_nid = -1;
  554. }
  555. static void node_states_set_node(int node, struct memory_notify *arg)
  556. {
  557. if (arg->status_change_nid_normal >= 0)
  558. node_set_state(node, N_NORMAL_MEMORY);
  559. if (arg->status_change_nid_high >= 0)
  560. node_set_state(node, N_HIGH_MEMORY);
  561. node_set_state(node, N_MEMORY);
  562. }
  563. int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
  564. {
  565. unsigned long onlined_pages = 0;
  566. struct zone *zone;
  567. int need_zonelists_rebuild = 0;
  568. int nid;
  569. int ret;
  570. struct memory_notify arg;
  571. lock_memory_hotplug();
  572. /*
  573. * This doesn't need a lock to do pfn_to_page().
  574. * The section can't be removed here because of the
  575. * memory_block->state_mutex.
  576. */
  577. zone = page_zone(pfn_to_page(pfn));
  578. if ((zone_idx(zone) > ZONE_NORMAL || online_type == ONLINE_MOVABLE) &&
  579. !can_online_high_movable(zone)) {
  580. unlock_memory_hotplug();
  581. return -1;
  582. }
  583. if (online_type == ONLINE_KERNEL && zone_idx(zone) == ZONE_MOVABLE) {
  584. if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages)) {
  585. unlock_memory_hotplug();
  586. return -1;
  587. }
  588. }
  589. if (online_type == ONLINE_MOVABLE && zone_idx(zone) == ZONE_MOVABLE - 1) {
  590. if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages)) {
  591. unlock_memory_hotplug();
  592. return -1;
  593. }
  594. }
  595. /* Previous code may changed the zone of the pfn range */
  596. zone = page_zone(pfn_to_page(pfn));
  597. arg.start_pfn = pfn;
  598. arg.nr_pages = nr_pages;
  599. node_states_check_changes_online(nr_pages, zone, &arg);
  600. nid = page_to_nid(pfn_to_page(pfn));
  601. ret = memory_notify(MEM_GOING_ONLINE, &arg);
  602. ret = notifier_to_errno(ret);
  603. if (ret) {
  604. memory_notify(MEM_CANCEL_ONLINE, &arg);
  605. unlock_memory_hotplug();
  606. return ret;
  607. }
  608. /*
  609. * If this zone is not populated, then it is not in zonelist.
  610. * This means the page allocator ignores this zone.
  611. * So, zonelist must be updated after online.
  612. */
  613. mutex_lock(&zonelists_mutex);
  614. if (!populated_zone(zone)) {
  615. need_zonelists_rebuild = 1;
  616. build_all_zonelists(NULL, zone);
  617. }
  618. ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
  619. online_pages_range);
  620. if (ret) {
  621. if (need_zonelists_rebuild)
  622. zone_pcp_reset(zone);
  623. mutex_unlock(&zonelists_mutex);
  624. printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
  625. (unsigned long long) pfn << PAGE_SHIFT,
  626. (((unsigned long long) pfn + nr_pages)
  627. << PAGE_SHIFT) - 1);
  628. memory_notify(MEM_CANCEL_ONLINE, &arg);
  629. unlock_memory_hotplug();
  630. return ret;
  631. }
  632. zone->managed_pages += onlined_pages;
  633. zone->present_pages += onlined_pages;
  634. zone->zone_pgdat->node_present_pages += onlined_pages;
  635. if (onlined_pages) {
  636. node_states_set_node(zone_to_nid(zone), &arg);
  637. if (need_zonelists_rebuild)
  638. build_all_zonelists(NULL, NULL);
  639. else
  640. zone_pcp_update(zone);
  641. }
  642. mutex_unlock(&zonelists_mutex);
  643. init_per_zone_wmark_min();
  644. if (onlined_pages)
  645. kswapd_run(zone_to_nid(zone));
  646. vm_total_pages = nr_free_pagecache_pages();
  647. writeback_set_ratelimit();
  648. if (onlined_pages)
  649. memory_notify(MEM_ONLINE, &arg);
  650. unlock_memory_hotplug();
  651. return 0;
  652. }
  653. #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
  654. /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
  655. static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
  656. {
  657. struct pglist_data *pgdat;
  658. unsigned long zones_size[MAX_NR_ZONES] = {0};
  659. unsigned long zholes_size[MAX_NR_ZONES] = {0};
  660. unsigned long start_pfn = start >> PAGE_SHIFT;
  661. pgdat = arch_alloc_nodedata(nid);
  662. if (!pgdat)
  663. return NULL;
  664. arch_refresh_nodedata(nid, pgdat);
  665. /* we can use NODE_DATA(nid) from here */
  666. /* init node's zones as empty zones, we don't have any present pages.*/
  667. free_area_init_node(nid, zones_size, start_pfn, zholes_size);
  668. /*
  669. * The node we allocated has no zone fallback lists. For avoiding
  670. * to access not-initialized zonelist, build here.
  671. */
  672. mutex_lock(&zonelists_mutex);
  673. build_all_zonelists(pgdat, NULL);
  674. mutex_unlock(&zonelists_mutex);
  675. return pgdat;
  676. }
  677. static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
  678. {
  679. arch_refresh_nodedata(nid, NULL);
  680. arch_free_nodedata(pgdat);
  681. return;
  682. }
  683. /*
  684. * called by cpu_up() to online a node without onlined memory.
  685. */
  686. int mem_online_node(int nid)
  687. {
  688. pg_data_t *pgdat;
  689. int ret;
  690. lock_memory_hotplug();
  691. pgdat = hotadd_new_pgdat(nid, 0);
  692. if (!pgdat) {
  693. ret = -ENOMEM;
  694. goto out;
  695. }
  696. node_set_online(nid);
  697. ret = register_one_node(nid);
  698. BUG_ON(ret);
  699. out:
  700. unlock_memory_hotplug();
  701. return ret;
  702. }
  703. /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
  704. int __ref add_memory(int nid, u64 start, u64 size)
  705. {
  706. pg_data_t *pgdat = NULL;
  707. int new_pgdat = 0;
  708. struct resource *res;
  709. int ret;
  710. lock_memory_hotplug();
  711. res = register_memory_resource(start, size);
  712. ret = -EEXIST;
  713. if (!res)
  714. goto out;
  715. if (!node_online(nid)) {
  716. pgdat = hotadd_new_pgdat(nid, start);
  717. ret = -ENOMEM;
  718. if (!pgdat)
  719. goto error;
  720. new_pgdat = 1;
  721. }
  722. /* call arch's memory hotadd */
  723. ret = arch_add_memory(nid, start, size);
  724. if (ret < 0)
  725. goto error;
  726. /* we online node here. we can't roll back from here. */
  727. node_set_online(nid);
  728. if (new_pgdat) {
  729. ret = register_one_node(nid);
  730. /*
  731. * If sysfs file of new node can't create, cpu on the node
  732. * can't be hot-added. There is no rollback way now.
  733. * So, check by BUG_ON() to catch it reluctantly..
  734. */
  735. BUG_ON(ret);
  736. }
  737. /* create new memmap entry */
  738. firmware_map_add_hotplug(start, start + size, "System RAM");
  739. goto out;
  740. error:
  741. /* rollback pgdat allocation and others */
  742. if (new_pgdat)
  743. rollback_node_hotadd(nid, pgdat);
  744. if (res)
  745. release_memory_resource(res);
  746. out:
  747. unlock_memory_hotplug();
  748. return ret;
  749. }
  750. EXPORT_SYMBOL_GPL(add_memory);
  751. #ifdef CONFIG_MEMORY_HOTREMOVE
  752. /*
  753. * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
  754. * set and the size of the free page is given by page_order(). Using this,
  755. * the function determines if the pageblock contains only free pages.
  756. * Due to buddy contraints, a free page at least the size of a pageblock will
  757. * be located at the start of the pageblock
  758. */
  759. static inline int pageblock_free(struct page *page)
  760. {
  761. return PageBuddy(page) && page_order(page) >= pageblock_order;
  762. }
  763. /* Return the start of the next active pageblock after a given page */
  764. static struct page *next_active_pageblock(struct page *page)
  765. {
  766. /* Ensure the starting page is pageblock-aligned */
  767. BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
  768. /* If the entire pageblock is free, move to the end of free page */
  769. if (pageblock_free(page)) {
  770. int order;
  771. /* be careful. we don't have locks, page_order can be changed.*/
  772. order = page_order(page);
  773. if ((order < MAX_ORDER) && (order >= pageblock_order))
  774. return page + (1 << order);
  775. }
  776. return page + pageblock_nr_pages;
  777. }
  778. /* Checks if this range of memory is likely to be hot-removable. */
  779. int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
  780. {
  781. struct page *page = pfn_to_page(start_pfn);
  782. struct page *end_page = page + nr_pages;
  783. /* Check the starting page of each pageblock within the range */
  784. for (; page < end_page; page = next_active_pageblock(page)) {
  785. if (!is_pageblock_removable_nolock(page))
  786. return 0;
  787. cond_resched();
  788. }
  789. /* All pageblocks in the memory block are likely to be hot-removable */
  790. return 1;
  791. }
  792. /*
  793. * Confirm all pages in a range [start, end) is belongs to the same zone.
  794. */
  795. static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
  796. {
  797. unsigned long pfn;
  798. struct zone *zone = NULL;
  799. struct page *page;
  800. int i;
  801. for (pfn = start_pfn;
  802. pfn < end_pfn;
  803. pfn += MAX_ORDER_NR_PAGES) {
  804. i = 0;
  805. /* This is just a CONFIG_HOLES_IN_ZONE check.*/
  806. while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
  807. i++;
  808. if (i == MAX_ORDER_NR_PAGES)
  809. continue;
  810. page = pfn_to_page(pfn + i);
  811. if (zone && page_zone(page) != zone)
  812. return 0;
  813. zone = page_zone(page);
  814. }
  815. return 1;
  816. }
  817. /*
  818. * Scanning pfn is much easier than scanning lru list.
  819. * Scan pfn from start to end and Find LRU page.
  820. */
  821. static unsigned long scan_lru_pages(unsigned long start, unsigned long end)
  822. {
  823. unsigned long pfn;
  824. struct page *page;
  825. for (pfn = start; pfn < end; pfn++) {
  826. if (pfn_valid(pfn)) {
  827. page = pfn_to_page(pfn);
  828. if (PageLRU(page))
  829. return pfn;
  830. }
  831. }
  832. return 0;
  833. }
  834. #define NR_OFFLINE_AT_ONCE_PAGES (256)
  835. static int
  836. do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
  837. {
  838. unsigned long pfn;
  839. struct page *page;
  840. int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
  841. int not_managed = 0;
  842. int ret = 0;
  843. LIST_HEAD(source);
  844. for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
  845. if (!pfn_valid(pfn))
  846. continue;
  847. page = pfn_to_page(pfn);
  848. if (!get_page_unless_zero(page))
  849. continue;
  850. /*
  851. * We can skip free pages. And we can only deal with pages on
  852. * LRU.
  853. */
  854. ret = isolate_lru_page(page);
  855. if (!ret) { /* Success */
  856. put_page(page);
  857. list_add_tail(&page->lru, &source);
  858. move_pages--;
  859. inc_zone_page_state(page, NR_ISOLATED_ANON +
  860. page_is_file_cache(page));
  861. } else {
  862. #ifdef CONFIG_DEBUG_VM
  863. printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
  864. pfn);
  865. dump_page(page);
  866. #endif
  867. put_page(page);
  868. /* Because we don't have big zone->lock. we should
  869. check this again here. */
  870. if (page_count(page)) {
  871. not_managed++;
  872. ret = -EBUSY;
  873. break;
  874. }
  875. }
  876. }
  877. if (!list_empty(&source)) {
  878. if (not_managed) {
  879. putback_lru_pages(&source);
  880. goto out;
  881. }
  882. /*
  883. * alloc_migrate_target should be improooooved!!
  884. * migrate_pages returns # of failed pages.
  885. */
  886. ret = migrate_pages(&source, alloc_migrate_target, 0,
  887. true, MIGRATE_SYNC,
  888. MR_MEMORY_HOTPLUG);
  889. if (ret)
  890. putback_lru_pages(&source);
  891. }
  892. out:
  893. return ret;
  894. }
  895. /*
  896. * remove from free_area[] and mark all as Reserved.
  897. */
  898. static int
  899. offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
  900. void *data)
  901. {
  902. __offline_isolated_pages(start, start + nr_pages);
  903. return 0;
  904. }
  905. static void
  906. offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
  907. {
  908. walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
  909. offline_isolated_pages_cb);
  910. }
  911. /*
  912. * Check all pages in range, recoreded as memory resource, are isolated.
  913. */
  914. static int
  915. check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
  916. void *data)
  917. {
  918. int ret;
  919. long offlined = *(long *)data;
  920. ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
  921. offlined = nr_pages;
  922. if (!ret)
  923. *(long *)data += offlined;
  924. return ret;
  925. }
  926. static long
  927. check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
  928. {
  929. long offlined = 0;
  930. int ret;
  931. ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
  932. check_pages_isolated_cb);
  933. if (ret < 0)
  934. offlined = (long)ret;
  935. return offlined;
  936. }
  937. #ifdef CONFIG_MOVABLE_NODE
  938. /* when CONFIG_MOVABLE_NODE, we allow online node don't have normal memory */
  939. static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
  940. {
  941. return true;
  942. }
  943. #else /* #ifdef CONFIG_MOVABLE_NODE */
  944. /* ensure the node has NORMAL memory if it is still online */
  945. static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
  946. {
  947. struct pglist_data *pgdat = zone->zone_pgdat;
  948. unsigned long present_pages = 0;
  949. enum zone_type zt;
  950. for (zt = 0; zt <= ZONE_NORMAL; zt++)
  951. present_pages += pgdat->node_zones[zt].present_pages;
  952. if (present_pages > nr_pages)
  953. return true;
  954. present_pages = 0;
  955. for (; zt <= ZONE_MOVABLE; zt++)
  956. present_pages += pgdat->node_zones[zt].present_pages;
  957. /*
  958. * we can't offline the last normal memory until all
  959. * higher memory is offlined.
  960. */
  961. return present_pages == 0;
  962. }
  963. #endif /* #ifdef CONFIG_MOVABLE_NODE */
  964. /* check which state of node_states will be changed when offline memory */
  965. static void node_states_check_changes_offline(unsigned long nr_pages,
  966. struct zone *zone, struct memory_notify *arg)
  967. {
  968. struct pglist_data *pgdat = zone->zone_pgdat;
  969. unsigned long present_pages = 0;
  970. enum zone_type zt, zone_last = ZONE_NORMAL;
  971. /*
  972. * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
  973. * contains nodes which have zones of 0...ZONE_NORMAL,
  974. * set zone_last to ZONE_NORMAL.
  975. *
  976. * If we don't have HIGHMEM nor movable node,
  977. * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
  978. * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
  979. */
  980. if (N_MEMORY == N_NORMAL_MEMORY)
  981. zone_last = ZONE_MOVABLE;
  982. /*
  983. * check whether node_states[N_NORMAL_MEMORY] will be changed.
  984. * If the memory to be offline is in a zone of 0...zone_last,
  985. * and it is the last present memory, 0...zone_last will
  986. * become empty after offline , thus we can determind we will
  987. * need to clear the node from node_states[N_NORMAL_MEMORY].
  988. */
  989. for (zt = 0; zt <= zone_last; zt++)
  990. present_pages += pgdat->node_zones[zt].present_pages;
  991. if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
  992. arg->status_change_nid_normal = zone_to_nid(zone);
  993. else
  994. arg->status_change_nid_normal = -1;
  995. #ifdef CONFIG_HIGHMEM
  996. /*
  997. * If we have movable node, node_states[N_HIGH_MEMORY]
  998. * contains nodes which have zones of 0...ZONE_HIGHMEM,
  999. * set zone_last to ZONE_HIGHMEM.
  1000. *
  1001. * If we don't have movable node, node_states[N_NORMAL_MEMORY]
  1002. * contains nodes which have zones of 0...ZONE_MOVABLE,
  1003. * set zone_last to ZONE_MOVABLE.
  1004. */
  1005. zone_last = ZONE_HIGHMEM;
  1006. if (N_MEMORY == N_HIGH_MEMORY)
  1007. zone_last = ZONE_MOVABLE;
  1008. for (; zt <= zone_last; zt++)
  1009. present_pages += pgdat->node_zones[zt].present_pages;
  1010. if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
  1011. arg->status_change_nid_high = zone_to_nid(zone);
  1012. else
  1013. arg->status_change_nid_high = -1;
  1014. #else
  1015. arg->status_change_nid_high = arg->status_change_nid_normal;
  1016. #endif
  1017. /*
  1018. * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
  1019. */
  1020. zone_last = ZONE_MOVABLE;
  1021. /*
  1022. * check whether node_states[N_HIGH_MEMORY] will be changed
  1023. * If we try to offline the last present @nr_pages from the node,
  1024. * we can determind we will need to clear the node from
  1025. * node_states[N_HIGH_MEMORY].
  1026. */
  1027. for (; zt <= zone_last; zt++)
  1028. present_pages += pgdat->node_zones[zt].present_pages;
  1029. if (nr_pages >= present_pages)
  1030. arg->status_change_nid = zone_to_nid(zone);
  1031. else
  1032. arg->status_change_nid = -1;
  1033. }
  1034. static void node_states_clear_node(int node, struct memory_notify *arg)
  1035. {
  1036. if (arg->status_change_nid_normal >= 0)
  1037. node_clear_state(node, N_NORMAL_MEMORY);
  1038. if ((N_MEMORY != N_NORMAL_MEMORY) &&
  1039. (arg->status_change_nid_high >= 0))
  1040. node_clear_state(node, N_HIGH_MEMORY);
  1041. if ((N_MEMORY != N_HIGH_MEMORY) &&
  1042. (arg->status_change_nid >= 0))
  1043. node_clear_state(node, N_MEMORY);
  1044. }
  1045. static int __ref __offline_pages(unsigned long start_pfn,
  1046. unsigned long end_pfn, unsigned long timeout)
  1047. {
  1048. unsigned long pfn, nr_pages, expire;
  1049. long offlined_pages;
  1050. int ret, drain, retry_max, node;
  1051. struct zone *zone;
  1052. struct memory_notify arg;
  1053. BUG_ON(start_pfn >= end_pfn);
  1054. /* at least, alignment against pageblock is necessary */
  1055. if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
  1056. return -EINVAL;
  1057. if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
  1058. return -EINVAL;
  1059. /* This makes hotplug much easier...and readable.
  1060. we assume this for now. .*/
  1061. if (!test_pages_in_a_zone(start_pfn, end_pfn))
  1062. return -EINVAL;
  1063. lock_memory_hotplug();
  1064. zone = page_zone(pfn_to_page(start_pfn));
  1065. node = zone_to_nid(zone);
  1066. nr_pages = end_pfn - start_pfn;
  1067. ret = -EINVAL;
  1068. if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
  1069. goto out;
  1070. /* set above range as isolated */
  1071. ret = start_isolate_page_range(start_pfn, end_pfn,
  1072. MIGRATE_MOVABLE, true);
  1073. if (ret)
  1074. goto out;
  1075. arg.start_pfn = start_pfn;
  1076. arg.nr_pages = nr_pages;
  1077. node_states_check_changes_offline(nr_pages, zone, &arg);
  1078. ret = memory_notify(MEM_GOING_OFFLINE, &arg);
  1079. ret = notifier_to_errno(ret);
  1080. if (ret)
  1081. goto failed_removal;
  1082. pfn = start_pfn;
  1083. expire = jiffies + timeout;
  1084. drain = 0;
  1085. retry_max = 5;
  1086. repeat:
  1087. /* start memory hot removal */
  1088. ret = -EAGAIN;
  1089. if (time_after(jiffies, expire))
  1090. goto failed_removal;
  1091. ret = -EINTR;
  1092. if (signal_pending(current))
  1093. goto failed_removal;
  1094. ret = 0;
  1095. if (drain) {
  1096. lru_add_drain_all();
  1097. cond_resched();
  1098. drain_all_pages();
  1099. }
  1100. pfn = scan_lru_pages(start_pfn, end_pfn);
  1101. if (pfn) { /* We have page on LRU */
  1102. ret = do_migrate_range(pfn, end_pfn);
  1103. if (!ret) {
  1104. drain = 1;
  1105. goto repeat;
  1106. } else {
  1107. if (ret < 0)
  1108. if (--retry_max == 0)
  1109. goto failed_removal;
  1110. yield();
  1111. drain = 1;
  1112. goto repeat;
  1113. }
  1114. }
  1115. /* drain all zone's lru pagevec, this is asynchronous... */
  1116. lru_add_drain_all();
  1117. yield();
  1118. /* drain pcp pages, this is synchronous. */
  1119. drain_all_pages();
  1120. /* check again */
  1121. offlined_pages = check_pages_isolated(start_pfn, end_pfn);
  1122. if (offlined_pages < 0) {
  1123. ret = -EBUSY;
  1124. goto failed_removal;
  1125. }
  1126. printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
  1127. /* Ok, all of our target is isolated.
  1128. We cannot do rollback at this point. */
  1129. offline_isolated_pages(start_pfn, end_pfn);
  1130. /* reset pagetype flags and makes migrate type to be MOVABLE */
  1131. undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
  1132. /* removal success */
  1133. zone->managed_pages -= offlined_pages;
  1134. zone->present_pages -= offlined_pages;
  1135. zone->zone_pgdat->node_present_pages -= offlined_pages;
  1136. totalram_pages -= offlined_pages;
  1137. init_per_zone_wmark_min();
  1138. if (!populated_zone(zone)) {
  1139. zone_pcp_reset(zone);
  1140. mutex_lock(&zonelists_mutex);
  1141. build_all_zonelists(NULL, NULL);
  1142. mutex_unlock(&zonelists_mutex);
  1143. } else
  1144. zone_pcp_update(zone);
  1145. node_states_clear_node(node, &arg);
  1146. if (arg.status_change_nid >= 0)
  1147. kswapd_stop(node);
  1148. vm_total_pages = nr_free_pagecache_pages();
  1149. writeback_set_ratelimit();
  1150. memory_notify(MEM_OFFLINE, &arg);
  1151. unlock_memory_hotplug();
  1152. return 0;
  1153. failed_removal:
  1154. printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
  1155. (unsigned long long) start_pfn << PAGE_SHIFT,
  1156. ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
  1157. memory_notify(MEM_CANCEL_OFFLINE, &arg);
  1158. /* pushback to free area */
  1159. undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
  1160. out:
  1161. unlock_memory_hotplug();
  1162. return ret;
  1163. }
  1164. int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
  1165. {
  1166. return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
  1167. }
  1168. int remove_memory(u64 start, u64 size)
  1169. {
  1170. struct memory_block *mem = NULL;
  1171. struct mem_section *section;
  1172. unsigned long start_pfn, end_pfn;
  1173. unsigned long pfn, section_nr;
  1174. int ret;
  1175. start_pfn = PFN_DOWN(start);
  1176. end_pfn = start_pfn + PFN_DOWN(size);
  1177. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  1178. section_nr = pfn_to_section_nr(pfn);
  1179. if (!present_section_nr(section_nr))
  1180. continue;
  1181. section = __nr_to_section(section_nr);
  1182. /* same memblock? */
  1183. if (mem)
  1184. if ((section_nr >= mem->start_section_nr) &&
  1185. (section_nr <= mem->end_section_nr))
  1186. continue;
  1187. mem = find_memory_block_hinted(section, mem);
  1188. if (!mem)
  1189. continue;
  1190. ret = offline_memory_block(mem);
  1191. if (ret) {
  1192. kobject_put(&mem->dev.kobj);
  1193. return ret;
  1194. }
  1195. }
  1196. if (mem)
  1197. kobject_put(&mem->dev.kobj);
  1198. return 0;
  1199. }
  1200. #else
  1201. int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
  1202. {
  1203. return -EINVAL;
  1204. }
  1205. int remove_memory(u64 start, u64 size)
  1206. {
  1207. return -EINVAL;
  1208. }
  1209. #endif /* CONFIG_MEMORY_HOTREMOVE */
  1210. EXPORT_SYMBOL_GPL(remove_memory);