memory_hotplug.c 21 KB

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