memory_hotplug.c 18 KB

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