memory_hotplug.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001
  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 __meminit 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 __meminit grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
  353. unsigned long end_pfn)
  354. {
  355. unsigned long old_pgdat_end_pfn = pgdat_end_pfn(pgdat);
  356. if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
  357. pgdat->node_start_pfn = start_pfn;
  358. pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
  359. pgdat->node_start_pfn;
  360. }
  361. static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
  362. {
  363. struct pglist_data *pgdat = zone->zone_pgdat;
  364. int nr_pages = PAGES_PER_SECTION;
  365. int nid = pgdat->node_id;
  366. int zone_type;
  367. unsigned long flags;
  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 ||
  815. online_type == MMOP_ONLINE_MOVABLE) &&
  816. !can_online_high_movable(zone))
  817. goto out;
  818. if (online_type == MMOP_ONLINE_KERNEL &&
  819. zone_idx(zone) == ZONE_MOVABLE) {
  820. if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages))
  821. goto out;
  822. }
  823. if (online_type == MMOP_ONLINE_MOVABLE &&
  824. zone_idx(zone) == ZONE_MOVABLE - 1) {
  825. if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages))
  826. goto out;
  827. }
  828. /* Previous code may changed the zone of the pfn range */
  829. zone = page_zone(pfn_to_page(pfn));
  830. arg.start_pfn = pfn;
  831. arg.nr_pages = nr_pages;
  832. node_states_check_changes_online(nr_pages, zone, &arg);
  833. nid = pfn_to_nid(pfn);
  834. ret = memory_notify(MEM_GOING_ONLINE, &arg);
  835. ret = notifier_to_errno(ret);
  836. if (ret) {
  837. memory_notify(MEM_CANCEL_ONLINE, &arg);
  838. goto out;
  839. }
  840. /*
  841. * If this zone is not populated, then it is not in zonelist.
  842. * This means the page allocator ignores this zone.
  843. * So, zonelist must be updated after online.
  844. */
  845. mutex_lock(&zonelists_mutex);
  846. if (!populated_zone(zone)) {
  847. need_zonelists_rebuild = 1;
  848. build_all_zonelists(NULL, zone);
  849. }
  850. ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
  851. online_pages_range);
  852. if (ret) {
  853. if (need_zonelists_rebuild)
  854. zone_pcp_reset(zone);
  855. mutex_unlock(&zonelists_mutex);
  856. printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
  857. (unsigned long long) pfn << PAGE_SHIFT,
  858. (((unsigned long long) pfn + nr_pages)
  859. << PAGE_SHIFT) - 1);
  860. memory_notify(MEM_CANCEL_ONLINE, &arg);
  861. goto out;
  862. }
  863. zone->present_pages += onlined_pages;
  864. pgdat_resize_lock(zone->zone_pgdat, &flags);
  865. zone->zone_pgdat->node_present_pages += onlined_pages;
  866. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  867. if (onlined_pages) {
  868. node_states_set_node(zone_to_nid(zone), &arg);
  869. if (need_zonelists_rebuild)
  870. build_all_zonelists(NULL, NULL);
  871. else
  872. zone_pcp_update(zone);
  873. }
  874. mutex_unlock(&zonelists_mutex);
  875. init_per_zone_wmark_min();
  876. if (onlined_pages)
  877. kswapd_run(zone_to_nid(zone));
  878. vm_total_pages = nr_free_pagecache_pages();
  879. writeback_set_ratelimit();
  880. if (onlined_pages)
  881. memory_notify(MEM_ONLINE, &arg);
  882. out:
  883. mem_hotplug_done();
  884. return ret;
  885. }
  886. #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
  887. /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
  888. static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
  889. {
  890. struct pglist_data *pgdat;
  891. unsigned long zones_size[MAX_NR_ZONES] = {0};
  892. unsigned long zholes_size[MAX_NR_ZONES] = {0};
  893. unsigned long start_pfn = PFN_DOWN(start);
  894. pgdat = NODE_DATA(nid);
  895. if (!pgdat) {
  896. pgdat = arch_alloc_nodedata(nid);
  897. if (!pgdat)
  898. return NULL;
  899. arch_refresh_nodedata(nid, pgdat);
  900. }
  901. /* we can use NODE_DATA(nid) from here */
  902. /* init node's zones as empty zones, we don't have any present pages.*/
  903. free_area_init_node(nid, zones_size, start_pfn, zholes_size);
  904. /*
  905. * The node we allocated has no zone fallback lists. For avoiding
  906. * to access not-initialized zonelist, build here.
  907. */
  908. mutex_lock(&zonelists_mutex);
  909. build_all_zonelists(pgdat, NULL);
  910. mutex_unlock(&zonelists_mutex);
  911. return pgdat;
  912. }
  913. static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
  914. {
  915. arch_refresh_nodedata(nid, NULL);
  916. arch_free_nodedata(pgdat);
  917. return;
  918. }
  919. /**
  920. * try_online_node - online a node if offlined
  921. *
  922. * called by cpu_up() to online a node without onlined memory.
  923. */
  924. int try_online_node(int nid)
  925. {
  926. pg_data_t *pgdat;
  927. int ret;
  928. if (node_online(nid))
  929. return 0;
  930. mem_hotplug_begin();
  931. pgdat = hotadd_new_pgdat(nid, 0);
  932. if (!pgdat) {
  933. pr_err("Cannot online node %d due to NULL pgdat\n", nid);
  934. ret = -ENOMEM;
  935. goto out;
  936. }
  937. node_set_online(nid);
  938. ret = register_one_node(nid);
  939. BUG_ON(ret);
  940. if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
  941. mutex_lock(&zonelists_mutex);
  942. build_all_zonelists(NULL, NULL);
  943. mutex_unlock(&zonelists_mutex);
  944. }
  945. out:
  946. mem_hotplug_done();
  947. return ret;
  948. }
  949. static int check_hotplug_memory_range(u64 start, u64 size)
  950. {
  951. u64 start_pfn = PFN_DOWN(start);
  952. u64 nr_pages = size >> PAGE_SHIFT;
  953. /* Memory range must be aligned with section */
  954. if ((start_pfn & ~PAGE_SECTION_MASK) ||
  955. (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
  956. pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
  957. (unsigned long long)start,
  958. (unsigned long long)size);
  959. return -EINVAL;
  960. }
  961. return 0;
  962. }
  963. /*
  964. * If movable zone has already been setup, newly added memory should be check.
  965. * If its address is higher than movable zone, it should be added as movable.
  966. * Without this check, movable zone may overlap with other zone.
  967. */
  968. static int should_add_memory_movable(int nid, u64 start, u64 size)
  969. {
  970. unsigned long start_pfn = start >> PAGE_SHIFT;
  971. pg_data_t *pgdat = NODE_DATA(nid);
  972. struct zone *movable_zone = pgdat->node_zones + ZONE_MOVABLE;
  973. if (zone_is_empty(movable_zone))
  974. return 0;
  975. if (movable_zone->zone_start_pfn <= start_pfn)
  976. return 1;
  977. return 0;
  978. }
  979. int zone_for_memory(int nid, u64 start, u64 size, int zone_default)
  980. {
  981. if (should_add_memory_movable(nid, start, size))
  982. return ZONE_MOVABLE;
  983. return zone_default;
  984. }
  985. /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
  986. int __ref add_memory(int nid, u64 start, u64 size)
  987. {
  988. pg_data_t *pgdat = NULL;
  989. bool new_pgdat;
  990. bool new_node;
  991. struct resource *res;
  992. int ret;
  993. ret = check_hotplug_memory_range(start, size);
  994. if (ret)
  995. return ret;
  996. res = register_memory_resource(start, size);
  997. ret = -EEXIST;
  998. if (!res)
  999. return ret;
  1000. { /* Stupid hack to suppress address-never-null warning */
  1001. void *p = NODE_DATA(nid);
  1002. new_pgdat = !p;
  1003. }
  1004. mem_hotplug_begin();
  1005. new_node = !node_online(nid);
  1006. if (new_node) {
  1007. pgdat = hotadd_new_pgdat(nid, start);
  1008. ret = -ENOMEM;
  1009. if (!pgdat)
  1010. goto error;
  1011. }
  1012. /* call arch's memory hotadd */
  1013. ret = arch_add_memory(nid, start, size);
  1014. if (ret < 0)
  1015. goto error;
  1016. /* we online node here. we can't roll back from here. */
  1017. node_set_online(nid);
  1018. if (new_node) {
  1019. ret = register_one_node(nid);
  1020. /*
  1021. * If sysfs file of new node can't create, cpu on the node
  1022. * can't be hot-added. There is no rollback way now.
  1023. * So, check by BUG_ON() to catch it reluctantly..
  1024. */
  1025. BUG_ON(ret);
  1026. }
  1027. /* create new memmap entry */
  1028. firmware_map_add_hotplug(start, start + size, "System RAM");
  1029. goto out;
  1030. error:
  1031. /* rollback pgdat allocation and others */
  1032. if (new_pgdat)
  1033. rollback_node_hotadd(nid, pgdat);
  1034. release_memory_resource(res);
  1035. out:
  1036. mem_hotplug_done();
  1037. return ret;
  1038. }
  1039. EXPORT_SYMBOL_GPL(add_memory);
  1040. #ifdef CONFIG_MEMORY_HOTREMOVE
  1041. /*
  1042. * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
  1043. * set and the size of the free page is given by page_order(). Using this,
  1044. * the function determines if the pageblock contains only free pages.
  1045. * Due to buddy contraints, a free page at least the size of a pageblock will
  1046. * be located at the start of the pageblock
  1047. */
  1048. static inline int pageblock_free(struct page *page)
  1049. {
  1050. return PageBuddy(page) && page_order(page) >= pageblock_order;
  1051. }
  1052. /* Return the start of the next active pageblock after a given page */
  1053. static struct page *next_active_pageblock(struct page *page)
  1054. {
  1055. /* Ensure the starting page is pageblock-aligned */
  1056. BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
  1057. /* If the entire pageblock is free, move to the end of free page */
  1058. if (pageblock_free(page)) {
  1059. int order;
  1060. /* be careful. we don't have locks, page_order can be changed.*/
  1061. order = page_order(page);
  1062. if ((order < MAX_ORDER) && (order >= pageblock_order))
  1063. return page + (1 << order);
  1064. }
  1065. return page + pageblock_nr_pages;
  1066. }
  1067. /* Checks if this range of memory is likely to be hot-removable. */
  1068. int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
  1069. {
  1070. struct page *page = pfn_to_page(start_pfn);
  1071. struct page *end_page = page + nr_pages;
  1072. /* Check the starting page of each pageblock within the range */
  1073. for (; page < end_page; page = next_active_pageblock(page)) {
  1074. if (!is_pageblock_removable_nolock(page))
  1075. return 0;
  1076. cond_resched();
  1077. }
  1078. /* All pageblocks in the memory block are likely to be hot-removable */
  1079. return 1;
  1080. }
  1081. /*
  1082. * Confirm all pages in a range [start, end) is belongs to the same zone.
  1083. */
  1084. int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
  1085. {
  1086. unsigned long pfn;
  1087. struct zone *zone = NULL;
  1088. struct page *page;
  1089. int i;
  1090. for (pfn = start_pfn;
  1091. pfn < end_pfn;
  1092. pfn += MAX_ORDER_NR_PAGES) {
  1093. i = 0;
  1094. /* This is just a CONFIG_HOLES_IN_ZONE check.*/
  1095. while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
  1096. i++;
  1097. if (i == MAX_ORDER_NR_PAGES)
  1098. continue;
  1099. page = pfn_to_page(pfn + i);
  1100. if (zone && page_zone(page) != zone)
  1101. return 0;
  1102. zone = page_zone(page);
  1103. }
  1104. return 1;
  1105. }
  1106. /*
  1107. * Scan pfn range [start,end) to find movable/migratable pages (LRU pages
  1108. * and hugepages). We scan pfn because it's much easier than scanning over
  1109. * linked list. This function returns the pfn of the first found movable
  1110. * page if it's found, otherwise 0.
  1111. */
  1112. static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
  1113. {
  1114. unsigned long pfn;
  1115. struct page *page;
  1116. for (pfn = start; pfn < end; pfn++) {
  1117. if (pfn_valid(pfn)) {
  1118. page = pfn_to_page(pfn);
  1119. if (PageLRU(page))
  1120. return pfn;
  1121. if (PageHuge(page)) {
  1122. if (is_hugepage_active(page))
  1123. return pfn;
  1124. else
  1125. pfn = round_up(pfn + 1,
  1126. 1 << compound_order(page)) - 1;
  1127. }
  1128. }
  1129. }
  1130. return 0;
  1131. }
  1132. #define NR_OFFLINE_AT_ONCE_PAGES (256)
  1133. static int
  1134. do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
  1135. {
  1136. unsigned long pfn;
  1137. struct page *page;
  1138. int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
  1139. int not_managed = 0;
  1140. int ret = 0;
  1141. LIST_HEAD(source);
  1142. for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
  1143. if (!pfn_valid(pfn))
  1144. continue;
  1145. page = pfn_to_page(pfn);
  1146. if (PageHuge(page)) {
  1147. struct page *head = compound_head(page);
  1148. pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
  1149. if (compound_order(head) > PFN_SECTION_SHIFT) {
  1150. ret = -EBUSY;
  1151. break;
  1152. }
  1153. if (isolate_huge_page(page, &source))
  1154. move_pages -= 1 << compound_order(head);
  1155. continue;
  1156. }
  1157. if (!get_page_unless_zero(page))
  1158. continue;
  1159. /*
  1160. * We can skip free pages. And we can only deal with pages on
  1161. * LRU.
  1162. */
  1163. ret = isolate_lru_page(page);
  1164. if (!ret) { /* Success */
  1165. put_page(page);
  1166. list_add_tail(&page->lru, &source);
  1167. move_pages--;
  1168. inc_zone_page_state(page, NR_ISOLATED_ANON +
  1169. page_is_file_cache(page));
  1170. } else {
  1171. #ifdef CONFIG_DEBUG_VM
  1172. printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
  1173. pfn);
  1174. dump_page(page, "failed to remove from LRU");
  1175. #endif
  1176. put_page(page);
  1177. /* Because we don't have big zone->lock. we should
  1178. check this again here. */
  1179. if (page_count(page)) {
  1180. not_managed++;
  1181. ret = -EBUSY;
  1182. break;
  1183. }
  1184. }
  1185. }
  1186. if (!list_empty(&source)) {
  1187. if (not_managed) {
  1188. putback_movable_pages(&source);
  1189. goto out;
  1190. }
  1191. /*
  1192. * alloc_migrate_target should be improooooved!!
  1193. * migrate_pages returns # of failed pages.
  1194. */
  1195. ret = migrate_pages(&source, alloc_migrate_target, NULL, 0,
  1196. MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
  1197. if (ret)
  1198. putback_movable_pages(&source);
  1199. }
  1200. out:
  1201. return ret;
  1202. }
  1203. /*
  1204. * remove from free_area[] and mark all as Reserved.
  1205. */
  1206. static int
  1207. offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
  1208. void *data)
  1209. {
  1210. __offline_isolated_pages(start, start + nr_pages);
  1211. return 0;
  1212. }
  1213. static void
  1214. offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
  1215. {
  1216. walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
  1217. offline_isolated_pages_cb);
  1218. }
  1219. /*
  1220. * Check all pages in range, recoreded as memory resource, are isolated.
  1221. */
  1222. static int
  1223. check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
  1224. void *data)
  1225. {
  1226. int ret;
  1227. long offlined = *(long *)data;
  1228. ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
  1229. offlined = nr_pages;
  1230. if (!ret)
  1231. *(long *)data += offlined;
  1232. return ret;
  1233. }
  1234. static long
  1235. check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
  1236. {
  1237. long offlined = 0;
  1238. int ret;
  1239. ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
  1240. check_pages_isolated_cb);
  1241. if (ret < 0)
  1242. offlined = (long)ret;
  1243. return offlined;
  1244. }
  1245. #ifdef CONFIG_MOVABLE_NODE
  1246. /*
  1247. * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
  1248. * normal memory.
  1249. */
  1250. static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
  1251. {
  1252. return true;
  1253. }
  1254. #else /* CONFIG_MOVABLE_NODE */
  1255. /* ensure the node has NORMAL memory if it is still online */
  1256. static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
  1257. {
  1258. struct pglist_data *pgdat = zone->zone_pgdat;
  1259. unsigned long present_pages = 0;
  1260. enum zone_type zt;
  1261. for (zt = 0; zt <= ZONE_NORMAL; zt++)
  1262. present_pages += pgdat->node_zones[zt].present_pages;
  1263. if (present_pages > nr_pages)
  1264. return true;
  1265. present_pages = 0;
  1266. for (; zt <= ZONE_MOVABLE; zt++)
  1267. present_pages += pgdat->node_zones[zt].present_pages;
  1268. /*
  1269. * we can't offline the last normal memory until all
  1270. * higher memory is offlined.
  1271. */
  1272. return present_pages == 0;
  1273. }
  1274. #endif /* CONFIG_MOVABLE_NODE */
  1275. static int __init cmdline_parse_movable_node(char *p)
  1276. {
  1277. #ifdef CONFIG_MOVABLE_NODE
  1278. /*
  1279. * Memory used by the kernel cannot be hot-removed because Linux
  1280. * cannot migrate the kernel pages. When memory hotplug is
  1281. * enabled, we should prevent memblock from allocating memory
  1282. * for the kernel.
  1283. *
  1284. * ACPI SRAT records all hotpluggable memory ranges. But before
  1285. * SRAT is parsed, we don't know about it.
  1286. *
  1287. * The kernel image is loaded into memory at very early time. We
  1288. * cannot prevent this anyway. So on NUMA system, we set any
  1289. * node the kernel resides in as un-hotpluggable.
  1290. *
  1291. * Since on modern servers, one node could have double-digit
  1292. * gigabytes memory, we can assume the memory around the kernel
  1293. * image is also un-hotpluggable. So before SRAT is parsed, just
  1294. * allocate memory near the kernel image to try the best to keep
  1295. * the kernel away from hotpluggable memory.
  1296. */
  1297. memblock_set_bottom_up(true);
  1298. movable_node_enabled = true;
  1299. #else
  1300. pr_warn("movable_node option not supported\n");
  1301. #endif
  1302. return 0;
  1303. }
  1304. early_param("movable_node", cmdline_parse_movable_node);
  1305. /* check which state of node_states will be changed when offline memory */
  1306. static void node_states_check_changes_offline(unsigned long nr_pages,
  1307. struct zone *zone, struct memory_notify *arg)
  1308. {
  1309. struct pglist_data *pgdat = zone->zone_pgdat;
  1310. unsigned long present_pages = 0;
  1311. enum zone_type zt, zone_last = ZONE_NORMAL;
  1312. /*
  1313. * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
  1314. * contains nodes which have zones of 0...ZONE_NORMAL,
  1315. * set zone_last to ZONE_NORMAL.
  1316. *
  1317. * If we don't have HIGHMEM nor movable node,
  1318. * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
  1319. * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
  1320. */
  1321. if (N_MEMORY == N_NORMAL_MEMORY)
  1322. zone_last = ZONE_MOVABLE;
  1323. /*
  1324. * check whether node_states[N_NORMAL_MEMORY] will be changed.
  1325. * If the memory to be offline is in a zone of 0...zone_last,
  1326. * and it is the last present memory, 0...zone_last will
  1327. * become empty after offline , thus we can determind we will
  1328. * need to clear the node from node_states[N_NORMAL_MEMORY].
  1329. */
  1330. for (zt = 0; zt <= zone_last; zt++)
  1331. present_pages += pgdat->node_zones[zt].present_pages;
  1332. if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
  1333. arg->status_change_nid_normal = zone_to_nid(zone);
  1334. else
  1335. arg->status_change_nid_normal = -1;
  1336. #ifdef CONFIG_HIGHMEM
  1337. /*
  1338. * If we have movable node, node_states[N_HIGH_MEMORY]
  1339. * contains nodes which have zones of 0...ZONE_HIGHMEM,
  1340. * set zone_last to ZONE_HIGHMEM.
  1341. *
  1342. * If we don't have movable node, node_states[N_NORMAL_MEMORY]
  1343. * contains nodes which have zones of 0...ZONE_MOVABLE,
  1344. * set zone_last to ZONE_MOVABLE.
  1345. */
  1346. zone_last = ZONE_HIGHMEM;
  1347. if (N_MEMORY == N_HIGH_MEMORY)
  1348. zone_last = ZONE_MOVABLE;
  1349. for (; zt <= zone_last; zt++)
  1350. present_pages += pgdat->node_zones[zt].present_pages;
  1351. if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
  1352. arg->status_change_nid_high = zone_to_nid(zone);
  1353. else
  1354. arg->status_change_nid_high = -1;
  1355. #else
  1356. arg->status_change_nid_high = arg->status_change_nid_normal;
  1357. #endif
  1358. /*
  1359. * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
  1360. */
  1361. zone_last = ZONE_MOVABLE;
  1362. /*
  1363. * check whether node_states[N_HIGH_MEMORY] will be changed
  1364. * If we try to offline the last present @nr_pages from the node,
  1365. * we can determind we will need to clear the node from
  1366. * node_states[N_HIGH_MEMORY].
  1367. */
  1368. for (; zt <= zone_last; zt++)
  1369. present_pages += pgdat->node_zones[zt].present_pages;
  1370. if (nr_pages >= present_pages)
  1371. arg->status_change_nid = zone_to_nid(zone);
  1372. else
  1373. arg->status_change_nid = -1;
  1374. }
  1375. static void node_states_clear_node(int node, struct memory_notify *arg)
  1376. {
  1377. if (arg->status_change_nid_normal >= 0)
  1378. node_clear_state(node, N_NORMAL_MEMORY);
  1379. if ((N_MEMORY != N_NORMAL_MEMORY) &&
  1380. (arg->status_change_nid_high >= 0))
  1381. node_clear_state(node, N_HIGH_MEMORY);
  1382. if ((N_MEMORY != N_HIGH_MEMORY) &&
  1383. (arg->status_change_nid >= 0))
  1384. node_clear_state(node, N_MEMORY);
  1385. }
  1386. static int __ref __offline_pages(unsigned long start_pfn,
  1387. unsigned long end_pfn, unsigned long timeout)
  1388. {
  1389. unsigned long pfn, nr_pages, expire;
  1390. long offlined_pages;
  1391. int ret, drain, retry_max, node;
  1392. unsigned long flags;
  1393. struct zone *zone;
  1394. struct memory_notify arg;
  1395. /* at least, alignment against pageblock is necessary */
  1396. if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
  1397. return -EINVAL;
  1398. if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
  1399. return -EINVAL;
  1400. /* This makes hotplug much easier...and readable.
  1401. we assume this for now. .*/
  1402. if (!test_pages_in_a_zone(start_pfn, end_pfn))
  1403. return -EINVAL;
  1404. mem_hotplug_begin();
  1405. zone = page_zone(pfn_to_page(start_pfn));
  1406. node = zone_to_nid(zone);
  1407. nr_pages = end_pfn - start_pfn;
  1408. ret = -EINVAL;
  1409. if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
  1410. goto out;
  1411. /* set above range as isolated */
  1412. ret = start_isolate_page_range(start_pfn, end_pfn,
  1413. MIGRATE_MOVABLE, true);
  1414. if (ret)
  1415. goto out;
  1416. arg.start_pfn = start_pfn;
  1417. arg.nr_pages = nr_pages;
  1418. node_states_check_changes_offline(nr_pages, zone, &arg);
  1419. ret = memory_notify(MEM_GOING_OFFLINE, &arg);
  1420. ret = notifier_to_errno(ret);
  1421. if (ret)
  1422. goto failed_removal;
  1423. pfn = start_pfn;
  1424. expire = jiffies + timeout;
  1425. drain = 0;
  1426. retry_max = 5;
  1427. repeat:
  1428. /* start memory hot removal */
  1429. ret = -EAGAIN;
  1430. if (time_after(jiffies, expire))
  1431. goto failed_removal;
  1432. ret = -EINTR;
  1433. if (signal_pending(current))
  1434. goto failed_removal;
  1435. ret = 0;
  1436. if (drain) {
  1437. lru_add_drain_all();
  1438. cond_resched();
  1439. drain_all_pages();
  1440. }
  1441. pfn = scan_movable_pages(start_pfn, end_pfn);
  1442. if (pfn) { /* We have movable pages */
  1443. ret = do_migrate_range(pfn, end_pfn);
  1444. if (!ret) {
  1445. drain = 1;
  1446. goto repeat;
  1447. } else {
  1448. if (ret < 0)
  1449. if (--retry_max == 0)
  1450. goto failed_removal;
  1451. yield();
  1452. drain = 1;
  1453. goto repeat;
  1454. }
  1455. }
  1456. /* drain all zone's lru pagevec, this is asynchronous... */
  1457. lru_add_drain_all();
  1458. yield();
  1459. /* drain pcp pages, this is synchronous. */
  1460. drain_all_pages();
  1461. /*
  1462. * dissolve free hugepages in the memory block before doing offlining
  1463. * actually in order to make hugetlbfs's object counting consistent.
  1464. */
  1465. dissolve_free_huge_pages(start_pfn, end_pfn);
  1466. /* check again */
  1467. offlined_pages = check_pages_isolated(start_pfn, end_pfn);
  1468. if (offlined_pages < 0) {
  1469. ret = -EBUSY;
  1470. goto failed_removal;
  1471. }
  1472. printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
  1473. /* Ok, all of our target is isolated.
  1474. We cannot do rollback at this point. */
  1475. offline_isolated_pages(start_pfn, end_pfn);
  1476. /* reset pagetype flags and makes migrate type to be MOVABLE */
  1477. undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
  1478. /* removal success */
  1479. adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
  1480. zone->present_pages -= offlined_pages;
  1481. pgdat_resize_lock(zone->zone_pgdat, &flags);
  1482. zone->zone_pgdat->node_present_pages -= offlined_pages;
  1483. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  1484. init_per_zone_wmark_min();
  1485. if (!populated_zone(zone)) {
  1486. zone_pcp_reset(zone);
  1487. mutex_lock(&zonelists_mutex);
  1488. build_all_zonelists(NULL, NULL);
  1489. mutex_unlock(&zonelists_mutex);
  1490. } else
  1491. zone_pcp_update(zone);
  1492. node_states_clear_node(node, &arg);
  1493. if (arg.status_change_nid >= 0)
  1494. kswapd_stop(node);
  1495. vm_total_pages = nr_free_pagecache_pages();
  1496. writeback_set_ratelimit();
  1497. memory_notify(MEM_OFFLINE, &arg);
  1498. mem_hotplug_done();
  1499. return 0;
  1500. failed_removal:
  1501. printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
  1502. (unsigned long long) start_pfn << PAGE_SHIFT,
  1503. ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
  1504. memory_notify(MEM_CANCEL_OFFLINE, &arg);
  1505. /* pushback to free area */
  1506. undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
  1507. out:
  1508. mem_hotplug_done();
  1509. return ret;
  1510. }
  1511. int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
  1512. {
  1513. return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
  1514. }
  1515. #endif /* CONFIG_MEMORY_HOTREMOVE */
  1516. /**
  1517. * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
  1518. * @start_pfn: start pfn of the memory range
  1519. * @end_pfn: end pfn of the memory range
  1520. * @arg: argument passed to func
  1521. * @func: callback for each memory section walked
  1522. *
  1523. * This function walks through all present mem sections in range
  1524. * [start_pfn, end_pfn) and call func on each mem section.
  1525. *
  1526. * Returns the return value of func.
  1527. */
  1528. int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
  1529. void *arg, int (*func)(struct memory_block *, void *))
  1530. {
  1531. struct memory_block *mem = NULL;
  1532. struct mem_section *section;
  1533. unsigned long pfn, section_nr;
  1534. int ret;
  1535. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  1536. section_nr = pfn_to_section_nr(pfn);
  1537. if (!present_section_nr(section_nr))
  1538. continue;
  1539. section = __nr_to_section(section_nr);
  1540. /* same memblock? */
  1541. if (mem)
  1542. if ((section_nr >= mem->start_section_nr) &&
  1543. (section_nr <= mem->end_section_nr))
  1544. continue;
  1545. mem = find_memory_block_hinted(section, mem);
  1546. if (!mem)
  1547. continue;
  1548. ret = func(mem, arg);
  1549. if (ret) {
  1550. kobject_put(&mem->dev.kobj);
  1551. return ret;
  1552. }
  1553. }
  1554. if (mem)
  1555. kobject_put(&mem->dev.kobj);
  1556. return 0;
  1557. }
  1558. #ifdef CONFIG_MEMORY_HOTREMOVE
  1559. static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
  1560. {
  1561. int ret = !is_memblock_offlined(mem);
  1562. if (unlikely(ret)) {
  1563. phys_addr_t beginpa, endpa;
  1564. beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
  1565. endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
  1566. pr_warn("removing memory fails, because memory "
  1567. "[%pa-%pa] is onlined\n",
  1568. &beginpa, &endpa);
  1569. }
  1570. return ret;
  1571. }
  1572. static int check_cpu_on_node(pg_data_t *pgdat)
  1573. {
  1574. int cpu;
  1575. for_each_present_cpu(cpu) {
  1576. if (cpu_to_node(cpu) == pgdat->node_id)
  1577. /*
  1578. * the cpu on this node isn't removed, and we can't
  1579. * offline this node.
  1580. */
  1581. return -EBUSY;
  1582. }
  1583. return 0;
  1584. }
  1585. static void unmap_cpu_on_node(pg_data_t *pgdat)
  1586. {
  1587. #ifdef CONFIG_ACPI_NUMA
  1588. int cpu;
  1589. for_each_possible_cpu(cpu)
  1590. if (cpu_to_node(cpu) == pgdat->node_id)
  1591. numa_clear_node(cpu);
  1592. #endif
  1593. }
  1594. static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
  1595. {
  1596. int ret;
  1597. ret = check_cpu_on_node(pgdat);
  1598. if (ret)
  1599. return ret;
  1600. /*
  1601. * the node will be offlined when we come here, so we can clear
  1602. * the cpu_to_node() now.
  1603. */
  1604. unmap_cpu_on_node(pgdat);
  1605. return 0;
  1606. }
  1607. /**
  1608. * try_offline_node
  1609. *
  1610. * Offline a node if all memory sections and cpus of the node are removed.
  1611. *
  1612. * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
  1613. * and online/offline operations before this call.
  1614. */
  1615. void try_offline_node(int nid)
  1616. {
  1617. pg_data_t *pgdat = NODE_DATA(nid);
  1618. unsigned long start_pfn = pgdat->node_start_pfn;
  1619. unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
  1620. unsigned long pfn;
  1621. int i;
  1622. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  1623. unsigned long section_nr = pfn_to_section_nr(pfn);
  1624. if (!present_section_nr(section_nr))
  1625. continue;
  1626. if (pfn_to_nid(pfn) != nid)
  1627. continue;
  1628. /*
  1629. * some memory sections of this node are not removed, and we
  1630. * can't offline node now.
  1631. */
  1632. return;
  1633. }
  1634. if (check_and_unmap_cpu_on_node(pgdat))
  1635. return;
  1636. /*
  1637. * all memory/cpu of this node are removed, we can offline this
  1638. * node now.
  1639. */
  1640. node_set_offline(nid);
  1641. unregister_one_node(nid);
  1642. /* free waittable in each zone */
  1643. for (i = 0; i < MAX_NR_ZONES; i++) {
  1644. struct zone *zone = pgdat->node_zones + i;
  1645. /*
  1646. * wait_table may be allocated from boot memory,
  1647. * here only free if it's allocated by vmalloc.
  1648. */
  1649. if (is_vmalloc_addr(zone->wait_table))
  1650. vfree(zone->wait_table);
  1651. }
  1652. /*
  1653. * Since there is no way to guarentee the address of pgdat/zone is not
  1654. * on stack of any kernel threads or used by other kernel objects
  1655. * without reference counting or other symchronizing method, do not
  1656. * reset node_data and free pgdat here. Just reset it to 0 and reuse
  1657. * the memory when the node is online again.
  1658. */
  1659. memset(pgdat, 0, sizeof(*pgdat));
  1660. }
  1661. EXPORT_SYMBOL(try_offline_node);
  1662. /**
  1663. * remove_memory
  1664. *
  1665. * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
  1666. * and online/offline operations before this call, as required by
  1667. * try_offline_node().
  1668. */
  1669. void __ref remove_memory(int nid, u64 start, u64 size)
  1670. {
  1671. int ret;
  1672. BUG_ON(check_hotplug_memory_range(start, size));
  1673. mem_hotplug_begin();
  1674. /*
  1675. * All memory blocks must be offlined before removing memory. Check
  1676. * whether all memory blocks in question are offline and trigger a BUG()
  1677. * if this is not the case.
  1678. */
  1679. ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
  1680. check_memblock_offlined_cb);
  1681. if (ret)
  1682. BUG();
  1683. /* remove memmap entry */
  1684. firmware_map_remove(start, start + size, "System RAM");
  1685. arch_remove_memory(start, size);
  1686. try_offline_node(nid);
  1687. mem_hotplug_done();
  1688. }
  1689. EXPORT_SYMBOL_GPL(remove_memory);
  1690. #endif /* CONFIG_MEMORY_HOTREMOVE */