memory_hotplug.c 52 KB

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