memory_hotplug.c 21 KB

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