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