memory_hotplug.c 22 KB

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