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