memory_hotplug.c 19 KB

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