memory_hotplug.c 22 KB

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