memory_hotplug.c 47 KB

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