memory_hotplug.c 21 KB

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