memory_hotplug.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  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 <asm/tlbflush.h>
  32. #include "internal.h"
  33. /*
  34. * online_page_callback contains pointer to current page onlining function.
  35. * Initially it is generic_online_page(). If it is required it could be
  36. * changed by calling set_online_page_callback() for callback registration
  37. * and restore_online_page_callback() for generic callback restore.
  38. */
  39. static void generic_online_page(struct page *page);
  40. static online_page_callback_t online_page_callback = generic_online_page;
  41. DEFINE_MUTEX(mem_hotplug_mutex);
  42. void lock_memory_hotplug(void)
  43. {
  44. mutex_lock(&mem_hotplug_mutex);
  45. /* for exclusive hibernation if CONFIG_HIBERNATION=y */
  46. lock_system_sleep();
  47. }
  48. void unlock_memory_hotplug(void)
  49. {
  50. unlock_system_sleep();
  51. mutex_unlock(&mem_hotplug_mutex);
  52. }
  53. /* add this memory to iomem resource */
  54. static struct resource *register_memory_resource(u64 start, u64 size)
  55. {
  56. struct resource *res;
  57. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  58. BUG_ON(!res);
  59. res->name = "System RAM";
  60. res->start = start;
  61. res->end = start + size - 1;
  62. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  63. if (request_resource(&iomem_resource, res) < 0) {
  64. printk("System RAM resource %pR cannot be added\n", res);
  65. kfree(res);
  66. res = NULL;
  67. }
  68. return res;
  69. }
  70. static void release_memory_resource(struct resource *res)
  71. {
  72. if (!res)
  73. return;
  74. release_resource(res);
  75. kfree(res);
  76. return;
  77. }
  78. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  79. #ifndef CONFIG_SPARSEMEM_VMEMMAP
  80. static void get_page_bootmem(unsigned long info, struct page *page,
  81. unsigned long type)
  82. {
  83. page->lru.next = (struct list_head *) type;
  84. SetPagePrivate(page);
  85. set_page_private(page, info);
  86. atomic_inc(&page->_count);
  87. }
  88. /* reference to __meminit __free_pages_bootmem is valid
  89. * so use __ref to tell modpost not to generate a warning */
  90. void __ref put_page_bootmem(struct page *page)
  91. {
  92. unsigned long type;
  93. type = (unsigned long) page->lru.next;
  94. BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
  95. type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
  96. if (atomic_dec_return(&page->_count) == 1) {
  97. ClearPagePrivate(page);
  98. set_page_private(page, 0);
  99. INIT_LIST_HEAD(&page->lru);
  100. __free_pages_bootmem(page, 0);
  101. }
  102. }
  103. static void register_page_bootmem_info_section(unsigned long start_pfn)
  104. {
  105. unsigned long *usemap, mapsize, section_nr, i;
  106. struct mem_section *ms;
  107. struct page *page, *memmap;
  108. section_nr = pfn_to_section_nr(start_pfn);
  109. ms = __nr_to_section(section_nr);
  110. /* Get section's memmap address */
  111. memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
  112. /*
  113. * Get page for the memmap's phys address
  114. * XXX: need more consideration for sparse_vmemmap...
  115. */
  116. page = virt_to_page(memmap);
  117. mapsize = sizeof(struct page) * PAGES_PER_SECTION;
  118. mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
  119. /* remember memmap's page */
  120. for (i = 0; i < mapsize; i++, page++)
  121. get_page_bootmem(section_nr, page, SECTION_INFO);
  122. usemap = __nr_to_section(section_nr)->pageblock_flags;
  123. page = virt_to_page(usemap);
  124. mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
  125. for (i = 0; i < mapsize; i++, page++)
  126. get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
  127. }
  128. void register_page_bootmem_info_node(struct pglist_data *pgdat)
  129. {
  130. unsigned long i, pfn, end_pfn, nr_pages;
  131. int node = pgdat->node_id;
  132. struct page *page;
  133. struct zone *zone;
  134. nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
  135. page = virt_to_page(pgdat);
  136. for (i = 0; i < nr_pages; i++, page++)
  137. get_page_bootmem(node, page, NODE_INFO);
  138. zone = &pgdat->node_zones[0];
  139. for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
  140. if (zone->wait_table) {
  141. nr_pages = zone->wait_table_hash_nr_entries
  142. * sizeof(wait_queue_head_t);
  143. nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
  144. page = virt_to_page(zone->wait_table);
  145. for (i = 0; i < nr_pages; i++, page++)
  146. get_page_bootmem(node, page, NODE_INFO);
  147. }
  148. }
  149. pfn = pgdat->node_start_pfn;
  150. end_pfn = pfn + pgdat->node_spanned_pages;
  151. /* register_section info */
  152. for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  153. /*
  154. * Some platforms can assign the same pfn to multiple nodes - on
  155. * node0 as well as nodeN. To avoid registering a pfn against
  156. * multiple nodes we check that this pfn does not already
  157. * reside in some other node.
  158. */
  159. if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
  160. register_page_bootmem_info_section(pfn);
  161. }
  162. }
  163. #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
  164. static void grow_zone_span(struct zone *zone, unsigned long start_pfn,
  165. unsigned long end_pfn)
  166. {
  167. unsigned long old_zone_end_pfn;
  168. zone_span_writelock(zone);
  169. old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
  170. if (!zone->spanned_pages || start_pfn < zone->zone_start_pfn)
  171. zone->zone_start_pfn = start_pfn;
  172. zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
  173. zone->zone_start_pfn;
  174. zone_span_writeunlock(zone);
  175. }
  176. static void resize_zone(struct zone *zone, unsigned long start_pfn,
  177. unsigned long end_pfn)
  178. {
  179. zone_span_writelock(zone);
  180. if (end_pfn - start_pfn) {
  181. zone->zone_start_pfn = start_pfn;
  182. zone->spanned_pages = end_pfn - start_pfn;
  183. } else {
  184. /*
  185. * make it consist as free_area_init_core(),
  186. * if spanned_pages = 0, then keep start_pfn = 0
  187. */
  188. zone->zone_start_pfn = 0;
  189. zone->spanned_pages = 0;
  190. }
  191. zone_span_writeunlock(zone);
  192. }
  193. static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
  194. unsigned long end_pfn)
  195. {
  196. enum zone_type zid = zone_idx(zone);
  197. int nid = zone->zone_pgdat->node_id;
  198. unsigned long pfn;
  199. for (pfn = start_pfn; pfn < end_pfn; pfn++)
  200. set_page_links(pfn_to_page(pfn), zid, nid, pfn);
  201. }
  202. static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
  203. unsigned long start_pfn, unsigned long end_pfn)
  204. {
  205. int ret;
  206. unsigned long flags;
  207. unsigned long z1_start_pfn;
  208. if (!z1->wait_table) {
  209. ret = init_currently_empty_zone(z1, start_pfn,
  210. end_pfn - start_pfn, MEMMAP_HOTPLUG);
  211. if (ret)
  212. return ret;
  213. }
  214. pgdat_resize_lock(z1->zone_pgdat, &flags);
  215. /* can't move pfns which are higher than @z2 */
  216. if (end_pfn > z2->zone_start_pfn + z2->spanned_pages)
  217. goto out_fail;
  218. /* the move out part mast at the left most of @z2 */
  219. if (start_pfn > z2->zone_start_pfn)
  220. goto out_fail;
  221. /* must included/overlap */
  222. if (end_pfn <= z2->zone_start_pfn)
  223. goto out_fail;
  224. /* use start_pfn for z1's start_pfn if z1 is empty */
  225. if (z1->spanned_pages)
  226. z1_start_pfn = z1->zone_start_pfn;
  227. else
  228. z1_start_pfn = start_pfn;
  229. resize_zone(z1, z1_start_pfn, end_pfn);
  230. resize_zone(z2, end_pfn, z2->zone_start_pfn + z2->spanned_pages);
  231. pgdat_resize_unlock(z1->zone_pgdat, &flags);
  232. fix_zone_id(z1, start_pfn, end_pfn);
  233. return 0;
  234. out_fail:
  235. pgdat_resize_unlock(z1->zone_pgdat, &flags);
  236. return -1;
  237. }
  238. static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
  239. unsigned long start_pfn, unsigned long end_pfn)
  240. {
  241. int ret;
  242. unsigned long flags;
  243. unsigned long z2_end_pfn;
  244. if (!z2->wait_table) {
  245. ret = init_currently_empty_zone(z2, start_pfn,
  246. end_pfn - start_pfn, MEMMAP_HOTPLUG);
  247. if (ret)
  248. return ret;
  249. }
  250. pgdat_resize_lock(z1->zone_pgdat, &flags);
  251. /* can't move pfns which are lower than @z1 */
  252. if (z1->zone_start_pfn > start_pfn)
  253. goto out_fail;
  254. /* the move out part mast at the right most of @z1 */
  255. if (z1->zone_start_pfn + z1->spanned_pages > end_pfn)
  256. goto out_fail;
  257. /* must included/overlap */
  258. if (start_pfn >= z1->zone_start_pfn + z1->spanned_pages)
  259. goto out_fail;
  260. /* use end_pfn for z2's end_pfn if z2 is empty */
  261. if (z2->spanned_pages)
  262. z2_end_pfn = z2->zone_start_pfn + z2->spanned_pages;
  263. else
  264. z2_end_pfn = end_pfn;
  265. resize_zone(z1, z1->zone_start_pfn, start_pfn);
  266. resize_zone(z2, start_pfn, z2_end_pfn);
  267. pgdat_resize_unlock(z1->zone_pgdat, &flags);
  268. fix_zone_id(z2, start_pfn, end_pfn);
  269. return 0;
  270. out_fail:
  271. pgdat_resize_unlock(z1->zone_pgdat, &flags);
  272. return -1;
  273. }
  274. static void grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
  275. unsigned long end_pfn)
  276. {
  277. unsigned long old_pgdat_end_pfn =
  278. pgdat->node_start_pfn + pgdat->node_spanned_pages;
  279. if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
  280. pgdat->node_start_pfn = start_pfn;
  281. pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
  282. pgdat->node_start_pfn;
  283. }
  284. static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
  285. {
  286. struct pglist_data *pgdat = zone->zone_pgdat;
  287. int nr_pages = PAGES_PER_SECTION;
  288. int nid = pgdat->node_id;
  289. int zone_type;
  290. unsigned long flags;
  291. zone_type = zone - pgdat->node_zones;
  292. if (!zone->wait_table) {
  293. int ret;
  294. ret = init_currently_empty_zone(zone, phys_start_pfn,
  295. nr_pages, MEMMAP_HOTPLUG);
  296. if (ret)
  297. return ret;
  298. }
  299. pgdat_resize_lock(zone->zone_pgdat, &flags);
  300. grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
  301. grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
  302. phys_start_pfn + nr_pages);
  303. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  304. memmap_init_zone(nr_pages, nid, zone_type,
  305. phys_start_pfn, MEMMAP_HOTPLUG);
  306. return 0;
  307. }
  308. static int __meminit __add_section(int nid, struct zone *zone,
  309. unsigned long phys_start_pfn)
  310. {
  311. int nr_pages = PAGES_PER_SECTION;
  312. int ret;
  313. if (pfn_valid(phys_start_pfn))
  314. return -EEXIST;
  315. ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
  316. if (ret < 0)
  317. return ret;
  318. ret = __add_zone(zone, phys_start_pfn);
  319. if (ret < 0)
  320. return ret;
  321. return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
  322. }
  323. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  324. static int __remove_section(struct zone *zone, struct mem_section *ms)
  325. {
  326. /*
  327. * XXX: Freeing memmap with vmemmap is not implement yet.
  328. * This should be removed later.
  329. */
  330. return -EBUSY;
  331. }
  332. #else
  333. static int __remove_section(struct zone *zone, struct mem_section *ms)
  334. {
  335. unsigned long flags;
  336. struct pglist_data *pgdat = zone->zone_pgdat;
  337. int ret = -EINVAL;
  338. if (!valid_section(ms))
  339. return ret;
  340. ret = unregister_memory_section(ms);
  341. if (ret)
  342. return ret;
  343. pgdat_resize_lock(pgdat, &flags);
  344. sparse_remove_one_section(zone, ms);
  345. pgdat_resize_unlock(pgdat, &flags);
  346. return 0;
  347. }
  348. #endif
  349. /*
  350. * Reasonably generic function for adding memory. It is
  351. * expected that archs that support memory hotplug will
  352. * call this function after deciding the zone to which to
  353. * add the new pages.
  354. */
  355. int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
  356. unsigned long nr_pages)
  357. {
  358. unsigned long i;
  359. int err = 0;
  360. int start_sec, end_sec;
  361. /* during initialize mem_map, align hot-added range to section */
  362. start_sec = pfn_to_section_nr(phys_start_pfn);
  363. end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
  364. for (i = start_sec; i <= end_sec; i++) {
  365. err = __add_section(nid, zone, i << PFN_SECTION_SHIFT);
  366. /*
  367. * EEXIST is finally dealt with by ioresource collision
  368. * check. see add_memory() => register_memory_resource()
  369. * Warning will be printed if there is collision.
  370. */
  371. if (err && (err != -EEXIST))
  372. break;
  373. err = 0;
  374. }
  375. return err;
  376. }
  377. EXPORT_SYMBOL_GPL(__add_pages);
  378. /**
  379. * __remove_pages() - remove sections of pages from a zone
  380. * @zone: zone from which pages need to be removed
  381. * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
  382. * @nr_pages: number of pages to remove (must be multiple of section size)
  383. *
  384. * Generic helper function to remove section mappings and sysfs entries
  385. * for the section of the memory we are removing. Caller needs to make
  386. * sure that pages are marked reserved and zones are adjust properly by
  387. * calling offline_pages().
  388. */
  389. int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
  390. unsigned long nr_pages)
  391. {
  392. unsigned long i, ret = 0;
  393. int sections_to_remove;
  394. /*
  395. * We can only remove entire sections
  396. */
  397. BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
  398. BUG_ON(nr_pages % PAGES_PER_SECTION);
  399. release_mem_region(phys_start_pfn << PAGE_SHIFT, nr_pages * PAGE_SIZE);
  400. sections_to_remove = nr_pages / PAGES_PER_SECTION;
  401. for (i = 0; i < sections_to_remove; i++) {
  402. unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
  403. ret = __remove_section(zone, __pfn_to_section(pfn));
  404. if (ret)
  405. break;
  406. }
  407. return ret;
  408. }
  409. EXPORT_SYMBOL_GPL(__remove_pages);
  410. int set_online_page_callback(online_page_callback_t callback)
  411. {
  412. int rc = -EINVAL;
  413. lock_memory_hotplug();
  414. if (online_page_callback == generic_online_page) {
  415. online_page_callback = callback;
  416. rc = 0;
  417. }
  418. unlock_memory_hotplug();
  419. return rc;
  420. }
  421. EXPORT_SYMBOL_GPL(set_online_page_callback);
  422. int restore_online_page_callback(online_page_callback_t callback)
  423. {
  424. int rc = -EINVAL;
  425. lock_memory_hotplug();
  426. if (online_page_callback == callback) {
  427. online_page_callback = generic_online_page;
  428. rc = 0;
  429. }
  430. unlock_memory_hotplug();
  431. return rc;
  432. }
  433. EXPORT_SYMBOL_GPL(restore_online_page_callback);
  434. void __online_page_set_limits(struct page *page)
  435. {
  436. unsigned long pfn = page_to_pfn(page);
  437. if (pfn >= num_physpages)
  438. num_physpages = pfn + 1;
  439. }
  440. EXPORT_SYMBOL_GPL(__online_page_set_limits);
  441. void __online_page_increment_counters(struct page *page)
  442. {
  443. totalram_pages++;
  444. #ifdef CONFIG_HIGHMEM
  445. if (PageHighMem(page))
  446. totalhigh_pages++;
  447. #endif
  448. }
  449. EXPORT_SYMBOL_GPL(__online_page_increment_counters);
  450. void __online_page_free(struct page *page)
  451. {
  452. ClearPageReserved(page);
  453. init_page_count(page);
  454. __free_page(page);
  455. }
  456. EXPORT_SYMBOL_GPL(__online_page_free);
  457. static void generic_online_page(struct page *page)
  458. {
  459. __online_page_set_limits(page);
  460. __online_page_increment_counters(page);
  461. __online_page_free(page);
  462. }
  463. static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
  464. void *arg)
  465. {
  466. unsigned long i;
  467. unsigned long onlined_pages = *(unsigned long *)arg;
  468. struct page *page;
  469. if (PageReserved(pfn_to_page(start_pfn)))
  470. for (i = 0; i < nr_pages; i++) {
  471. page = pfn_to_page(start_pfn + i);
  472. (*online_page_callback)(page);
  473. onlined_pages++;
  474. }
  475. *(unsigned long *)arg = onlined_pages;
  476. return 0;
  477. }
  478. #ifdef CONFIG_MOVABLE_NODE
  479. /* when CONFIG_MOVABLE_NODE, we allow online node don't have normal memory */
  480. static bool can_online_high_movable(struct zone *zone)
  481. {
  482. return true;
  483. }
  484. #else /* #ifdef CONFIG_MOVABLE_NODE */
  485. /* ensure every online node has NORMAL memory */
  486. static bool can_online_high_movable(struct zone *zone)
  487. {
  488. return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
  489. }
  490. #endif /* #ifdef CONFIG_MOVABLE_NODE */
  491. /* check which state of node_states will be changed when online memory */
  492. static void node_states_check_changes_online(unsigned long nr_pages,
  493. struct zone *zone, struct memory_notify *arg)
  494. {
  495. int nid = zone_to_nid(zone);
  496. enum zone_type zone_last = ZONE_NORMAL;
  497. /*
  498. * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
  499. * contains nodes which have zones of 0...ZONE_NORMAL,
  500. * set zone_last to ZONE_NORMAL.
  501. *
  502. * If we don't have HIGHMEM nor movable node,
  503. * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
  504. * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
  505. */
  506. if (N_MEMORY == N_NORMAL_MEMORY)
  507. zone_last = ZONE_MOVABLE;
  508. /*
  509. * if the memory to be online is in a zone of 0...zone_last, and
  510. * the zones of 0...zone_last don't have memory before online, we will
  511. * need to set the node to node_states[N_NORMAL_MEMORY] after
  512. * the memory is online.
  513. */
  514. if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
  515. arg->status_change_nid_normal = nid;
  516. else
  517. arg->status_change_nid_normal = -1;
  518. #ifdef CONFIG_HIGHMEM
  519. /*
  520. * If we have movable node, node_states[N_HIGH_MEMORY]
  521. * contains nodes which have zones of 0...ZONE_HIGHMEM,
  522. * set zone_last to ZONE_HIGHMEM.
  523. *
  524. * If we don't have movable node, node_states[N_NORMAL_MEMORY]
  525. * contains nodes which have zones of 0...ZONE_MOVABLE,
  526. * set zone_last to ZONE_MOVABLE.
  527. */
  528. zone_last = ZONE_HIGHMEM;
  529. if (N_MEMORY == N_HIGH_MEMORY)
  530. zone_last = ZONE_MOVABLE;
  531. if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
  532. arg->status_change_nid_high = nid;
  533. else
  534. arg->status_change_nid_high = -1;
  535. #else
  536. arg->status_change_nid_high = arg->status_change_nid_normal;
  537. #endif
  538. /*
  539. * if the node don't have memory befor online, we will need to
  540. * set the node to node_states[N_MEMORY] after the memory
  541. * is online.
  542. */
  543. if (!node_state(nid, N_MEMORY))
  544. arg->status_change_nid = nid;
  545. else
  546. arg->status_change_nid = -1;
  547. }
  548. static void node_states_set_node(int node, struct memory_notify *arg)
  549. {
  550. if (arg->status_change_nid_normal >= 0)
  551. node_set_state(node, N_NORMAL_MEMORY);
  552. if (arg->status_change_nid_high >= 0)
  553. node_set_state(node, N_HIGH_MEMORY);
  554. node_set_state(node, N_MEMORY);
  555. }
  556. int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
  557. {
  558. unsigned long onlined_pages = 0;
  559. struct zone *zone;
  560. int need_zonelists_rebuild = 0;
  561. int nid;
  562. int ret;
  563. struct memory_notify arg;
  564. lock_memory_hotplug();
  565. /*
  566. * This doesn't need a lock to do pfn_to_page().
  567. * The section can't be removed here because of the
  568. * memory_block->state_mutex.
  569. */
  570. zone = page_zone(pfn_to_page(pfn));
  571. if ((zone_idx(zone) > ZONE_NORMAL || online_type == ONLINE_MOVABLE) &&
  572. !can_online_high_movable(zone)) {
  573. unlock_memory_hotplug();
  574. return -1;
  575. }
  576. if (online_type == ONLINE_KERNEL && zone_idx(zone) == ZONE_MOVABLE) {
  577. if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages)) {
  578. unlock_memory_hotplug();
  579. return -1;
  580. }
  581. }
  582. if (online_type == ONLINE_MOVABLE && zone_idx(zone) == ZONE_MOVABLE - 1) {
  583. if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages)) {
  584. unlock_memory_hotplug();
  585. return -1;
  586. }
  587. }
  588. /* Previous code may changed the zone of the pfn range */
  589. zone = page_zone(pfn_to_page(pfn));
  590. arg.start_pfn = pfn;
  591. arg.nr_pages = nr_pages;
  592. node_states_check_changes_online(nr_pages, zone, &arg);
  593. nid = page_to_nid(pfn_to_page(pfn));
  594. ret = memory_notify(MEM_GOING_ONLINE, &arg);
  595. ret = notifier_to_errno(ret);
  596. if (ret) {
  597. memory_notify(MEM_CANCEL_ONLINE, &arg);
  598. unlock_memory_hotplug();
  599. return ret;
  600. }
  601. /*
  602. * If this zone is not populated, then it is not in zonelist.
  603. * This means the page allocator ignores this zone.
  604. * So, zonelist must be updated after online.
  605. */
  606. mutex_lock(&zonelists_mutex);
  607. if (!populated_zone(zone)) {
  608. need_zonelists_rebuild = 1;
  609. build_all_zonelists(NULL, zone);
  610. }
  611. ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
  612. online_pages_range);
  613. if (ret) {
  614. if (need_zonelists_rebuild)
  615. zone_pcp_reset(zone);
  616. mutex_unlock(&zonelists_mutex);
  617. printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
  618. (unsigned long long) pfn << PAGE_SHIFT,
  619. (((unsigned long long) pfn + nr_pages)
  620. << PAGE_SHIFT) - 1);
  621. memory_notify(MEM_CANCEL_ONLINE, &arg);
  622. unlock_memory_hotplug();
  623. return ret;
  624. }
  625. zone->present_pages += onlined_pages;
  626. zone->zone_pgdat->node_present_pages += onlined_pages;
  627. if (onlined_pages) {
  628. node_states_set_node(zone_to_nid(zone), &arg);
  629. if (need_zonelists_rebuild)
  630. build_all_zonelists(NULL, NULL);
  631. else
  632. zone_pcp_update(zone);
  633. }
  634. mutex_unlock(&zonelists_mutex);
  635. init_per_zone_wmark_min();
  636. if (onlined_pages)
  637. kswapd_run(zone_to_nid(zone));
  638. vm_total_pages = nr_free_pagecache_pages();
  639. writeback_set_ratelimit();
  640. if (onlined_pages)
  641. memory_notify(MEM_ONLINE, &arg);
  642. unlock_memory_hotplug();
  643. return 0;
  644. }
  645. #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
  646. /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
  647. static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
  648. {
  649. struct pglist_data *pgdat;
  650. unsigned long zones_size[MAX_NR_ZONES] = {0};
  651. unsigned long zholes_size[MAX_NR_ZONES] = {0};
  652. unsigned long start_pfn = start >> PAGE_SHIFT;
  653. pgdat = arch_alloc_nodedata(nid);
  654. if (!pgdat)
  655. return NULL;
  656. arch_refresh_nodedata(nid, pgdat);
  657. /* we can use NODE_DATA(nid) from here */
  658. /* init node's zones as empty zones, we don't have any present pages.*/
  659. free_area_init_node(nid, zones_size, start_pfn, zholes_size);
  660. /*
  661. * The node we allocated has no zone fallback lists. For avoiding
  662. * to access not-initialized zonelist, build here.
  663. */
  664. mutex_lock(&zonelists_mutex);
  665. build_all_zonelists(pgdat, NULL);
  666. mutex_unlock(&zonelists_mutex);
  667. return pgdat;
  668. }
  669. static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
  670. {
  671. arch_refresh_nodedata(nid, NULL);
  672. arch_free_nodedata(pgdat);
  673. return;
  674. }
  675. /*
  676. * called by cpu_up() to online a node without onlined memory.
  677. */
  678. int mem_online_node(int nid)
  679. {
  680. pg_data_t *pgdat;
  681. int ret;
  682. lock_memory_hotplug();
  683. pgdat = hotadd_new_pgdat(nid, 0);
  684. if (!pgdat) {
  685. ret = -ENOMEM;
  686. goto out;
  687. }
  688. node_set_online(nid);
  689. ret = register_one_node(nid);
  690. BUG_ON(ret);
  691. out:
  692. unlock_memory_hotplug();
  693. return ret;
  694. }
  695. /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
  696. int __ref add_memory(int nid, u64 start, u64 size)
  697. {
  698. pg_data_t *pgdat = NULL;
  699. int new_pgdat = 0;
  700. struct resource *res;
  701. int ret;
  702. lock_memory_hotplug();
  703. res = register_memory_resource(start, size);
  704. ret = -EEXIST;
  705. if (!res)
  706. goto out;
  707. if (!node_online(nid)) {
  708. pgdat = hotadd_new_pgdat(nid, start);
  709. ret = -ENOMEM;
  710. if (!pgdat)
  711. goto error;
  712. new_pgdat = 1;
  713. }
  714. /* call arch's memory hotadd */
  715. ret = arch_add_memory(nid, start, size);
  716. if (ret < 0)
  717. goto error;
  718. /* we online node here. we can't roll back from here. */
  719. node_set_online(nid);
  720. if (new_pgdat) {
  721. ret = register_one_node(nid);
  722. /*
  723. * If sysfs file of new node can't create, cpu on the node
  724. * can't be hot-added. There is no rollback way now.
  725. * So, check by BUG_ON() to catch it reluctantly..
  726. */
  727. BUG_ON(ret);
  728. }
  729. /* create new memmap entry */
  730. firmware_map_add_hotplug(start, start + size, "System RAM");
  731. goto out;
  732. error:
  733. /* rollback pgdat allocation and others */
  734. if (new_pgdat)
  735. rollback_node_hotadd(nid, pgdat);
  736. if (res)
  737. release_memory_resource(res);
  738. out:
  739. unlock_memory_hotplug();
  740. return ret;
  741. }
  742. EXPORT_SYMBOL_GPL(add_memory);
  743. #ifdef CONFIG_MEMORY_HOTREMOVE
  744. /*
  745. * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
  746. * set and the size of the free page is given by page_order(). Using this,
  747. * the function determines if the pageblock contains only free pages.
  748. * Due to buddy contraints, a free page at least the size of a pageblock will
  749. * be located at the start of the pageblock
  750. */
  751. static inline int pageblock_free(struct page *page)
  752. {
  753. return PageBuddy(page) && page_order(page) >= pageblock_order;
  754. }
  755. /* Return the start of the next active pageblock after a given page */
  756. static struct page *next_active_pageblock(struct page *page)
  757. {
  758. /* Ensure the starting page is pageblock-aligned */
  759. BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
  760. /* If the entire pageblock is free, move to the end of free page */
  761. if (pageblock_free(page)) {
  762. int order;
  763. /* be careful. we don't have locks, page_order can be changed.*/
  764. order = page_order(page);
  765. if ((order < MAX_ORDER) && (order >= pageblock_order))
  766. return page + (1 << order);
  767. }
  768. return page + pageblock_nr_pages;
  769. }
  770. /* Checks if this range of memory is likely to be hot-removable. */
  771. int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
  772. {
  773. struct page *page = pfn_to_page(start_pfn);
  774. struct page *end_page = page + nr_pages;
  775. /* Check the starting page of each pageblock within the range */
  776. for (; page < end_page; page = next_active_pageblock(page)) {
  777. if (!is_pageblock_removable_nolock(page))
  778. return 0;
  779. cond_resched();
  780. }
  781. /* All pageblocks in the memory block are likely to be hot-removable */
  782. return 1;
  783. }
  784. /*
  785. * Confirm all pages in a range [start, end) is belongs to the same zone.
  786. */
  787. static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
  788. {
  789. unsigned long pfn;
  790. struct zone *zone = NULL;
  791. struct page *page;
  792. int i;
  793. for (pfn = start_pfn;
  794. pfn < end_pfn;
  795. pfn += MAX_ORDER_NR_PAGES) {
  796. i = 0;
  797. /* This is just a CONFIG_HOLES_IN_ZONE check.*/
  798. while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
  799. i++;
  800. if (i == MAX_ORDER_NR_PAGES)
  801. continue;
  802. page = pfn_to_page(pfn + i);
  803. if (zone && page_zone(page) != zone)
  804. return 0;
  805. zone = page_zone(page);
  806. }
  807. return 1;
  808. }
  809. /*
  810. * Scanning pfn is much easier than scanning lru list.
  811. * Scan pfn from start to end and Find LRU page.
  812. */
  813. static unsigned long scan_lru_pages(unsigned long start, unsigned long end)
  814. {
  815. unsigned long pfn;
  816. struct page *page;
  817. for (pfn = start; pfn < end; pfn++) {
  818. if (pfn_valid(pfn)) {
  819. page = pfn_to_page(pfn);
  820. if (PageLRU(page))
  821. return pfn;
  822. }
  823. }
  824. return 0;
  825. }
  826. #define NR_OFFLINE_AT_ONCE_PAGES (256)
  827. static int
  828. do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
  829. {
  830. unsigned long pfn;
  831. struct page *page;
  832. int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
  833. int not_managed = 0;
  834. int ret = 0;
  835. LIST_HEAD(source);
  836. for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
  837. if (!pfn_valid(pfn))
  838. continue;
  839. page = pfn_to_page(pfn);
  840. if (!get_page_unless_zero(page))
  841. continue;
  842. /*
  843. * We can skip free pages. And we can only deal with pages on
  844. * LRU.
  845. */
  846. ret = isolate_lru_page(page);
  847. if (!ret) { /* Success */
  848. put_page(page);
  849. list_add_tail(&page->lru, &source);
  850. move_pages--;
  851. inc_zone_page_state(page, NR_ISOLATED_ANON +
  852. page_is_file_cache(page));
  853. } else {
  854. #ifdef CONFIG_DEBUG_VM
  855. printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
  856. pfn);
  857. dump_page(page);
  858. #endif
  859. put_page(page);
  860. /* Because we don't have big zone->lock. we should
  861. check this again here. */
  862. if (page_count(page)) {
  863. not_managed++;
  864. ret = -EBUSY;
  865. break;
  866. }
  867. }
  868. }
  869. if (!list_empty(&source)) {
  870. if (not_managed) {
  871. putback_lru_pages(&source);
  872. goto out;
  873. }
  874. /*
  875. * alloc_migrate_target should be improooooved!!
  876. * migrate_pages returns # of failed pages.
  877. */
  878. ret = migrate_pages(&source, alloc_migrate_target, 0,
  879. true, MIGRATE_SYNC);
  880. if (ret)
  881. putback_lru_pages(&source);
  882. }
  883. out:
  884. return ret;
  885. }
  886. /*
  887. * remove from free_area[] and mark all as Reserved.
  888. */
  889. static int
  890. offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
  891. void *data)
  892. {
  893. __offline_isolated_pages(start, start + nr_pages);
  894. return 0;
  895. }
  896. static void
  897. offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
  898. {
  899. walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
  900. offline_isolated_pages_cb);
  901. }
  902. /*
  903. * Check all pages in range, recoreded as memory resource, are isolated.
  904. */
  905. static int
  906. check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
  907. void *data)
  908. {
  909. int ret;
  910. long offlined = *(long *)data;
  911. ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
  912. offlined = nr_pages;
  913. if (!ret)
  914. *(long *)data += offlined;
  915. return ret;
  916. }
  917. static long
  918. check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
  919. {
  920. long offlined = 0;
  921. int ret;
  922. ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
  923. check_pages_isolated_cb);
  924. if (ret < 0)
  925. offlined = (long)ret;
  926. return offlined;
  927. }
  928. #ifdef CONFIG_MOVABLE_NODE
  929. /* when CONFIG_MOVABLE_NODE, we allow online node don't have normal memory */
  930. static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
  931. {
  932. return true;
  933. }
  934. #else /* #ifdef CONFIG_MOVABLE_NODE */
  935. /* ensure the node has NORMAL memory if it is still online */
  936. static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
  937. {
  938. struct pglist_data *pgdat = zone->zone_pgdat;
  939. unsigned long present_pages = 0;
  940. enum zone_type zt;
  941. for (zt = 0; zt <= ZONE_NORMAL; zt++)
  942. present_pages += pgdat->node_zones[zt].present_pages;
  943. if (present_pages > nr_pages)
  944. return true;
  945. present_pages = 0;
  946. for (; zt <= ZONE_MOVABLE; zt++)
  947. present_pages += pgdat->node_zones[zt].present_pages;
  948. /*
  949. * we can't offline the last normal memory until all
  950. * higher memory is offlined.
  951. */
  952. return present_pages == 0;
  953. }
  954. #endif /* #ifdef CONFIG_MOVABLE_NODE */
  955. /* check which state of node_states will be changed when offline memory */
  956. static void node_states_check_changes_offline(unsigned long nr_pages,
  957. struct zone *zone, struct memory_notify *arg)
  958. {
  959. struct pglist_data *pgdat = zone->zone_pgdat;
  960. unsigned long present_pages = 0;
  961. enum zone_type zt, zone_last = ZONE_NORMAL;
  962. /*
  963. * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
  964. * contains nodes which have zones of 0...ZONE_NORMAL,
  965. * set zone_last to ZONE_NORMAL.
  966. *
  967. * If we don't have HIGHMEM nor movable node,
  968. * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
  969. * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
  970. */
  971. if (N_MEMORY == N_NORMAL_MEMORY)
  972. zone_last = ZONE_MOVABLE;
  973. /*
  974. * check whether node_states[N_NORMAL_MEMORY] will be changed.
  975. * If the memory to be offline is in a zone of 0...zone_last,
  976. * and it is the last present memory, 0...zone_last will
  977. * become empty after offline , thus we can determind we will
  978. * need to clear the node from node_states[N_NORMAL_MEMORY].
  979. */
  980. for (zt = 0; zt <= zone_last; zt++)
  981. present_pages += pgdat->node_zones[zt].present_pages;
  982. if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
  983. arg->status_change_nid_normal = zone_to_nid(zone);
  984. else
  985. arg->status_change_nid_normal = -1;
  986. #ifdef CONFIG_HIGHMEM
  987. /*
  988. * If we have movable node, node_states[N_HIGH_MEMORY]
  989. * contains nodes which have zones of 0...ZONE_HIGHMEM,
  990. * set zone_last to ZONE_HIGHMEM.
  991. *
  992. * If we don't have movable node, node_states[N_NORMAL_MEMORY]
  993. * contains nodes which have zones of 0...ZONE_MOVABLE,
  994. * set zone_last to ZONE_MOVABLE.
  995. */
  996. zone_last = ZONE_HIGHMEM;
  997. if (N_MEMORY == N_HIGH_MEMORY)
  998. zone_last = ZONE_MOVABLE;
  999. for (; zt <= zone_last; zt++)
  1000. present_pages += pgdat->node_zones[zt].present_pages;
  1001. if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
  1002. arg->status_change_nid_high = zone_to_nid(zone);
  1003. else
  1004. arg->status_change_nid_high = -1;
  1005. #else
  1006. arg->status_change_nid_high = arg->status_change_nid_normal;
  1007. #endif
  1008. /*
  1009. * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
  1010. */
  1011. zone_last = ZONE_MOVABLE;
  1012. /*
  1013. * check whether node_states[N_HIGH_MEMORY] will be changed
  1014. * If we try to offline the last present @nr_pages from the node,
  1015. * we can determind we will need to clear the node from
  1016. * node_states[N_HIGH_MEMORY].
  1017. */
  1018. for (; zt <= zone_last; zt++)
  1019. present_pages += pgdat->node_zones[zt].present_pages;
  1020. if (nr_pages >= present_pages)
  1021. arg->status_change_nid = zone_to_nid(zone);
  1022. else
  1023. arg->status_change_nid = -1;
  1024. }
  1025. static void node_states_clear_node(int node, struct memory_notify *arg)
  1026. {
  1027. if (arg->status_change_nid_normal >= 0)
  1028. node_clear_state(node, N_NORMAL_MEMORY);
  1029. if ((N_MEMORY != N_NORMAL_MEMORY) &&
  1030. (arg->status_change_nid_high >= 0))
  1031. node_clear_state(node, N_HIGH_MEMORY);
  1032. if ((N_MEMORY != N_HIGH_MEMORY) &&
  1033. (arg->status_change_nid >= 0))
  1034. node_clear_state(node, N_MEMORY);
  1035. }
  1036. static int __ref __offline_pages(unsigned long start_pfn,
  1037. unsigned long end_pfn, unsigned long timeout)
  1038. {
  1039. unsigned long pfn, nr_pages, expire;
  1040. long offlined_pages;
  1041. int ret, drain, retry_max, node;
  1042. struct zone *zone;
  1043. struct memory_notify arg;
  1044. BUG_ON(start_pfn >= end_pfn);
  1045. /* at least, alignment against pageblock is necessary */
  1046. if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
  1047. return -EINVAL;
  1048. if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
  1049. return -EINVAL;
  1050. /* This makes hotplug much easier...and readable.
  1051. we assume this for now. .*/
  1052. if (!test_pages_in_a_zone(start_pfn, end_pfn))
  1053. return -EINVAL;
  1054. lock_memory_hotplug();
  1055. zone = page_zone(pfn_to_page(start_pfn));
  1056. node = zone_to_nid(zone);
  1057. nr_pages = end_pfn - start_pfn;
  1058. ret = -EINVAL;
  1059. if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
  1060. goto out;
  1061. /* set above range as isolated */
  1062. ret = start_isolate_page_range(start_pfn, end_pfn,
  1063. MIGRATE_MOVABLE, true);
  1064. if (ret)
  1065. goto out;
  1066. arg.start_pfn = start_pfn;
  1067. arg.nr_pages = nr_pages;
  1068. node_states_check_changes_offline(nr_pages, zone, &arg);
  1069. ret = memory_notify(MEM_GOING_OFFLINE, &arg);
  1070. ret = notifier_to_errno(ret);
  1071. if (ret)
  1072. goto failed_removal;
  1073. pfn = start_pfn;
  1074. expire = jiffies + timeout;
  1075. drain = 0;
  1076. retry_max = 5;
  1077. repeat:
  1078. /* start memory hot removal */
  1079. ret = -EAGAIN;
  1080. if (time_after(jiffies, expire))
  1081. goto failed_removal;
  1082. ret = -EINTR;
  1083. if (signal_pending(current))
  1084. goto failed_removal;
  1085. ret = 0;
  1086. if (drain) {
  1087. lru_add_drain_all();
  1088. cond_resched();
  1089. drain_all_pages();
  1090. }
  1091. pfn = scan_lru_pages(start_pfn, end_pfn);
  1092. if (pfn) { /* We have page on LRU */
  1093. ret = do_migrate_range(pfn, end_pfn);
  1094. if (!ret) {
  1095. drain = 1;
  1096. goto repeat;
  1097. } else {
  1098. if (ret < 0)
  1099. if (--retry_max == 0)
  1100. goto failed_removal;
  1101. yield();
  1102. drain = 1;
  1103. goto repeat;
  1104. }
  1105. }
  1106. /* drain all zone's lru pagevec, this is asyncronous... */
  1107. lru_add_drain_all();
  1108. yield();
  1109. /* drain pcp pages , this is synchrouns. */
  1110. drain_all_pages();
  1111. /* check again */
  1112. offlined_pages = check_pages_isolated(start_pfn, end_pfn);
  1113. if (offlined_pages < 0) {
  1114. ret = -EBUSY;
  1115. goto failed_removal;
  1116. }
  1117. printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
  1118. /* Ok, all of our target is islaoted.
  1119. We cannot do rollback at this point. */
  1120. offline_isolated_pages(start_pfn, end_pfn);
  1121. /* reset pagetype flags and makes migrate type to be MOVABLE */
  1122. undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
  1123. /* removal success */
  1124. zone->present_pages -= offlined_pages;
  1125. zone->zone_pgdat->node_present_pages -= offlined_pages;
  1126. totalram_pages -= offlined_pages;
  1127. init_per_zone_wmark_min();
  1128. if (!populated_zone(zone)) {
  1129. zone_pcp_reset(zone);
  1130. mutex_lock(&zonelists_mutex);
  1131. build_all_zonelists(NULL, NULL);
  1132. mutex_unlock(&zonelists_mutex);
  1133. } else
  1134. zone_pcp_update(zone);
  1135. node_states_clear_node(node, &arg);
  1136. if (arg.status_change_nid >= 0)
  1137. kswapd_stop(node);
  1138. vm_total_pages = nr_free_pagecache_pages();
  1139. writeback_set_ratelimit();
  1140. memory_notify(MEM_OFFLINE, &arg);
  1141. unlock_memory_hotplug();
  1142. return 0;
  1143. failed_removal:
  1144. printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
  1145. (unsigned long long) start_pfn << PAGE_SHIFT,
  1146. ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
  1147. memory_notify(MEM_CANCEL_OFFLINE, &arg);
  1148. /* pushback to free area */
  1149. undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
  1150. out:
  1151. unlock_memory_hotplug();
  1152. return ret;
  1153. }
  1154. int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
  1155. {
  1156. return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
  1157. }
  1158. int remove_memory(u64 start, u64 size)
  1159. {
  1160. struct memory_block *mem = NULL;
  1161. struct mem_section *section;
  1162. unsigned long start_pfn, end_pfn;
  1163. unsigned long pfn, section_nr;
  1164. int ret;
  1165. start_pfn = PFN_DOWN(start);
  1166. end_pfn = start_pfn + PFN_DOWN(size);
  1167. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  1168. section_nr = pfn_to_section_nr(pfn);
  1169. if (!present_section_nr(section_nr))
  1170. continue;
  1171. section = __nr_to_section(section_nr);
  1172. /* same memblock? */
  1173. if (mem)
  1174. if ((section_nr >= mem->start_section_nr) &&
  1175. (section_nr <= mem->end_section_nr))
  1176. continue;
  1177. mem = find_memory_block_hinted(section, mem);
  1178. if (!mem)
  1179. continue;
  1180. ret = offline_memory_block(mem);
  1181. if (ret) {
  1182. kobject_put(&mem->dev.kobj);
  1183. return ret;
  1184. }
  1185. }
  1186. if (mem)
  1187. kobject_put(&mem->dev.kobj);
  1188. return 0;
  1189. }
  1190. #else
  1191. int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
  1192. {
  1193. return -EINVAL;
  1194. }
  1195. int remove_memory(u64 start, u64 size)
  1196. {
  1197. return -EINVAL;
  1198. }
  1199. #endif /* CONFIG_MEMORY_HOTREMOVE */
  1200. EXPORT_SYMBOL_GPL(remove_memory);