memory_hotplug.c 21 KB

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