memory_hotplug.c 22 KB

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