memory_hotplug.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097
  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 <linux/rmap.h>
  35. #include <asm/tlbflush.h>
  36. #include "internal.h"
  37. /*
  38. * online_page_callback contains pointer to current page onlining function.
  39. * Initially it is generic_online_page(). If it is required it could be
  40. * changed by calling set_online_page_callback() for callback registration
  41. * and restore_online_page_callback() for generic callback restore.
  42. */
  43. static void generic_online_page(struct page *page);
  44. static online_page_callback_t online_page_callback = generic_online_page;
  45. static DEFINE_MUTEX(online_page_callback_lock);
  46. /* The same as the cpu_hotplug lock, but for memory hotplug. */
  47. static struct {
  48. struct task_struct *active_writer;
  49. struct mutex lock; /* Synchronizes accesses to refcount, */
  50. /*
  51. * Also blocks the new readers during
  52. * an ongoing mem hotplug operation.
  53. */
  54. int refcount;
  55. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  56. struct lockdep_map dep_map;
  57. #endif
  58. } mem_hotplug = {
  59. .active_writer = NULL,
  60. .lock = __MUTEX_INITIALIZER(mem_hotplug.lock),
  61. .refcount = 0,
  62. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  63. .dep_map = {.name = "mem_hotplug.lock" },
  64. #endif
  65. };
  66. /* Lockdep annotations for get/put_online_mems() and mem_hotplug_begin/end() */
  67. #define memhp_lock_acquire_read() lock_map_acquire_read(&mem_hotplug.dep_map)
  68. #define memhp_lock_acquire() lock_map_acquire(&mem_hotplug.dep_map)
  69. #define memhp_lock_release() lock_map_release(&mem_hotplug.dep_map)
  70. void get_online_mems(void)
  71. {
  72. might_sleep();
  73. if (mem_hotplug.active_writer == current)
  74. return;
  75. memhp_lock_acquire_read();
  76. mutex_lock(&mem_hotplug.lock);
  77. mem_hotplug.refcount++;
  78. mutex_unlock(&mem_hotplug.lock);
  79. }
  80. void put_online_mems(void)
  81. {
  82. if (mem_hotplug.active_writer == current)
  83. return;
  84. mutex_lock(&mem_hotplug.lock);
  85. if (WARN_ON(!mem_hotplug.refcount))
  86. mem_hotplug.refcount++; /* try to fix things up */
  87. if (!--mem_hotplug.refcount && unlikely(mem_hotplug.active_writer))
  88. wake_up_process(mem_hotplug.active_writer);
  89. mutex_unlock(&mem_hotplug.lock);
  90. memhp_lock_release();
  91. }
  92. void mem_hotplug_begin(void)
  93. {
  94. mem_hotplug.active_writer = current;
  95. memhp_lock_acquire();
  96. for (;;) {
  97. mutex_lock(&mem_hotplug.lock);
  98. if (likely(!mem_hotplug.refcount))
  99. break;
  100. __set_current_state(TASK_UNINTERRUPTIBLE);
  101. mutex_unlock(&mem_hotplug.lock);
  102. schedule();
  103. }
  104. }
  105. void mem_hotplug_done(void)
  106. {
  107. mem_hotplug.active_writer = NULL;
  108. mutex_unlock(&mem_hotplug.lock);
  109. memhp_lock_release();
  110. }
  111. /* add this memory to iomem resource */
  112. static struct resource *register_memory_resource(u64 start, u64 size)
  113. {
  114. struct resource *res;
  115. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  116. BUG_ON(!res);
  117. res->name = "System RAM";
  118. res->start = start;
  119. res->end = start + size - 1;
  120. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  121. if (request_resource(&iomem_resource, res) < 0) {
  122. pr_debug("System RAM resource %pR cannot be added\n", res);
  123. kfree(res);
  124. res = NULL;
  125. }
  126. return res;
  127. }
  128. static void release_memory_resource(struct resource *res)
  129. {
  130. if (!res)
  131. return;
  132. release_resource(res);
  133. kfree(res);
  134. return;
  135. }
  136. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  137. void get_page_bootmem(unsigned long info, struct page *page,
  138. unsigned long type)
  139. {
  140. page->lru.next = (struct list_head *) type;
  141. SetPagePrivate(page);
  142. set_page_private(page, info);
  143. atomic_inc(&page->_count);
  144. }
  145. void put_page_bootmem(struct page *page)
  146. {
  147. unsigned long type;
  148. type = (unsigned long) page->lru.next;
  149. BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
  150. type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
  151. if (atomic_dec_return(&page->_count) == 1) {
  152. ClearPagePrivate(page);
  153. set_page_private(page, 0);
  154. INIT_LIST_HEAD(&page->lru);
  155. free_reserved_page(page);
  156. }
  157. }
  158. #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
  159. #ifndef CONFIG_SPARSEMEM_VMEMMAP
  160. static void register_page_bootmem_info_section(unsigned long start_pfn)
  161. {
  162. unsigned long *usemap, mapsize, section_nr, i;
  163. struct mem_section *ms;
  164. struct page *page, *memmap;
  165. section_nr = pfn_to_section_nr(start_pfn);
  166. ms = __nr_to_section(section_nr);
  167. /* Get section's memmap address */
  168. memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
  169. /*
  170. * Get page for the memmap's phys address
  171. * XXX: need more consideration for sparse_vmemmap...
  172. */
  173. page = virt_to_page(memmap);
  174. mapsize = sizeof(struct page) * PAGES_PER_SECTION;
  175. mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
  176. /* remember memmap's page */
  177. for (i = 0; i < mapsize; i++, page++)
  178. get_page_bootmem(section_nr, page, SECTION_INFO);
  179. usemap = __nr_to_section(section_nr)->pageblock_flags;
  180. page = virt_to_page(usemap);
  181. mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
  182. for (i = 0; i < mapsize; i++, page++)
  183. get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
  184. }
  185. #else /* CONFIG_SPARSEMEM_VMEMMAP */
  186. static void register_page_bootmem_info_section(unsigned long start_pfn)
  187. {
  188. unsigned long *usemap, mapsize, section_nr, i;
  189. struct mem_section *ms;
  190. struct page *page, *memmap;
  191. if (!pfn_valid(start_pfn))
  192. return;
  193. section_nr = pfn_to_section_nr(start_pfn);
  194. ms = __nr_to_section(section_nr);
  195. memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
  196. register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
  197. usemap = __nr_to_section(section_nr)->pageblock_flags;
  198. page = virt_to_page(usemap);
  199. mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
  200. for (i = 0; i < mapsize; i++, page++)
  201. get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
  202. }
  203. #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
  204. void register_page_bootmem_info_node(struct pglist_data *pgdat)
  205. {
  206. unsigned long i, pfn, end_pfn, nr_pages;
  207. int node = pgdat->node_id;
  208. struct page *page;
  209. struct zone *zone;
  210. nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
  211. page = virt_to_page(pgdat);
  212. for (i = 0; i < nr_pages; i++, page++)
  213. get_page_bootmem(node, page, NODE_INFO);
  214. zone = &pgdat->node_zones[0];
  215. for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
  216. if (zone_is_initialized(zone)) {
  217. nr_pages = zone->wait_table_hash_nr_entries
  218. * sizeof(wait_queue_head_t);
  219. nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
  220. page = virt_to_page(zone->wait_table);
  221. for (i = 0; i < nr_pages; i++, page++)
  222. get_page_bootmem(node, page, NODE_INFO);
  223. }
  224. }
  225. pfn = pgdat->node_start_pfn;
  226. end_pfn = pgdat_end_pfn(pgdat);
  227. /* register section info */
  228. for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  229. /*
  230. * Some platforms can assign the same pfn to multiple nodes - on
  231. * node0 as well as nodeN. To avoid registering a pfn against
  232. * multiple nodes we check that this pfn does not already
  233. * reside in some other nodes.
  234. */
  235. if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
  236. register_page_bootmem_info_section(pfn);
  237. }
  238. }
  239. #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
  240. static void __meminit grow_zone_span(struct zone *zone, unsigned long start_pfn,
  241. unsigned long end_pfn)
  242. {
  243. unsigned long old_zone_end_pfn;
  244. zone_span_writelock(zone);
  245. old_zone_end_pfn = zone_end_pfn(zone);
  246. if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
  247. zone->zone_start_pfn = start_pfn;
  248. zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
  249. zone->zone_start_pfn;
  250. zone_span_writeunlock(zone);
  251. }
  252. static void resize_zone(struct zone *zone, unsigned long start_pfn,
  253. unsigned long end_pfn)
  254. {
  255. zone_span_writelock(zone);
  256. if (end_pfn - start_pfn) {
  257. zone->zone_start_pfn = start_pfn;
  258. zone->spanned_pages = end_pfn - start_pfn;
  259. } else {
  260. /*
  261. * make it consist as free_area_init_core(),
  262. * if spanned_pages = 0, then keep start_pfn = 0
  263. */
  264. zone->zone_start_pfn = 0;
  265. zone->spanned_pages = 0;
  266. }
  267. zone_span_writeunlock(zone);
  268. }
  269. static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
  270. unsigned long end_pfn)
  271. {
  272. enum zone_type zid = zone_idx(zone);
  273. int nid = zone->zone_pgdat->node_id;
  274. unsigned long pfn;
  275. for (pfn = start_pfn; pfn < end_pfn; pfn++)
  276. set_page_links(pfn_to_page(pfn), zid, nid, pfn);
  277. }
  278. /* Can fail with -ENOMEM from allocating a wait table with vmalloc() or
  279. * alloc_bootmem_node_nopanic()/memblock_virt_alloc_node_nopanic() */
  280. static int __ref ensure_zone_is_initialized(struct zone *zone,
  281. unsigned long start_pfn, unsigned long num_pages)
  282. {
  283. if (!zone_is_initialized(zone))
  284. return init_currently_empty_zone(zone, start_pfn, num_pages);
  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_resource(int nid, struct resource *res)
  1023. {
  1024. u64 start, size;
  1025. pg_data_t *pgdat = NULL;
  1026. bool new_pgdat;
  1027. bool new_node;
  1028. int ret;
  1029. start = res->start;
  1030. size = resource_size(res);
  1031. ret = check_hotplug_memory_range(start, size);
  1032. if (ret)
  1033. return ret;
  1034. { /* Stupid hack to suppress address-never-null warning */
  1035. void *p = NODE_DATA(nid);
  1036. new_pgdat = !p;
  1037. }
  1038. mem_hotplug_begin();
  1039. /*
  1040. * Add new range to memblock so that when hotadd_new_pgdat() is called
  1041. * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
  1042. * this new range and calculate total pages correctly. The range will
  1043. * be removed at hot-remove time.
  1044. */
  1045. memblock_add_node(start, size, nid);
  1046. new_node = !node_online(nid);
  1047. if (new_node) {
  1048. pgdat = hotadd_new_pgdat(nid, start);
  1049. ret = -ENOMEM;
  1050. if (!pgdat)
  1051. goto error;
  1052. }
  1053. /* call arch's memory hotadd */
  1054. ret = arch_add_memory(nid, start, size, false);
  1055. if (ret < 0)
  1056. goto error;
  1057. /* we online node here. we can't roll back from here. */
  1058. node_set_online(nid);
  1059. if (new_node) {
  1060. ret = register_one_node(nid);
  1061. /*
  1062. * If sysfs file of new node can't create, cpu on the node
  1063. * can't be hot-added. There is no rollback way now.
  1064. * So, check by BUG_ON() to catch it reluctantly..
  1065. */
  1066. BUG_ON(ret);
  1067. }
  1068. /* create new memmap entry */
  1069. firmware_map_add_hotplug(start, start + size, "System RAM");
  1070. goto out;
  1071. error:
  1072. /* rollback pgdat allocation and others */
  1073. if (new_pgdat)
  1074. rollback_node_hotadd(nid, pgdat);
  1075. memblock_remove(start, size);
  1076. out:
  1077. mem_hotplug_done();
  1078. return ret;
  1079. }
  1080. EXPORT_SYMBOL_GPL(add_memory_resource);
  1081. int __ref add_memory(int nid, u64 start, u64 size)
  1082. {
  1083. struct resource *res;
  1084. int ret;
  1085. res = register_memory_resource(start, size);
  1086. if (!res)
  1087. return -EEXIST;
  1088. ret = add_memory_resource(nid, res);
  1089. if (ret < 0)
  1090. release_memory_resource(res);
  1091. return ret;
  1092. }
  1093. EXPORT_SYMBOL_GPL(add_memory);
  1094. #ifdef CONFIG_MEMORY_HOTREMOVE
  1095. /*
  1096. * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
  1097. * set and the size of the free page is given by page_order(). Using this,
  1098. * the function determines if the pageblock contains only free pages.
  1099. * Due to buddy contraints, a free page at least the size of a pageblock will
  1100. * be located at the start of the pageblock
  1101. */
  1102. static inline int pageblock_free(struct page *page)
  1103. {
  1104. return PageBuddy(page) && page_order(page) >= pageblock_order;
  1105. }
  1106. /* Return the start of the next active pageblock after a given page */
  1107. static struct page *next_active_pageblock(struct page *page)
  1108. {
  1109. /* Ensure the starting page is pageblock-aligned */
  1110. BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
  1111. /* If the entire pageblock is free, move to the end of free page */
  1112. if (pageblock_free(page)) {
  1113. int order;
  1114. /* be careful. we don't have locks, page_order can be changed.*/
  1115. order = page_order(page);
  1116. if ((order < MAX_ORDER) && (order >= pageblock_order))
  1117. return page + (1 << order);
  1118. }
  1119. return page + pageblock_nr_pages;
  1120. }
  1121. /* Checks if this range of memory is likely to be hot-removable. */
  1122. int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
  1123. {
  1124. struct page *page = pfn_to_page(start_pfn);
  1125. unsigned long end_pfn = min(start_pfn + nr_pages, zone_end_pfn(page_zone(page)));
  1126. struct page *end_page = pfn_to_page(end_pfn);
  1127. /* Check the starting page of each pageblock within the range */
  1128. for (; page < end_page; page = next_active_pageblock(page)) {
  1129. if (!is_pageblock_removable_nolock(page))
  1130. return 0;
  1131. cond_resched();
  1132. }
  1133. /* All pageblocks in the memory block are likely to be hot-removable */
  1134. return 1;
  1135. }
  1136. /*
  1137. * Confirm all pages in a range [start, end) belong to the same zone.
  1138. * When true, return its valid [start, end).
  1139. */
  1140. int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn,
  1141. unsigned long *valid_start, unsigned long *valid_end)
  1142. {
  1143. unsigned long pfn, sec_end_pfn;
  1144. unsigned long start, end;
  1145. struct zone *zone = NULL;
  1146. struct page *page;
  1147. int i;
  1148. for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn + 1);
  1149. pfn < end_pfn;
  1150. pfn = sec_end_pfn, sec_end_pfn += PAGES_PER_SECTION) {
  1151. /* Make sure the memory section is present first */
  1152. if (!present_section_nr(pfn_to_section_nr(pfn)))
  1153. continue;
  1154. for (; pfn < sec_end_pfn && pfn < end_pfn;
  1155. pfn += MAX_ORDER_NR_PAGES) {
  1156. i = 0;
  1157. /* This is just a CONFIG_HOLES_IN_ZONE check.*/
  1158. while ((i < MAX_ORDER_NR_PAGES) &&
  1159. !pfn_valid_within(pfn + i))
  1160. i++;
  1161. if (i == MAX_ORDER_NR_PAGES)
  1162. continue;
  1163. /* Check if we got outside of the zone */
  1164. if (zone && !zone_spans_pfn(zone, pfn + i))
  1165. return 0;
  1166. page = pfn_to_page(pfn + i);
  1167. if (zone && page_zone(page) != zone)
  1168. return 0;
  1169. if (!zone)
  1170. start = pfn + i;
  1171. zone = page_zone(page);
  1172. end = pfn + MAX_ORDER_NR_PAGES;
  1173. }
  1174. }
  1175. if (zone) {
  1176. *valid_start = start;
  1177. *valid_end = end;
  1178. return 1;
  1179. } else {
  1180. return 0;
  1181. }
  1182. }
  1183. /*
  1184. * Scan pfn range [start,end) to find movable/migratable pages (LRU pages
  1185. * and hugepages). We scan pfn because it's much easier than scanning over
  1186. * linked list. This function returns the pfn of the first found movable
  1187. * page if it's found, otherwise 0.
  1188. */
  1189. static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
  1190. {
  1191. unsigned long pfn;
  1192. struct page *page;
  1193. for (pfn = start; pfn < end; pfn++) {
  1194. if (pfn_valid(pfn)) {
  1195. page = pfn_to_page(pfn);
  1196. if (PageLRU(page))
  1197. return pfn;
  1198. if (PageHuge(page)) {
  1199. if (page_huge_active(page))
  1200. return pfn;
  1201. else
  1202. pfn = round_up(pfn + 1,
  1203. 1 << compound_order(page)) - 1;
  1204. }
  1205. }
  1206. }
  1207. return 0;
  1208. }
  1209. #define NR_OFFLINE_AT_ONCE_PAGES (256)
  1210. static int
  1211. do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
  1212. {
  1213. unsigned long pfn;
  1214. struct page *page;
  1215. int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
  1216. int not_managed = 0;
  1217. int ret = 0;
  1218. LIST_HEAD(source);
  1219. for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
  1220. if (!pfn_valid(pfn))
  1221. continue;
  1222. page = pfn_to_page(pfn);
  1223. if (PageHuge(page)) {
  1224. struct page *head = compound_head(page);
  1225. pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
  1226. if (compound_order(head) > PFN_SECTION_SHIFT) {
  1227. ret = -EBUSY;
  1228. break;
  1229. }
  1230. if (isolate_huge_page(page, &source))
  1231. move_pages -= 1 << compound_order(head);
  1232. continue;
  1233. }
  1234. /*
  1235. * HWPoison pages have elevated reference counts so the migration would
  1236. * fail on them. It also doesn't make any sense to migrate them in the
  1237. * first place. Still try to unmap such a page in case it is still mapped
  1238. * (e.g. current hwpoison implementation doesn't unmap KSM pages but keep
  1239. * the unmap as the catch all safety net).
  1240. */
  1241. if (PageHWPoison(page)) {
  1242. if (WARN_ON(PageLRU(page)))
  1243. isolate_lru_page(page);
  1244. if (page_mapped(page))
  1245. try_to_unmap(page, TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS);
  1246. continue;
  1247. }
  1248. if (!get_page_unless_zero(page))
  1249. continue;
  1250. /*
  1251. * We can skip free pages. And we can only deal with pages on
  1252. * LRU.
  1253. */
  1254. ret = isolate_lru_page(page);
  1255. if (!ret) { /* Success */
  1256. put_page(page);
  1257. list_add_tail(&page->lru, &source);
  1258. move_pages--;
  1259. inc_zone_page_state(page, NR_ISOLATED_ANON +
  1260. page_is_file_cache(page));
  1261. } else {
  1262. #ifdef CONFIG_DEBUG_VM
  1263. printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
  1264. pfn);
  1265. dump_page(page, "failed to remove from LRU");
  1266. #endif
  1267. put_page(page);
  1268. /* Because we don't have big zone->lock. we should
  1269. check this again here. */
  1270. if (page_count(page)) {
  1271. not_managed++;
  1272. ret = -EBUSY;
  1273. break;
  1274. }
  1275. }
  1276. }
  1277. if (!list_empty(&source)) {
  1278. if (not_managed) {
  1279. putback_movable_pages(&source);
  1280. goto out;
  1281. }
  1282. /*
  1283. * alloc_migrate_target should be improooooved!!
  1284. * migrate_pages returns # of failed pages.
  1285. */
  1286. ret = migrate_pages(&source, alloc_migrate_target, NULL, 0,
  1287. MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
  1288. if (ret)
  1289. putback_movable_pages(&source);
  1290. }
  1291. out:
  1292. return ret;
  1293. }
  1294. /*
  1295. * remove from free_area[] and mark all as Reserved.
  1296. */
  1297. static int
  1298. offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
  1299. void *data)
  1300. {
  1301. __offline_isolated_pages(start, start + nr_pages);
  1302. return 0;
  1303. }
  1304. static void
  1305. offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
  1306. {
  1307. walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
  1308. offline_isolated_pages_cb);
  1309. }
  1310. /*
  1311. * Check all pages in range, recoreded as memory resource, are isolated.
  1312. */
  1313. static int
  1314. check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
  1315. void *data)
  1316. {
  1317. int ret;
  1318. long offlined = *(long *)data;
  1319. ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
  1320. offlined = nr_pages;
  1321. if (!ret)
  1322. *(long *)data += offlined;
  1323. return ret;
  1324. }
  1325. static long
  1326. check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
  1327. {
  1328. long offlined = 0;
  1329. int ret;
  1330. ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
  1331. check_pages_isolated_cb);
  1332. if (ret < 0)
  1333. offlined = (long)ret;
  1334. return offlined;
  1335. }
  1336. #ifdef CONFIG_MOVABLE_NODE
  1337. /*
  1338. * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
  1339. * normal memory.
  1340. */
  1341. static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
  1342. {
  1343. return true;
  1344. }
  1345. #else /* CONFIG_MOVABLE_NODE */
  1346. /* ensure the node has NORMAL memory if it is still online */
  1347. static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
  1348. {
  1349. struct pglist_data *pgdat = zone->zone_pgdat;
  1350. unsigned long present_pages = 0;
  1351. enum zone_type zt;
  1352. for (zt = 0; zt <= ZONE_NORMAL; zt++)
  1353. present_pages += pgdat->node_zones[zt].present_pages;
  1354. if (present_pages > nr_pages)
  1355. return true;
  1356. present_pages = 0;
  1357. for (; zt <= ZONE_MOVABLE; zt++)
  1358. present_pages += pgdat->node_zones[zt].present_pages;
  1359. /*
  1360. * we can't offline the last normal memory until all
  1361. * higher memory is offlined.
  1362. */
  1363. return present_pages == 0;
  1364. }
  1365. #endif /* CONFIG_MOVABLE_NODE */
  1366. static int __init cmdline_parse_movable_node(char *p)
  1367. {
  1368. #ifdef CONFIG_MOVABLE_NODE
  1369. /*
  1370. * Memory used by the kernel cannot be hot-removed because Linux
  1371. * cannot migrate the kernel pages. When memory hotplug is
  1372. * enabled, we should prevent memblock from allocating memory
  1373. * for the kernel.
  1374. *
  1375. * ACPI SRAT records all hotpluggable memory ranges. But before
  1376. * SRAT is parsed, we don't know about it.
  1377. *
  1378. * The kernel image is loaded into memory at very early time. We
  1379. * cannot prevent this anyway. So on NUMA system, we set any
  1380. * node the kernel resides in as un-hotpluggable.
  1381. *
  1382. * Since on modern servers, one node could have double-digit
  1383. * gigabytes memory, we can assume the memory around the kernel
  1384. * image is also un-hotpluggable. So before SRAT is parsed, just
  1385. * allocate memory near the kernel image to try the best to keep
  1386. * the kernel away from hotpluggable memory.
  1387. */
  1388. memblock_set_bottom_up(true);
  1389. movable_node_enabled = true;
  1390. #else
  1391. pr_warn("movable_node option not supported\n");
  1392. #endif
  1393. return 0;
  1394. }
  1395. early_param("movable_node", cmdline_parse_movable_node);
  1396. /* check which state of node_states will be changed when offline memory */
  1397. static void node_states_check_changes_offline(unsigned long nr_pages,
  1398. struct zone *zone, struct memory_notify *arg)
  1399. {
  1400. struct pglist_data *pgdat = zone->zone_pgdat;
  1401. unsigned long present_pages = 0;
  1402. enum zone_type zt, zone_last = ZONE_NORMAL;
  1403. /*
  1404. * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
  1405. * contains nodes which have zones of 0...ZONE_NORMAL,
  1406. * set zone_last to ZONE_NORMAL.
  1407. *
  1408. * If we don't have HIGHMEM nor movable node,
  1409. * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
  1410. * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
  1411. */
  1412. if (N_MEMORY == N_NORMAL_MEMORY)
  1413. zone_last = ZONE_MOVABLE;
  1414. /*
  1415. * check whether node_states[N_NORMAL_MEMORY] will be changed.
  1416. * If the memory to be offline is in a zone of 0...zone_last,
  1417. * and it is the last present memory, 0...zone_last will
  1418. * become empty after offline , thus we can determind we will
  1419. * need to clear the node from node_states[N_NORMAL_MEMORY].
  1420. */
  1421. for (zt = 0; zt <= zone_last; zt++)
  1422. present_pages += pgdat->node_zones[zt].present_pages;
  1423. if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
  1424. arg->status_change_nid_normal = zone_to_nid(zone);
  1425. else
  1426. arg->status_change_nid_normal = -1;
  1427. #ifdef CONFIG_HIGHMEM
  1428. /*
  1429. * If we have movable node, node_states[N_HIGH_MEMORY]
  1430. * contains nodes which have zones of 0...ZONE_HIGHMEM,
  1431. * set zone_last to ZONE_HIGHMEM.
  1432. *
  1433. * If we don't have movable node, node_states[N_NORMAL_MEMORY]
  1434. * contains nodes which have zones of 0...ZONE_MOVABLE,
  1435. * set zone_last to ZONE_MOVABLE.
  1436. */
  1437. zone_last = ZONE_HIGHMEM;
  1438. if (N_MEMORY == N_HIGH_MEMORY)
  1439. zone_last = ZONE_MOVABLE;
  1440. for (; zt <= zone_last; zt++)
  1441. present_pages += pgdat->node_zones[zt].present_pages;
  1442. if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
  1443. arg->status_change_nid_high = zone_to_nid(zone);
  1444. else
  1445. arg->status_change_nid_high = -1;
  1446. #else
  1447. arg->status_change_nid_high = arg->status_change_nid_normal;
  1448. #endif
  1449. /*
  1450. * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
  1451. */
  1452. zone_last = ZONE_MOVABLE;
  1453. /*
  1454. * check whether node_states[N_HIGH_MEMORY] will be changed
  1455. * If we try to offline the last present @nr_pages from the node,
  1456. * we can determind we will need to clear the node from
  1457. * node_states[N_HIGH_MEMORY].
  1458. */
  1459. for (; zt <= zone_last; zt++)
  1460. present_pages += pgdat->node_zones[zt].present_pages;
  1461. if (nr_pages >= present_pages)
  1462. arg->status_change_nid = zone_to_nid(zone);
  1463. else
  1464. arg->status_change_nid = -1;
  1465. }
  1466. static void node_states_clear_node(int node, struct memory_notify *arg)
  1467. {
  1468. if (arg->status_change_nid_normal >= 0)
  1469. node_clear_state(node, N_NORMAL_MEMORY);
  1470. if ((N_MEMORY != N_NORMAL_MEMORY) &&
  1471. (arg->status_change_nid_high >= 0))
  1472. node_clear_state(node, N_HIGH_MEMORY);
  1473. if ((N_MEMORY != N_HIGH_MEMORY) &&
  1474. (arg->status_change_nid >= 0))
  1475. node_clear_state(node, N_MEMORY);
  1476. }
  1477. static int __ref __offline_pages(unsigned long start_pfn,
  1478. unsigned long end_pfn, unsigned long timeout)
  1479. {
  1480. unsigned long pfn, nr_pages, expire;
  1481. long offlined_pages;
  1482. int ret, drain, retry_max, node;
  1483. unsigned long flags;
  1484. unsigned long valid_start, valid_end;
  1485. struct zone *zone;
  1486. struct memory_notify arg;
  1487. /* at least, alignment against pageblock is necessary */
  1488. if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
  1489. return -EINVAL;
  1490. if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
  1491. return -EINVAL;
  1492. /* This makes hotplug much easier...and readable.
  1493. we assume this for now. .*/
  1494. if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start, &valid_end))
  1495. return -EINVAL;
  1496. zone = page_zone(pfn_to_page(valid_start));
  1497. node = zone_to_nid(zone);
  1498. nr_pages = end_pfn - start_pfn;
  1499. if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
  1500. return -EINVAL;
  1501. /* set above range as isolated */
  1502. ret = start_isolate_page_range(start_pfn, end_pfn,
  1503. MIGRATE_MOVABLE, true);
  1504. if (ret)
  1505. return ret;
  1506. arg.start_pfn = start_pfn;
  1507. arg.nr_pages = nr_pages;
  1508. node_states_check_changes_offline(nr_pages, zone, &arg);
  1509. ret = memory_notify(MEM_GOING_OFFLINE, &arg);
  1510. ret = notifier_to_errno(ret);
  1511. if (ret)
  1512. goto failed_removal;
  1513. pfn = start_pfn;
  1514. expire = jiffies + timeout;
  1515. drain = 0;
  1516. retry_max = 5;
  1517. repeat:
  1518. /* start memory hot removal */
  1519. ret = -EAGAIN;
  1520. if (time_after(jiffies, expire))
  1521. goto failed_removal;
  1522. ret = -EINTR;
  1523. if (signal_pending(current))
  1524. goto failed_removal;
  1525. ret = 0;
  1526. if (drain) {
  1527. lru_add_drain_all();
  1528. cond_resched();
  1529. drain_all_pages(zone);
  1530. }
  1531. pfn = scan_movable_pages(start_pfn, end_pfn);
  1532. if (pfn) { /* We have movable pages */
  1533. ret = do_migrate_range(pfn, end_pfn);
  1534. if (!ret) {
  1535. drain = 1;
  1536. goto repeat;
  1537. } else {
  1538. if (ret < 0)
  1539. if (--retry_max == 0)
  1540. goto failed_removal;
  1541. yield();
  1542. drain = 1;
  1543. goto repeat;
  1544. }
  1545. }
  1546. /* drain all zone's lru pagevec, this is asynchronous... */
  1547. lru_add_drain_all();
  1548. yield();
  1549. /* drain pcp pages, this is synchronous. */
  1550. drain_all_pages(zone);
  1551. /*
  1552. * dissolve free hugepages in the memory block before doing offlining
  1553. * actually in order to make hugetlbfs's object counting consistent.
  1554. */
  1555. dissolve_free_huge_pages(start_pfn, end_pfn);
  1556. /* check again */
  1557. offlined_pages = check_pages_isolated(start_pfn, end_pfn);
  1558. if (offlined_pages < 0) {
  1559. ret = -EBUSY;
  1560. goto failed_removal;
  1561. }
  1562. printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
  1563. /* Ok, all of our target is isolated.
  1564. We cannot do rollback at this point. */
  1565. offline_isolated_pages(start_pfn, end_pfn);
  1566. /* reset pagetype flags and makes migrate type to be MOVABLE */
  1567. undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
  1568. /* removal success */
  1569. adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
  1570. zone->present_pages -= offlined_pages;
  1571. pgdat_resize_lock(zone->zone_pgdat, &flags);
  1572. zone->zone_pgdat->node_present_pages -= offlined_pages;
  1573. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  1574. init_per_zone_wmark_min();
  1575. if (!populated_zone(zone)) {
  1576. zone_pcp_reset(zone);
  1577. mutex_lock(&zonelists_mutex);
  1578. build_all_zonelists(NULL, NULL);
  1579. mutex_unlock(&zonelists_mutex);
  1580. } else
  1581. zone_pcp_update(zone);
  1582. node_states_clear_node(node, &arg);
  1583. if (arg.status_change_nid >= 0)
  1584. kswapd_stop(node);
  1585. vm_total_pages = nr_free_pagecache_pages();
  1586. writeback_set_ratelimit();
  1587. memory_notify(MEM_OFFLINE, &arg);
  1588. return 0;
  1589. failed_removal:
  1590. printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
  1591. (unsigned long long) start_pfn << PAGE_SHIFT,
  1592. ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
  1593. memory_notify(MEM_CANCEL_OFFLINE, &arg);
  1594. /* pushback to free area */
  1595. undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
  1596. return ret;
  1597. }
  1598. /* Must be protected by mem_hotplug_begin() */
  1599. int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
  1600. {
  1601. return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
  1602. }
  1603. #endif /* CONFIG_MEMORY_HOTREMOVE */
  1604. /**
  1605. * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
  1606. * @start_pfn: start pfn of the memory range
  1607. * @end_pfn: end pfn of the memory range
  1608. * @arg: argument passed to func
  1609. * @func: callback for each memory section walked
  1610. *
  1611. * This function walks through all present mem sections in range
  1612. * [start_pfn, end_pfn) and call func on each mem section.
  1613. *
  1614. * Returns the return value of func.
  1615. */
  1616. int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
  1617. void *arg, int (*func)(struct memory_block *, void *))
  1618. {
  1619. struct memory_block *mem = NULL;
  1620. struct mem_section *section;
  1621. unsigned long pfn, section_nr;
  1622. int ret;
  1623. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  1624. section_nr = pfn_to_section_nr(pfn);
  1625. if (!present_section_nr(section_nr))
  1626. continue;
  1627. section = __nr_to_section(section_nr);
  1628. /* same memblock? */
  1629. if (mem)
  1630. if ((section_nr >= mem->start_section_nr) &&
  1631. (section_nr <= mem->end_section_nr))
  1632. continue;
  1633. mem = find_memory_block_hinted(section, mem);
  1634. if (!mem)
  1635. continue;
  1636. ret = func(mem, arg);
  1637. if (ret) {
  1638. kobject_put(&mem->dev.kobj);
  1639. return ret;
  1640. }
  1641. }
  1642. if (mem)
  1643. kobject_put(&mem->dev.kobj);
  1644. return 0;
  1645. }
  1646. #ifdef CONFIG_MEMORY_HOTREMOVE
  1647. static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
  1648. {
  1649. int ret = !is_memblock_offlined(mem);
  1650. if (unlikely(ret)) {
  1651. phys_addr_t beginpa, endpa;
  1652. beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
  1653. endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
  1654. pr_warn("removing memory fails, because memory "
  1655. "[%pa-%pa] is onlined\n",
  1656. &beginpa, &endpa);
  1657. }
  1658. return ret;
  1659. }
  1660. static int check_cpu_on_node(pg_data_t *pgdat)
  1661. {
  1662. int cpu;
  1663. for_each_present_cpu(cpu) {
  1664. if (cpu_to_node(cpu) == pgdat->node_id)
  1665. /*
  1666. * the cpu on this node isn't removed, and we can't
  1667. * offline this node.
  1668. */
  1669. return -EBUSY;
  1670. }
  1671. return 0;
  1672. }
  1673. static void unmap_cpu_on_node(pg_data_t *pgdat)
  1674. {
  1675. #ifdef CONFIG_ACPI_NUMA
  1676. int cpu;
  1677. for_each_possible_cpu(cpu)
  1678. if (cpu_to_node(cpu) == pgdat->node_id)
  1679. numa_clear_node(cpu);
  1680. #endif
  1681. }
  1682. static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
  1683. {
  1684. int ret;
  1685. ret = check_cpu_on_node(pgdat);
  1686. if (ret)
  1687. return ret;
  1688. /*
  1689. * the node will be offlined when we come here, so we can clear
  1690. * the cpu_to_node() now.
  1691. */
  1692. unmap_cpu_on_node(pgdat);
  1693. return 0;
  1694. }
  1695. /**
  1696. * try_offline_node
  1697. *
  1698. * Offline a node if all memory sections and cpus of the node are removed.
  1699. *
  1700. * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
  1701. * and online/offline operations before this call.
  1702. */
  1703. void try_offline_node(int nid)
  1704. {
  1705. pg_data_t *pgdat = NODE_DATA(nid);
  1706. unsigned long start_pfn = pgdat->node_start_pfn;
  1707. unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
  1708. unsigned long pfn;
  1709. int i;
  1710. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  1711. unsigned long section_nr = pfn_to_section_nr(pfn);
  1712. if (!present_section_nr(section_nr))
  1713. continue;
  1714. if (pfn_to_nid(pfn) != nid)
  1715. continue;
  1716. /*
  1717. * some memory sections of this node are not removed, and we
  1718. * can't offline node now.
  1719. */
  1720. return;
  1721. }
  1722. if (check_and_unmap_cpu_on_node(pgdat))
  1723. return;
  1724. /*
  1725. * all memory/cpu of this node are removed, we can offline this
  1726. * node now.
  1727. */
  1728. node_set_offline(nid);
  1729. unregister_one_node(nid);
  1730. /* free waittable in each zone */
  1731. for (i = 0; i < MAX_NR_ZONES; i++) {
  1732. struct zone *zone = pgdat->node_zones + i;
  1733. /*
  1734. * wait_table may be allocated from boot memory,
  1735. * here only free if it's allocated by vmalloc.
  1736. */
  1737. if (is_vmalloc_addr(zone->wait_table)) {
  1738. vfree(zone->wait_table);
  1739. zone->wait_table = NULL;
  1740. }
  1741. }
  1742. }
  1743. EXPORT_SYMBOL(try_offline_node);
  1744. /**
  1745. * remove_memory
  1746. *
  1747. * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
  1748. * and online/offline operations before this call, as required by
  1749. * try_offline_node().
  1750. */
  1751. void __ref remove_memory(int nid, u64 start, u64 size)
  1752. {
  1753. int ret;
  1754. BUG_ON(check_hotplug_memory_range(start, size));
  1755. mem_hotplug_begin();
  1756. /*
  1757. * All memory blocks must be offlined before removing memory. Check
  1758. * whether all memory blocks in question are offline and trigger a BUG()
  1759. * if this is not the case.
  1760. */
  1761. ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
  1762. check_memblock_offlined_cb);
  1763. if (ret)
  1764. BUG();
  1765. /* remove memmap entry */
  1766. firmware_map_remove(start, start + size, "System RAM");
  1767. memblock_free(start, size);
  1768. memblock_remove(start, size);
  1769. arch_remove_memory(start, size);
  1770. try_offline_node(nid);
  1771. mem_hotplug_done();
  1772. }
  1773. EXPORT_SYMBOL_GPL(remove_memory);
  1774. #endif /* CONFIG_MEMORY_HOTREMOVE */