memory_hotplug.c 45 KB

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