memory_hotplug.c 52 KB

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