memory_hotplug.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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/module.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/cpuset.h>
  25. #include <linux/delay.h>
  26. #include <linux/migrate.h>
  27. #include <linux/page-isolation.h>
  28. #include <asm/tlbflush.h>
  29. /* add this memory to iomem resource */
  30. static struct resource *register_memory_resource(u64 start, u64 size)
  31. {
  32. struct resource *res;
  33. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  34. BUG_ON(!res);
  35. res->name = "System RAM";
  36. res->start = start;
  37. res->end = start + size - 1;
  38. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  39. if (request_resource(&iomem_resource, res) < 0) {
  40. printk("System RAM resource %llx - %llx cannot be added\n",
  41. (unsigned long long)res->start, (unsigned long long)res->end);
  42. kfree(res);
  43. res = NULL;
  44. }
  45. return res;
  46. }
  47. static void release_memory_resource(struct resource *res)
  48. {
  49. if (!res)
  50. return;
  51. release_resource(res);
  52. kfree(res);
  53. return;
  54. }
  55. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  56. #ifndef CONFIG_SPARSEMEM_VMEMMAP
  57. static void get_page_bootmem(unsigned long info, struct page *page, int magic)
  58. {
  59. atomic_set(&page->_mapcount, magic);
  60. SetPagePrivate(page);
  61. set_page_private(page, info);
  62. atomic_inc(&page->_count);
  63. }
  64. void put_page_bootmem(struct page *page)
  65. {
  66. int magic;
  67. magic = atomic_read(&page->_mapcount);
  68. BUG_ON(magic >= -1);
  69. if (atomic_dec_return(&page->_count) == 1) {
  70. ClearPagePrivate(page);
  71. set_page_private(page, 0);
  72. reset_page_mapcount(page);
  73. __free_pages_bootmem(page, 0);
  74. }
  75. }
  76. void register_page_bootmem_info_section(unsigned long start_pfn)
  77. {
  78. unsigned long *usemap, mapsize, section_nr, i;
  79. struct mem_section *ms;
  80. struct page *page, *memmap;
  81. if (!pfn_valid(start_pfn))
  82. return;
  83. section_nr = pfn_to_section_nr(start_pfn);
  84. ms = __nr_to_section(section_nr);
  85. /* Get section's memmap address */
  86. memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
  87. /*
  88. * Get page for the memmap's phys address
  89. * XXX: need more consideration for sparse_vmemmap...
  90. */
  91. page = virt_to_page(memmap);
  92. mapsize = sizeof(struct page) * PAGES_PER_SECTION;
  93. mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
  94. /* remember memmap's page */
  95. for (i = 0; i < mapsize; i++, page++)
  96. get_page_bootmem(section_nr, page, SECTION_INFO);
  97. usemap = __nr_to_section(section_nr)->pageblock_flags;
  98. page = virt_to_page(usemap);
  99. mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
  100. for (i = 0; i < mapsize; i++, page++)
  101. get_page_bootmem(section_nr, page, MIX_INFO);
  102. }
  103. void register_page_bootmem_info_node(struct pglist_data *pgdat)
  104. {
  105. unsigned long i, pfn, end_pfn, nr_pages;
  106. int node = pgdat->node_id;
  107. struct page *page;
  108. struct zone *zone;
  109. nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
  110. page = virt_to_page(pgdat);
  111. for (i = 0; i < nr_pages; i++, page++)
  112. get_page_bootmem(node, page, NODE_INFO);
  113. zone = &pgdat->node_zones[0];
  114. for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
  115. if (zone->wait_table) {
  116. nr_pages = zone->wait_table_hash_nr_entries
  117. * sizeof(wait_queue_head_t);
  118. nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
  119. page = virt_to_page(zone->wait_table);
  120. for (i = 0; i < nr_pages; i++, page++)
  121. get_page_bootmem(node, page, NODE_INFO);
  122. }
  123. }
  124. pfn = pgdat->node_start_pfn;
  125. end_pfn = pfn + pgdat->node_spanned_pages;
  126. /* register_section info */
  127. for (; pfn < end_pfn; pfn += PAGES_PER_SECTION)
  128. register_page_bootmem_info_section(pfn);
  129. }
  130. #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
  131. static int __add_zone(struct zone *zone, unsigned long phys_start_pfn)
  132. {
  133. struct pglist_data *pgdat = zone->zone_pgdat;
  134. int nr_pages = PAGES_PER_SECTION;
  135. int nid = pgdat->node_id;
  136. int zone_type;
  137. zone_type = zone - pgdat->node_zones;
  138. if (!zone->wait_table) {
  139. int ret = 0;
  140. ret = init_currently_empty_zone(zone, phys_start_pfn,
  141. nr_pages, MEMMAP_HOTPLUG);
  142. if (ret < 0)
  143. return ret;
  144. }
  145. memmap_init_zone(nr_pages, nid, zone_type,
  146. phys_start_pfn, MEMMAP_HOTPLUG);
  147. return 0;
  148. }
  149. static int __add_section(struct zone *zone, unsigned long phys_start_pfn)
  150. {
  151. int nr_pages = PAGES_PER_SECTION;
  152. int ret;
  153. if (pfn_valid(phys_start_pfn))
  154. return -EEXIST;
  155. ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
  156. if (ret < 0)
  157. return ret;
  158. ret = __add_zone(zone, phys_start_pfn);
  159. if (ret < 0)
  160. return ret;
  161. return register_new_memory(__pfn_to_section(phys_start_pfn));
  162. }
  163. static int __remove_section(struct zone *zone, struct mem_section *ms)
  164. {
  165. unsigned long flags;
  166. struct pglist_data *pgdat = zone->zone_pgdat;
  167. int ret = -EINVAL;
  168. if (!valid_section(ms))
  169. return ret;
  170. ret = unregister_memory_section(ms);
  171. if (ret)
  172. return ret;
  173. pgdat_resize_lock(pgdat, &flags);
  174. sparse_remove_one_section(zone, ms);
  175. pgdat_resize_unlock(pgdat, &flags);
  176. return 0;
  177. }
  178. /*
  179. * Reasonably generic function for adding memory. It is
  180. * expected that archs that support memory hotplug will
  181. * call this function after deciding the zone to which to
  182. * add the new pages.
  183. */
  184. int __add_pages(struct zone *zone, unsigned long phys_start_pfn,
  185. unsigned long nr_pages)
  186. {
  187. unsigned long i;
  188. int err = 0;
  189. int start_sec, end_sec;
  190. /* during initialize mem_map, align hot-added range to section */
  191. start_sec = pfn_to_section_nr(phys_start_pfn);
  192. end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
  193. for (i = start_sec; i <= end_sec; i++) {
  194. err = __add_section(zone, i << PFN_SECTION_SHIFT);
  195. /*
  196. * EEXIST is finally dealt with by ioresource collision
  197. * check. see add_memory() => register_memory_resource()
  198. * Warning will be printed if there is collision.
  199. */
  200. if (err && (err != -EEXIST))
  201. break;
  202. err = 0;
  203. }
  204. return err;
  205. }
  206. EXPORT_SYMBOL_GPL(__add_pages);
  207. /**
  208. * __remove_pages() - remove sections of pages from a zone
  209. * @zone: zone from which pages need to be removed
  210. * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
  211. * @nr_pages: number of pages to remove (must be multiple of section size)
  212. *
  213. * Generic helper function to remove section mappings and sysfs entries
  214. * for the section of the memory we are removing. Caller needs to make
  215. * sure that pages are marked reserved and zones are adjust properly by
  216. * calling offline_pages().
  217. */
  218. int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
  219. unsigned long nr_pages)
  220. {
  221. unsigned long i, ret = 0;
  222. int sections_to_remove;
  223. /*
  224. * We can only remove entire sections
  225. */
  226. BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
  227. BUG_ON(nr_pages % PAGES_PER_SECTION);
  228. release_mem_region(phys_start_pfn << PAGE_SHIFT, nr_pages * PAGE_SIZE);
  229. sections_to_remove = nr_pages / PAGES_PER_SECTION;
  230. for (i = 0; i < sections_to_remove; i++) {
  231. unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
  232. ret = __remove_section(zone, __pfn_to_section(pfn));
  233. if (ret)
  234. break;
  235. }
  236. return ret;
  237. }
  238. EXPORT_SYMBOL_GPL(__remove_pages);
  239. static void grow_zone_span(struct zone *zone,
  240. unsigned long start_pfn, unsigned long end_pfn)
  241. {
  242. unsigned long old_zone_end_pfn;
  243. zone_span_writelock(zone);
  244. old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
  245. if (start_pfn < zone->zone_start_pfn)
  246. zone->zone_start_pfn = start_pfn;
  247. zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
  248. zone->zone_start_pfn;
  249. zone_span_writeunlock(zone);
  250. }
  251. static void grow_pgdat_span(struct pglist_data *pgdat,
  252. unsigned long start_pfn, unsigned long end_pfn)
  253. {
  254. unsigned long old_pgdat_end_pfn =
  255. pgdat->node_start_pfn + pgdat->node_spanned_pages;
  256. if (start_pfn < pgdat->node_start_pfn)
  257. pgdat->node_start_pfn = start_pfn;
  258. pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
  259. pgdat->node_start_pfn;
  260. }
  261. void online_page(struct page *page)
  262. {
  263. totalram_pages++;
  264. num_physpages++;
  265. #ifdef CONFIG_HIGHMEM
  266. if (PageHighMem(page))
  267. totalhigh_pages++;
  268. #endif
  269. #ifdef CONFIG_FLATMEM
  270. max_mapnr = max(page_to_pfn(page), max_mapnr);
  271. #endif
  272. ClearPageReserved(page);
  273. init_page_count(page);
  274. __free_page(page);
  275. }
  276. static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
  277. void *arg)
  278. {
  279. unsigned long i;
  280. unsigned long onlined_pages = *(unsigned long *)arg;
  281. struct page *page;
  282. if (PageReserved(pfn_to_page(start_pfn)))
  283. for (i = 0; i < nr_pages; i++) {
  284. page = pfn_to_page(start_pfn + i);
  285. online_page(page);
  286. onlined_pages++;
  287. }
  288. *(unsigned long *)arg = onlined_pages;
  289. return 0;
  290. }
  291. int online_pages(unsigned long pfn, unsigned long nr_pages)
  292. {
  293. unsigned long flags;
  294. unsigned long onlined_pages = 0;
  295. struct zone *zone;
  296. int need_zonelists_rebuild = 0;
  297. int nid;
  298. int ret;
  299. struct memory_notify arg;
  300. arg.start_pfn = pfn;
  301. arg.nr_pages = nr_pages;
  302. arg.status_change_nid = -1;
  303. nid = page_to_nid(pfn_to_page(pfn));
  304. if (node_present_pages(nid) == 0)
  305. arg.status_change_nid = nid;
  306. ret = memory_notify(MEM_GOING_ONLINE, &arg);
  307. ret = notifier_to_errno(ret);
  308. if (ret) {
  309. memory_notify(MEM_CANCEL_ONLINE, &arg);
  310. return ret;
  311. }
  312. /*
  313. * This doesn't need a lock to do pfn_to_page().
  314. * The section can't be removed here because of the
  315. * memory_block->state_mutex.
  316. */
  317. zone = page_zone(pfn_to_page(pfn));
  318. pgdat_resize_lock(zone->zone_pgdat, &flags);
  319. grow_zone_span(zone, pfn, pfn + nr_pages);
  320. grow_pgdat_span(zone->zone_pgdat, pfn, pfn + nr_pages);
  321. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  322. /*
  323. * If this zone is not populated, then it is not in zonelist.
  324. * This means the page allocator ignores this zone.
  325. * So, zonelist must be updated after online.
  326. */
  327. if (!populated_zone(zone))
  328. need_zonelists_rebuild = 1;
  329. walk_memory_resource(pfn, nr_pages, &onlined_pages,
  330. online_pages_range);
  331. zone->present_pages += onlined_pages;
  332. zone->zone_pgdat->node_present_pages += onlined_pages;
  333. setup_per_zone_pages_min();
  334. if (onlined_pages) {
  335. kswapd_run(zone_to_nid(zone));
  336. node_set_state(zone_to_nid(zone), N_HIGH_MEMORY);
  337. }
  338. if (need_zonelists_rebuild)
  339. build_all_zonelists();
  340. vm_total_pages = nr_free_pagecache_pages();
  341. writeback_set_ratelimit();
  342. if (onlined_pages)
  343. memory_notify(MEM_ONLINE, &arg);
  344. return 0;
  345. }
  346. #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
  347. static pg_data_t *hotadd_new_pgdat(int nid, u64 start)
  348. {
  349. struct pglist_data *pgdat;
  350. unsigned long zones_size[MAX_NR_ZONES] = {0};
  351. unsigned long zholes_size[MAX_NR_ZONES] = {0};
  352. unsigned long start_pfn = start >> PAGE_SHIFT;
  353. pgdat = arch_alloc_nodedata(nid);
  354. if (!pgdat)
  355. return NULL;
  356. arch_refresh_nodedata(nid, pgdat);
  357. /* we can use NODE_DATA(nid) from here */
  358. /* init node's zones as empty zones, we don't have any present pages.*/
  359. free_area_init_node(nid, pgdat, zones_size, start_pfn, zholes_size);
  360. return pgdat;
  361. }
  362. static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
  363. {
  364. arch_refresh_nodedata(nid, NULL);
  365. arch_free_nodedata(pgdat);
  366. return;
  367. }
  368. int add_memory(int nid, u64 start, u64 size)
  369. {
  370. pg_data_t *pgdat = NULL;
  371. int new_pgdat = 0;
  372. struct resource *res;
  373. int ret;
  374. res = register_memory_resource(start, size);
  375. if (!res)
  376. return -EEXIST;
  377. if (!node_online(nid)) {
  378. pgdat = hotadd_new_pgdat(nid, start);
  379. if (!pgdat)
  380. return -ENOMEM;
  381. new_pgdat = 1;
  382. }
  383. /* call arch's memory hotadd */
  384. ret = arch_add_memory(nid, start, size);
  385. if (ret < 0)
  386. goto error;
  387. /* we online node here. we can't roll back from here. */
  388. node_set_online(nid);
  389. cpuset_track_online_nodes();
  390. if (new_pgdat) {
  391. ret = register_one_node(nid);
  392. /*
  393. * If sysfs file of new node can't create, cpu on the node
  394. * can't be hot-added. There is no rollback way now.
  395. * So, check by BUG_ON() to catch it reluctantly..
  396. */
  397. BUG_ON(ret);
  398. }
  399. return ret;
  400. error:
  401. /* rollback pgdat allocation and others */
  402. if (new_pgdat)
  403. rollback_node_hotadd(nid, pgdat);
  404. if (res)
  405. release_memory_resource(res);
  406. return ret;
  407. }
  408. EXPORT_SYMBOL_GPL(add_memory);
  409. #ifdef CONFIG_MEMORY_HOTREMOVE
  410. /*
  411. * Confirm all pages in a range [start, end) is belongs to the same zone.
  412. */
  413. static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
  414. {
  415. unsigned long pfn;
  416. struct zone *zone = NULL;
  417. struct page *page;
  418. int i;
  419. for (pfn = start_pfn;
  420. pfn < end_pfn;
  421. pfn += MAX_ORDER_NR_PAGES) {
  422. i = 0;
  423. /* This is just a CONFIG_HOLES_IN_ZONE check.*/
  424. while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
  425. i++;
  426. if (i == MAX_ORDER_NR_PAGES)
  427. continue;
  428. page = pfn_to_page(pfn + i);
  429. if (zone && page_zone(page) != zone)
  430. return 0;
  431. zone = page_zone(page);
  432. }
  433. return 1;
  434. }
  435. /*
  436. * Scanning pfn is much easier than scanning lru list.
  437. * Scan pfn from start to end and Find LRU page.
  438. */
  439. int scan_lru_pages(unsigned long start, unsigned long end)
  440. {
  441. unsigned long pfn;
  442. struct page *page;
  443. for (pfn = start; pfn < end; pfn++) {
  444. if (pfn_valid(pfn)) {
  445. page = pfn_to_page(pfn);
  446. if (PageLRU(page))
  447. return pfn;
  448. }
  449. }
  450. return 0;
  451. }
  452. static struct page *
  453. hotremove_migrate_alloc(struct page *page,
  454. unsigned long private,
  455. int **x)
  456. {
  457. /* This should be improoooooved!! */
  458. return alloc_page(GFP_HIGHUSER_PAGECACHE);
  459. }
  460. #define NR_OFFLINE_AT_ONCE_PAGES (256)
  461. static int
  462. do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
  463. {
  464. unsigned long pfn;
  465. struct page *page;
  466. int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
  467. int not_managed = 0;
  468. int ret = 0;
  469. LIST_HEAD(source);
  470. for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
  471. if (!pfn_valid(pfn))
  472. continue;
  473. page = pfn_to_page(pfn);
  474. if (!page_count(page))
  475. continue;
  476. /*
  477. * We can skip free pages. And we can only deal with pages on
  478. * LRU.
  479. */
  480. ret = isolate_lru_page(page, &source);
  481. if (!ret) { /* Success */
  482. move_pages--;
  483. } else {
  484. /* Becasue we don't have big zone->lock. we should
  485. check this again here. */
  486. if (page_count(page))
  487. not_managed++;
  488. #ifdef CONFIG_DEBUG_VM
  489. printk(KERN_INFO "removing from LRU failed"
  490. " %lx/%d/%lx\n",
  491. pfn, page_count(page), page->flags);
  492. #endif
  493. }
  494. }
  495. ret = -EBUSY;
  496. if (not_managed) {
  497. if (!list_empty(&source))
  498. putback_lru_pages(&source);
  499. goto out;
  500. }
  501. ret = 0;
  502. if (list_empty(&source))
  503. goto out;
  504. /* this function returns # of failed pages */
  505. ret = migrate_pages(&source, hotremove_migrate_alloc, 0);
  506. out:
  507. return ret;
  508. }
  509. /*
  510. * remove from free_area[] and mark all as Reserved.
  511. */
  512. static int
  513. offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
  514. void *data)
  515. {
  516. __offline_isolated_pages(start, start + nr_pages);
  517. return 0;
  518. }
  519. static void
  520. offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
  521. {
  522. walk_memory_resource(start_pfn, end_pfn - start_pfn, NULL,
  523. offline_isolated_pages_cb);
  524. }
  525. /*
  526. * Check all pages in range, recoreded as memory resource, are isolated.
  527. */
  528. static int
  529. check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
  530. void *data)
  531. {
  532. int ret;
  533. long offlined = *(long *)data;
  534. ret = test_pages_isolated(start_pfn, start_pfn + nr_pages);
  535. offlined = nr_pages;
  536. if (!ret)
  537. *(long *)data += offlined;
  538. return ret;
  539. }
  540. static long
  541. check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
  542. {
  543. long offlined = 0;
  544. int ret;
  545. ret = walk_memory_resource(start_pfn, end_pfn - start_pfn, &offlined,
  546. check_pages_isolated_cb);
  547. if (ret < 0)
  548. offlined = (long)ret;
  549. return offlined;
  550. }
  551. int offline_pages(unsigned long start_pfn,
  552. unsigned long end_pfn, unsigned long timeout)
  553. {
  554. unsigned long pfn, nr_pages, expire;
  555. long offlined_pages;
  556. int ret, drain, retry_max, node;
  557. struct zone *zone;
  558. struct memory_notify arg;
  559. BUG_ON(start_pfn >= end_pfn);
  560. /* at least, alignment against pageblock is necessary */
  561. if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
  562. return -EINVAL;
  563. if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
  564. return -EINVAL;
  565. /* This makes hotplug much easier...and readable.
  566. we assume this for now. .*/
  567. if (!test_pages_in_a_zone(start_pfn, end_pfn))
  568. return -EINVAL;
  569. zone = page_zone(pfn_to_page(start_pfn));
  570. node = zone_to_nid(zone);
  571. nr_pages = end_pfn - start_pfn;
  572. /* set above range as isolated */
  573. ret = start_isolate_page_range(start_pfn, end_pfn);
  574. if (ret)
  575. return ret;
  576. arg.start_pfn = start_pfn;
  577. arg.nr_pages = nr_pages;
  578. arg.status_change_nid = -1;
  579. if (nr_pages >= node_present_pages(node))
  580. arg.status_change_nid = node;
  581. ret = memory_notify(MEM_GOING_OFFLINE, &arg);
  582. ret = notifier_to_errno(ret);
  583. if (ret)
  584. goto failed_removal;
  585. pfn = start_pfn;
  586. expire = jiffies + timeout;
  587. drain = 0;
  588. retry_max = 5;
  589. repeat:
  590. /* start memory hot removal */
  591. ret = -EAGAIN;
  592. if (time_after(jiffies, expire))
  593. goto failed_removal;
  594. ret = -EINTR;
  595. if (signal_pending(current))
  596. goto failed_removal;
  597. ret = 0;
  598. if (drain) {
  599. lru_add_drain_all();
  600. flush_scheduled_work();
  601. cond_resched();
  602. drain_all_pages();
  603. }
  604. pfn = scan_lru_pages(start_pfn, end_pfn);
  605. if (pfn) { /* We have page on LRU */
  606. ret = do_migrate_range(pfn, end_pfn);
  607. if (!ret) {
  608. drain = 1;
  609. goto repeat;
  610. } else {
  611. if (ret < 0)
  612. if (--retry_max == 0)
  613. goto failed_removal;
  614. yield();
  615. drain = 1;
  616. goto repeat;
  617. }
  618. }
  619. /* drain all zone's lru pagevec, this is asyncronous... */
  620. lru_add_drain_all();
  621. flush_scheduled_work();
  622. yield();
  623. /* drain pcp pages , this is synchrouns. */
  624. drain_all_pages();
  625. /* check again */
  626. offlined_pages = check_pages_isolated(start_pfn, end_pfn);
  627. if (offlined_pages < 0) {
  628. ret = -EBUSY;
  629. goto failed_removal;
  630. }
  631. printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
  632. /* Ok, all of our target is islaoted.
  633. We cannot do rollback at this point. */
  634. offline_isolated_pages(start_pfn, end_pfn);
  635. /* reset pagetype flags and makes migrate type to be MOVABLE */
  636. undo_isolate_page_range(start_pfn, end_pfn);
  637. /* removal success */
  638. zone->present_pages -= offlined_pages;
  639. zone->zone_pgdat->node_present_pages -= offlined_pages;
  640. totalram_pages -= offlined_pages;
  641. num_physpages -= offlined_pages;
  642. vm_total_pages = nr_free_pagecache_pages();
  643. writeback_set_ratelimit();
  644. memory_notify(MEM_OFFLINE, &arg);
  645. return 0;
  646. failed_removal:
  647. printk(KERN_INFO "memory offlining %lx to %lx failed\n",
  648. start_pfn, end_pfn);
  649. memory_notify(MEM_CANCEL_OFFLINE, &arg);
  650. /* pushback to free area */
  651. undo_isolate_page_range(start_pfn, end_pfn);
  652. return ret;
  653. }
  654. #else
  655. int remove_memory(u64 start, u64 size)
  656. {
  657. return -EINVAL;
  658. }
  659. EXPORT_SYMBOL_GPL(remove_memory);
  660. #endif /* CONFIG_MEMORY_HOTREMOVE */