memory_hotplug.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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 <asm/tlbflush.h>
  29. /* add this memory to iomem resource */
  30. static struct resource *register_memory_resource(u64 start, u64 size)
  31. {
  32. struct resource *res;
  33. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  34. BUG_ON(!res);
  35. res->name = "System RAM";
  36. res->start = start;
  37. res->end = start + size - 1;
  38. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  39. if (request_resource(&iomem_resource, res) < 0) {
  40. printk("System RAM resource %llx - %llx cannot be added\n",
  41. (unsigned long long)res->start, (unsigned long long)res->end);
  42. kfree(res);
  43. res = NULL;
  44. }
  45. return res;
  46. }
  47. static void release_memory_resource(struct resource *res)
  48. {
  49. if (!res)
  50. return;
  51. release_resource(res);
  52. kfree(res);
  53. return;
  54. }
  55. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  56. static int __add_zone(struct zone *zone, unsigned long phys_start_pfn)
  57. {
  58. struct pglist_data *pgdat = zone->zone_pgdat;
  59. int nr_pages = PAGES_PER_SECTION;
  60. int nid = pgdat->node_id;
  61. int zone_type;
  62. zone_type = zone - pgdat->node_zones;
  63. if (!zone->wait_table) {
  64. int ret = 0;
  65. ret = init_currently_empty_zone(zone, phys_start_pfn,
  66. nr_pages, MEMMAP_HOTPLUG);
  67. if (ret < 0)
  68. return ret;
  69. }
  70. memmap_init_zone(nr_pages, nid, zone_type,
  71. phys_start_pfn, MEMMAP_HOTPLUG);
  72. return 0;
  73. }
  74. static int __add_section(struct zone *zone, unsigned long phys_start_pfn)
  75. {
  76. int nr_pages = PAGES_PER_SECTION;
  77. int ret;
  78. if (pfn_valid(phys_start_pfn))
  79. return -EEXIST;
  80. ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
  81. if (ret < 0)
  82. return ret;
  83. ret = __add_zone(zone, phys_start_pfn);
  84. if (ret < 0)
  85. return ret;
  86. return register_new_memory(__pfn_to_section(phys_start_pfn));
  87. }
  88. static int __remove_section(struct zone *zone, struct mem_section *ms)
  89. {
  90. unsigned long flags;
  91. struct pglist_data *pgdat = zone->zone_pgdat;
  92. int ret = -EINVAL;
  93. if (!valid_section(ms))
  94. return ret;
  95. ret = unregister_memory_section(ms);
  96. if (ret)
  97. return ret;
  98. pgdat_resize_lock(pgdat, &flags);
  99. sparse_remove_one_section(zone, ms);
  100. pgdat_resize_unlock(pgdat, &flags);
  101. return 0;
  102. }
  103. /*
  104. * Reasonably generic function for adding memory. It is
  105. * expected that archs that support memory hotplug will
  106. * call this function after deciding the zone to which to
  107. * add the new pages.
  108. */
  109. int __add_pages(struct zone *zone, unsigned long phys_start_pfn,
  110. unsigned long nr_pages)
  111. {
  112. unsigned long i;
  113. int err = 0;
  114. int start_sec, end_sec;
  115. /* during initialize mem_map, align hot-added range to section */
  116. start_sec = pfn_to_section_nr(phys_start_pfn);
  117. end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
  118. for (i = start_sec; i <= end_sec; i++) {
  119. err = __add_section(zone, i << PFN_SECTION_SHIFT);
  120. /*
  121. * EEXIST is finally dealt with by ioresource collision
  122. * check. see add_memory() => register_memory_resource()
  123. * Warning will be printed if there is collision.
  124. */
  125. if (err && (err != -EEXIST))
  126. break;
  127. err = 0;
  128. }
  129. return err;
  130. }
  131. EXPORT_SYMBOL_GPL(__add_pages);
  132. /**
  133. * __remove_pages() - remove sections of pages from a zone
  134. * @zone: zone from which pages need to be removed
  135. * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
  136. * @nr_pages: number of pages to remove (must be multiple of section size)
  137. *
  138. * Generic helper function to remove section mappings and sysfs entries
  139. * for the section of the memory we are removing. Caller needs to make
  140. * sure that pages are marked reserved and zones are adjust properly by
  141. * calling offline_pages().
  142. */
  143. int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
  144. unsigned long nr_pages)
  145. {
  146. unsigned long i, ret = 0;
  147. int sections_to_remove;
  148. /*
  149. * We can only remove entire sections
  150. */
  151. BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
  152. BUG_ON(nr_pages % PAGES_PER_SECTION);
  153. release_mem_region(phys_start_pfn << PAGE_SHIFT, nr_pages * PAGE_SIZE);
  154. sections_to_remove = nr_pages / PAGES_PER_SECTION;
  155. for (i = 0; i < sections_to_remove; i++) {
  156. unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
  157. ret = __remove_section(zone, __pfn_to_section(pfn));
  158. if (ret)
  159. break;
  160. }
  161. return ret;
  162. }
  163. EXPORT_SYMBOL_GPL(__remove_pages);
  164. static void grow_zone_span(struct zone *zone,
  165. unsigned long start_pfn, unsigned long end_pfn)
  166. {
  167. unsigned long old_zone_end_pfn;
  168. zone_span_writelock(zone);
  169. old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
  170. if (start_pfn < zone->zone_start_pfn)
  171. zone->zone_start_pfn = start_pfn;
  172. zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
  173. zone->zone_start_pfn;
  174. zone_span_writeunlock(zone);
  175. }
  176. static void grow_pgdat_span(struct pglist_data *pgdat,
  177. unsigned long start_pfn, unsigned long end_pfn)
  178. {
  179. unsigned long old_pgdat_end_pfn =
  180. pgdat->node_start_pfn + pgdat->node_spanned_pages;
  181. if (start_pfn < pgdat->node_start_pfn)
  182. pgdat->node_start_pfn = start_pfn;
  183. pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
  184. pgdat->node_start_pfn;
  185. }
  186. static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
  187. void *arg)
  188. {
  189. unsigned long i;
  190. unsigned long onlined_pages = *(unsigned long *)arg;
  191. struct page *page;
  192. if (PageReserved(pfn_to_page(start_pfn)))
  193. for (i = 0; i < nr_pages; i++) {
  194. page = pfn_to_page(start_pfn + i);
  195. online_page(page);
  196. onlined_pages++;
  197. }
  198. *(unsigned long *)arg = onlined_pages;
  199. return 0;
  200. }
  201. int online_pages(unsigned long pfn, unsigned long nr_pages)
  202. {
  203. unsigned long flags;
  204. unsigned long onlined_pages = 0;
  205. struct zone *zone;
  206. int need_zonelists_rebuild = 0;
  207. int nid;
  208. int ret;
  209. struct memory_notify arg;
  210. arg.start_pfn = pfn;
  211. arg.nr_pages = nr_pages;
  212. arg.status_change_nid = -1;
  213. nid = page_to_nid(pfn_to_page(pfn));
  214. if (node_present_pages(nid) == 0)
  215. arg.status_change_nid = nid;
  216. ret = memory_notify(MEM_GOING_ONLINE, &arg);
  217. ret = notifier_to_errno(ret);
  218. if (ret) {
  219. memory_notify(MEM_CANCEL_ONLINE, &arg);
  220. return ret;
  221. }
  222. /*
  223. * This doesn't need a lock to do pfn_to_page().
  224. * The section can't be removed here because of the
  225. * memory_block->state_mutex.
  226. */
  227. zone = page_zone(pfn_to_page(pfn));
  228. pgdat_resize_lock(zone->zone_pgdat, &flags);
  229. grow_zone_span(zone, pfn, pfn + nr_pages);
  230. grow_pgdat_span(zone->zone_pgdat, pfn, pfn + nr_pages);
  231. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  232. /*
  233. * If this zone is not populated, then it is not in zonelist.
  234. * This means the page allocator ignores this zone.
  235. * So, zonelist must be updated after online.
  236. */
  237. if (!populated_zone(zone))
  238. need_zonelists_rebuild = 1;
  239. walk_memory_resource(pfn, nr_pages, &onlined_pages,
  240. online_pages_range);
  241. zone->present_pages += onlined_pages;
  242. zone->zone_pgdat->node_present_pages += onlined_pages;
  243. setup_per_zone_pages_min();
  244. if (onlined_pages) {
  245. kswapd_run(zone_to_nid(zone));
  246. node_set_state(zone_to_nid(zone), N_HIGH_MEMORY);
  247. }
  248. if (need_zonelists_rebuild)
  249. build_all_zonelists();
  250. vm_total_pages = nr_free_pagecache_pages();
  251. writeback_set_ratelimit();
  252. if (onlined_pages)
  253. memory_notify(MEM_ONLINE, &arg);
  254. return 0;
  255. }
  256. #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
  257. static pg_data_t *hotadd_new_pgdat(int nid, u64 start)
  258. {
  259. struct pglist_data *pgdat;
  260. unsigned long zones_size[MAX_NR_ZONES] = {0};
  261. unsigned long zholes_size[MAX_NR_ZONES] = {0};
  262. unsigned long start_pfn = start >> PAGE_SHIFT;
  263. pgdat = arch_alloc_nodedata(nid);
  264. if (!pgdat)
  265. return NULL;
  266. arch_refresh_nodedata(nid, pgdat);
  267. /* we can use NODE_DATA(nid) from here */
  268. /* init node's zones as empty zones, we don't have any present pages.*/
  269. free_area_init_node(nid, pgdat, zones_size, start_pfn, zholes_size);
  270. return pgdat;
  271. }
  272. static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
  273. {
  274. arch_refresh_nodedata(nid, NULL);
  275. arch_free_nodedata(pgdat);
  276. return;
  277. }
  278. int add_memory(int nid, u64 start, u64 size)
  279. {
  280. pg_data_t *pgdat = NULL;
  281. int new_pgdat = 0;
  282. struct resource *res;
  283. int ret;
  284. res = register_memory_resource(start, size);
  285. if (!res)
  286. return -EEXIST;
  287. if (!node_online(nid)) {
  288. pgdat = hotadd_new_pgdat(nid, start);
  289. if (!pgdat)
  290. return -ENOMEM;
  291. new_pgdat = 1;
  292. }
  293. /* call arch's memory hotadd */
  294. ret = arch_add_memory(nid, start, size);
  295. if (ret < 0)
  296. goto error;
  297. /* we online node here. we can't roll back from here. */
  298. node_set_online(nid);
  299. cpuset_track_online_nodes();
  300. if (new_pgdat) {
  301. ret = register_one_node(nid);
  302. /*
  303. * If sysfs file of new node can't create, cpu on the node
  304. * can't be hot-added. There is no rollback way now.
  305. * So, check by BUG_ON() to catch it reluctantly..
  306. */
  307. BUG_ON(ret);
  308. }
  309. return ret;
  310. error:
  311. /* rollback pgdat allocation and others */
  312. if (new_pgdat)
  313. rollback_node_hotadd(nid, pgdat);
  314. if (res)
  315. release_memory_resource(res);
  316. return ret;
  317. }
  318. EXPORT_SYMBOL_GPL(add_memory);
  319. #ifdef CONFIG_MEMORY_HOTREMOVE
  320. /*
  321. * Confirm all pages in a range [start, end) is belongs to the same zone.
  322. */
  323. static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
  324. {
  325. unsigned long pfn;
  326. struct zone *zone = NULL;
  327. struct page *page;
  328. int i;
  329. for (pfn = start_pfn;
  330. pfn < end_pfn;
  331. pfn += MAX_ORDER_NR_PAGES) {
  332. i = 0;
  333. /* This is just a CONFIG_HOLES_IN_ZONE check.*/
  334. while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
  335. i++;
  336. if (i == MAX_ORDER_NR_PAGES)
  337. continue;
  338. page = pfn_to_page(pfn + i);
  339. if (zone && page_zone(page) != zone)
  340. return 0;
  341. zone = page_zone(page);
  342. }
  343. return 1;
  344. }
  345. /*
  346. * Scanning pfn is much easier than scanning lru list.
  347. * Scan pfn from start to end and Find LRU page.
  348. */
  349. int scan_lru_pages(unsigned long start, unsigned long end)
  350. {
  351. unsigned long pfn;
  352. struct page *page;
  353. for (pfn = start; pfn < end; pfn++) {
  354. if (pfn_valid(pfn)) {
  355. page = pfn_to_page(pfn);
  356. if (PageLRU(page))
  357. return pfn;
  358. }
  359. }
  360. return 0;
  361. }
  362. static struct page *
  363. hotremove_migrate_alloc(struct page *page,
  364. unsigned long private,
  365. int **x)
  366. {
  367. /* This should be improoooooved!! */
  368. return alloc_page(GFP_HIGHUSER_PAGECACHE);
  369. }
  370. #define NR_OFFLINE_AT_ONCE_PAGES (256)
  371. static int
  372. do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
  373. {
  374. unsigned long pfn;
  375. struct page *page;
  376. int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
  377. int not_managed = 0;
  378. int ret = 0;
  379. LIST_HEAD(source);
  380. for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
  381. if (!pfn_valid(pfn))
  382. continue;
  383. page = pfn_to_page(pfn);
  384. if (!page_count(page))
  385. continue;
  386. /*
  387. * We can skip free pages. And we can only deal with pages on
  388. * LRU.
  389. */
  390. ret = isolate_lru_page(page, &source);
  391. if (!ret) { /* Success */
  392. move_pages--;
  393. } else {
  394. /* Becasue we don't have big zone->lock. we should
  395. check this again here. */
  396. if (page_count(page))
  397. not_managed++;
  398. #ifdef CONFIG_DEBUG_VM
  399. printk(KERN_INFO "removing from LRU failed"
  400. " %lx/%d/%lx\n",
  401. pfn, page_count(page), page->flags);
  402. #endif
  403. }
  404. }
  405. ret = -EBUSY;
  406. if (not_managed) {
  407. if (!list_empty(&source))
  408. putback_lru_pages(&source);
  409. goto out;
  410. }
  411. ret = 0;
  412. if (list_empty(&source))
  413. goto out;
  414. /* this function returns # of failed pages */
  415. ret = migrate_pages(&source, hotremove_migrate_alloc, 0);
  416. out:
  417. return ret;
  418. }
  419. /*
  420. * remove from free_area[] and mark all as Reserved.
  421. */
  422. static int
  423. offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
  424. void *data)
  425. {
  426. __offline_isolated_pages(start, start + nr_pages);
  427. return 0;
  428. }
  429. static void
  430. offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
  431. {
  432. walk_memory_resource(start_pfn, end_pfn - start_pfn, NULL,
  433. offline_isolated_pages_cb);
  434. }
  435. /*
  436. * Check all pages in range, recoreded as memory resource, are isolated.
  437. */
  438. static int
  439. check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
  440. void *data)
  441. {
  442. int ret;
  443. long offlined = *(long *)data;
  444. ret = test_pages_isolated(start_pfn, start_pfn + nr_pages);
  445. offlined = nr_pages;
  446. if (!ret)
  447. *(long *)data += offlined;
  448. return ret;
  449. }
  450. static long
  451. check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
  452. {
  453. long offlined = 0;
  454. int ret;
  455. ret = walk_memory_resource(start_pfn, end_pfn - start_pfn, &offlined,
  456. check_pages_isolated_cb);
  457. if (ret < 0)
  458. offlined = (long)ret;
  459. return offlined;
  460. }
  461. int offline_pages(unsigned long start_pfn,
  462. unsigned long end_pfn, unsigned long timeout)
  463. {
  464. unsigned long pfn, nr_pages, expire;
  465. long offlined_pages;
  466. int ret, drain, retry_max, node;
  467. struct zone *zone;
  468. struct memory_notify arg;
  469. BUG_ON(start_pfn >= end_pfn);
  470. /* at least, alignment against pageblock is necessary */
  471. if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
  472. return -EINVAL;
  473. if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
  474. return -EINVAL;
  475. /* This makes hotplug much easier...and readable.
  476. we assume this for now. .*/
  477. if (!test_pages_in_a_zone(start_pfn, end_pfn))
  478. return -EINVAL;
  479. zone = page_zone(pfn_to_page(start_pfn));
  480. node = zone_to_nid(zone);
  481. nr_pages = end_pfn - start_pfn;
  482. /* set above range as isolated */
  483. ret = start_isolate_page_range(start_pfn, end_pfn);
  484. if (ret)
  485. return ret;
  486. arg.start_pfn = start_pfn;
  487. arg.nr_pages = nr_pages;
  488. arg.status_change_nid = -1;
  489. if (nr_pages >= node_present_pages(node))
  490. arg.status_change_nid = node;
  491. ret = memory_notify(MEM_GOING_OFFLINE, &arg);
  492. ret = notifier_to_errno(ret);
  493. if (ret)
  494. goto failed_removal;
  495. pfn = start_pfn;
  496. expire = jiffies + timeout;
  497. drain = 0;
  498. retry_max = 5;
  499. repeat:
  500. /* start memory hot removal */
  501. ret = -EAGAIN;
  502. if (time_after(jiffies, expire))
  503. goto failed_removal;
  504. ret = -EINTR;
  505. if (signal_pending(current))
  506. goto failed_removal;
  507. ret = 0;
  508. if (drain) {
  509. lru_add_drain_all();
  510. flush_scheduled_work();
  511. cond_resched();
  512. drain_all_pages();
  513. }
  514. pfn = scan_lru_pages(start_pfn, end_pfn);
  515. if (pfn) { /* We have page on LRU */
  516. ret = do_migrate_range(pfn, end_pfn);
  517. if (!ret) {
  518. drain = 1;
  519. goto repeat;
  520. } else {
  521. if (ret < 0)
  522. if (--retry_max == 0)
  523. goto failed_removal;
  524. yield();
  525. drain = 1;
  526. goto repeat;
  527. }
  528. }
  529. /* drain all zone's lru pagevec, this is asyncronous... */
  530. lru_add_drain_all();
  531. flush_scheduled_work();
  532. yield();
  533. /* drain pcp pages , this is synchrouns. */
  534. drain_all_pages();
  535. /* check again */
  536. offlined_pages = check_pages_isolated(start_pfn, end_pfn);
  537. if (offlined_pages < 0) {
  538. ret = -EBUSY;
  539. goto failed_removal;
  540. }
  541. printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
  542. /* Ok, all of our target is islaoted.
  543. We cannot do rollback at this point. */
  544. offline_isolated_pages(start_pfn, end_pfn);
  545. /* reset pagetype flags and makes migrate type to be MOVABLE */
  546. undo_isolate_page_range(start_pfn, end_pfn);
  547. /* removal success */
  548. zone->present_pages -= offlined_pages;
  549. zone->zone_pgdat->node_present_pages -= offlined_pages;
  550. totalram_pages -= offlined_pages;
  551. num_physpages -= offlined_pages;
  552. vm_total_pages = nr_free_pagecache_pages();
  553. writeback_set_ratelimit();
  554. memory_notify(MEM_OFFLINE, &arg);
  555. return 0;
  556. failed_removal:
  557. printk(KERN_INFO "memory offlining %lx to %lx failed\n",
  558. start_pfn, end_pfn);
  559. memory_notify(MEM_CANCEL_OFFLINE, &arg);
  560. /* pushback to free area */
  561. undo_isolate_page_range(start_pfn, end_pfn);
  562. return ret;
  563. }
  564. #else
  565. int remove_memory(u64 start, u64 size)
  566. {
  567. return -EINVAL;
  568. }
  569. EXPORT_SYMBOL_GPL(remove_memory);
  570. #endif /* CONFIG_MEMORY_HOTREMOVE */