memory_hotplug.c 50 KB

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