memory_hotplug.c 47 KB

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