memory_hotplug.c 52 KB

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