memory_hotplug.c 22 KB

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