memory_hotplug.c 56 KB

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