memory_hotplug.c 51 KB

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