memory_hotplug.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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. void online_page(struct page *page)
  187. {
  188. totalram_pages++;
  189. num_physpages++;
  190. #ifdef CONFIG_HIGHMEM
  191. if (PageHighMem(page))
  192. totalhigh_pages++;
  193. #endif
  194. #ifdef CONFIG_FLATMEM
  195. max_mapnr = max(page_to_pfn(page), max_mapnr);
  196. #endif
  197. ClearPageReserved(page);
  198. init_page_count(page);
  199. __free_page(page);
  200. }
  201. static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
  202. void *arg)
  203. {
  204. unsigned long i;
  205. unsigned long onlined_pages = *(unsigned long *)arg;
  206. struct page *page;
  207. if (PageReserved(pfn_to_page(start_pfn)))
  208. for (i = 0; i < nr_pages; i++) {
  209. page = pfn_to_page(start_pfn + i);
  210. online_page(page);
  211. onlined_pages++;
  212. }
  213. *(unsigned long *)arg = onlined_pages;
  214. return 0;
  215. }
  216. int online_pages(unsigned long pfn, unsigned long nr_pages)
  217. {
  218. unsigned long flags;
  219. unsigned long onlined_pages = 0;
  220. struct zone *zone;
  221. int need_zonelists_rebuild = 0;
  222. int nid;
  223. int ret;
  224. struct memory_notify arg;
  225. arg.start_pfn = pfn;
  226. arg.nr_pages = nr_pages;
  227. arg.status_change_nid = -1;
  228. nid = page_to_nid(pfn_to_page(pfn));
  229. if (node_present_pages(nid) == 0)
  230. arg.status_change_nid = nid;
  231. ret = memory_notify(MEM_GOING_ONLINE, &arg);
  232. ret = notifier_to_errno(ret);
  233. if (ret) {
  234. memory_notify(MEM_CANCEL_ONLINE, &arg);
  235. return ret;
  236. }
  237. /*
  238. * This doesn't need a lock to do pfn_to_page().
  239. * The section can't be removed here because of the
  240. * memory_block->state_mutex.
  241. */
  242. zone = page_zone(pfn_to_page(pfn));
  243. pgdat_resize_lock(zone->zone_pgdat, &flags);
  244. grow_zone_span(zone, pfn, pfn + nr_pages);
  245. grow_pgdat_span(zone->zone_pgdat, pfn, pfn + nr_pages);
  246. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  247. /*
  248. * If this zone is not populated, then it is not in zonelist.
  249. * This means the page allocator ignores this zone.
  250. * So, zonelist must be updated after online.
  251. */
  252. if (!populated_zone(zone))
  253. need_zonelists_rebuild = 1;
  254. walk_memory_resource(pfn, nr_pages, &onlined_pages,
  255. online_pages_range);
  256. zone->present_pages += onlined_pages;
  257. zone->zone_pgdat->node_present_pages += onlined_pages;
  258. setup_per_zone_pages_min();
  259. if (onlined_pages) {
  260. kswapd_run(zone_to_nid(zone));
  261. node_set_state(zone_to_nid(zone), N_HIGH_MEMORY);
  262. }
  263. if (need_zonelists_rebuild)
  264. build_all_zonelists();
  265. vm_total_pages = nr_free_pagecache_pages();
  266. writeback_set_ratelimit();
  267. if (onlined_pages)
  268. memory_notify(MEM_ONLINE, &arg);
  269. return 0;
  270. }
  271. #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
  272. static pg_data_t *hotadd_new_pgdat(int nid, u64 start)
  273. {
  274. struct pglist_data *pgdat;
  275. unsigned long zones_size[MAX_NR_ZONES] = {0};
  276. unsigned long zholes_size[MAX_NR_ZONES] = {0};
  277. unsigned long start_pfn = start >> PAGE_SHIFT;
  278. pgdat = arch_alloc_nodedata(nid);
  279. if (!pgdat)
  280. return NULL;
  281. arch_refresh_nodedata(nid, pgdat);
  282. /* we can use NODE_DATA(nid) from here */
  283. /* init node's zones as empty zones, we don't have any present pages.*/
  284. free_area_init_node(nid, pgdat, zones_size, start_pfn, zholes_size);
  285. return pgdat;
  286. }
  287. static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
  288. {
  289. arch_refresh_nodedata(nid, NULL);
  290. arch_free_nodedata(pgdat);
  291. return;
  292. }
  293. int add_memory(int nid, u64 start, u64 size)
  294. {
  295. pg_data_t *pgdat = NULL;
  296. int new_pgdat = 0;
  297. struct resource *res;
  298. int ret;
  299. res = register_memory_resource(start, size);
  300. if (!res)
  301. return -EEXIST;
  302. if (!node_online(nid)) {
  303. pgdat = hotadd_new_pgdat(nid, start);
  304. if (!pgdat)
  305. return -ENOMEM;
  306. new_pgdat = 1;
  307. }
  308. /* call arch's memory hotadd */
  309. ret = arch_add_memory(nid, start, size);
  310. if (ret < 0)
  311. goto error;
  312. /* we online node here. we can't roll back from here. */
  313. node_set_online(nid);
  314. cpuset_track_online_nodes();
  315. if (new_pgdat) {
  316. ret = register_one_node(nid);
  317. /*
  318. * If sysfs file of new node can't create, cpu on the node
  319. * can't be hot-added. There is no rollback way now.
  320. * So, check by BUG_ON() to catch it reluctantly..
  321. */
  322. BUG_ON(ret);
  323. }
  324. return ret;
  325. error:
  326. /* rollback pgdat allocation and others */
  327. if (new_pgdat)
  328. rollback_node_hotadd(nid, pgdat);
  329. if (res)
  330. release_memory_resource(res);
  331. return ret;
  332. }
  333. EXPORT_SYMBOL_GPL(add_memory);
  334. #ifdef CONFIG_MEMORY_HOTREMOVE
  335. /*
  336. * Confirm all pages in a range [start, end) is belongs to the same zone.
  337. */
  338. static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
  339. {
  340. unsigned long pfn;
  341. struct zone *zone = NULL;
  342. struct page *page;
  343. int i;
  344. for (pfn = start_pfn;
  345. pfn < end_pfn;
  346. pfn += MAX_ORDER_NR_PAGES) {
  347. i = 0;
  348. /* This is just a CONFIG_HOLES_IN_ZONE check.*/
  349. while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
  350. i++;
  351. if (i == MAX_ORDER_NR_PAGES)
  352. continue;
  353. page = pfn_to_page(pfn + i);
  354. if (zone && page_zone(page) != zone)
  355. return 0;
  356. zone = page_zone(page);
  357. }
  358. return 1;
  359. }
  360. /*
  361. * Scanning pfn is much easier than scanning lru list.
  362. * Scan pfn from start to end and Find LRU page.
  363. */
  364. int scan_lru_pages(unsigned long start, unsigned long end)
  365. {
  366. unsigned long pfn;
  367. struct page *page;
  368. for (pfn = start; pfn < end; pfn++) {
  369. if (pfn_valid(pfn)) {
  370. page = pfn_to_page(pfn);
  371. if (PageLRU(page))
  372. return pfn;
  373. }
  374. }
  375. return 0;
  376. }
  377. static struct page *
  378. hotremove_migrate_alloc(struct page *page,
  379. unsigned long private,
  380. int **x)
  381. {
  382. /* This should be improoooooved!! */
  383. return alloc_page(GFP_HIGHUSER_PAGECACHE);
  384. }
  385. #define NR_OFFLINE_AT_ONCE_PAGES (256)
  386. static int
  387. do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
  388. {
  389. unsigned long pfn;
  390. struct page *page;
  391. int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
  392. int not_managed = 0;
  393. int ret = 0;
  394. LIST_HEAD(source);
  395. for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
  396. if (!pfn_valid(pfn))
  397. continue;
  398. page = pfn_to_page(pfn);
  399. if (!page_count(page))
  400. continue;
  401. /*
  402. * We can skip free pages. And we can only deal with pages on
  403. * LRU.
  404. */
  405. ret = isolate_lru_page(page, &source);
  406. if (!ret) { /* Success */
  407. move_pages--;
  408. } else {
  409. /* Becasue we don't have big zone->lock. we should
  410. check this again here. */
  411. if (page_count(page))
  412. not_managed++;
  413. #ifdef CONFIG_DEBUG_VM
  414. printk(KERN_INFO "removing from LRU failed"
  415. " %lx/%d/%lx\n",
  416. pfn, page_count(page), page->flags);
  417. #endif
  418. }
  419. }
  420. ret = -EBUSY;
  421. if (not_managed) {
  422. if (!list_empty(&source))
  423. putback_lru_pages(&source);
  424. goto out;
  425. }
  426. ret = 0;
  427. if (list_empty(&source))
  428. goto out;
  429. /* this function returns # of failed pages */
  430. ret = migrate_pages(&source, hotremove_migrate_alloc, 0);
  431. out:
  432. return ret;
  433. }
  434. /*
  435. * remove from free_area[] and mark all as Reserved.
  436. */
  437. static int
  438. offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
  439. void *data)
  440. {
  441. __offline_isolated_pages(start, start + nr_pages);
  442. return 0;
  443. }
  444. static void
  445. offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
  446. {
  447. walk_memory_resource(start_pfn, end_pfn - start_pfn, NULL,
  448. offline_isolated_pages_cb);
  449. }
  450. /*
  451. * Check all pages in range, recoreded as memory resource, are isolated.
  452. */
  453. static int
  454. check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
  455. void *data)
  456. {
  457. int ret;
  458. long offlined = *(long *)data;
  459. ret = test_pages_isolated(start_pfn, start_pfn + nr_pages);
  460. offlined = nr_pages;
  461. if (!ret)
  462. *(long *)data += offlined;
  463. return ret;
  464. }
  465. static long
  466. check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
  467. {
  468. long offlined = 0;
  469. int ret;
  470. ret = walk_memory_resource(start_pfn, end_pfn - start_pfn, &offlined,
  471. check_pages_isolated_cb);
  472. if (ret < 0)
  473. offlined = (long)ret;
  474. return offlined;
  475. }
  476. int offline_pages(unsigned long start_pfn,
  477. unsigned long end_pfn, unsigned long timeout)
  478. {
  479. unsigned long pfn, nr_pages, expire;
  480. long offlined_pages;
  481. int ret, drain, retry_max, node;
  482. struct zone *zone;
  483. struct memory_notify arg;
  484. BUG_ON(start_pfn >= end_pfn);
  485. /* at least, alignment against pageblock is necessary */
  486. if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
  487. return -EINVAL;
  488. if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
  489. return -EINVAL;
  490. /* This makes hotplug much easier...and readable.
  491. we assume this for now. .*/
  492. if (!test_pages_in_a_zone(start_pfn, end_pfn))
  493. return -EINVAL;
  494. zone = page_zone(pfn_to_page(start_pfn));
  495. node = zone_to_nid(zone);
  496. nr_pages = end_pfn - start_pfn;
  497. /* set above range as isolated */
  498. ret = start_isolate_page_range(start_pfn, end_pfn);
  499. if (ret)
  500. return ret;
  501. arg.start_pfn = start_pfn;
  502. arg.nr_pages = nr_pages;
  503. arg.status_change_nid = -1;
  504. if (nr_pages >= node_present_pages(node))
  505. arg.status_change_nid = node;
  506. ret = memory_notify(MEM_GOING_OFFLINE, &arg);
  507. ret = notifier_to_errno(ret);
  508. if (ret)
  509. goto failed_removal;
  510. pfn = start_pfn;
  511. expire = jiffies + timeout;
  512. drain = 0;
  513. retry_max = 5;
  514. repeat:
  515. /* start memory hot removal */
  516. ret = -EAGAIN;
  517. if (time_after(jiffies, expire))
  518. goto failed_removal;
  519. ret = -EINTR;
  520. if (signal_pending(current))
  521. goto failed_removal;
  522. ret = 0;
  523. if (drain) {
  524. lru_add_drain_all();
  525. flush_scheduled_work();
  526. cond_resched();
  527. drain_all_pages();
  528. }
  529. pfn = scan_lru_pages(start_pfn, end_pfn);
  530. if (pfn) { /* We have page on LRU */
  531. ret = do_migrate_range(pfn, end_pfn);
  532. if (!ret) {
  533. drain = 1;
  534. goto repeat;
  535. } else {
  536. if (ret < 0)
  537. if (--retry_max == 0)
  538. goto failed_removal;
  539. yield();
  540. drain = 1;
  541. goto repeat;
  542. }
  543. }
  544. /* drain all zone's lru pagevec, this is asyncronous... */
  545. lru_add_drain_all();
  546. flush_scheduled_work();
  547. yield();
  548. /* drain pcp pages , this is synchrouns. */
  549. drain_all_pages();
  550. /* check again */
  551. offlined_pages = check_pages_isolated(start_pfn, end_pfn);
  552. if (offlined_pages < 0) {
  553. ret = -EBUSY;
  554. goto failed_removal;
  555. }
  556. printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
  557. /* Ok, all of our target is islaoted.
  558. We cannot do rollback at this point. */
  559. offline_isolated_pages(start_pfn, end_pfn);
  560. /* reset pagetype flags and makes migrate type to be MOVABLE */
  561. undo_isolate_page_range(start_pfn, end_pfn);
  562. /* removal success */
  563. zone->present_pages -= offlined_pages;
  564. zone->zone_pgdat->node_present_pages -= offlined_pages;
  565. totalram_pages -= offlined_pages;
  566. num_physpages -= offlined_pages;
  567. vm_total_pages = nr_free_pagecache_pages();
  568. writeback_set_ratelimit();
  569. memory_notify(MEM_OFFLINE, &arg);
  570. return 0;
  571. failed_removal:
  572. printk(KERN_INFO "memory offlining %lx to %lx failed\n",
  573. start_pfn, end_pfn);
  574. memory_notify(MEM_CANCEL_OFFLINE, &arg);
  575. /* pushback to free area */
  576. undo_isolate_page_range(start_pfn, end_pfn);
  577. return ret;
  578. }
  579. #else
  580. int remove_memory(u64 start, u64 size)
  581. {
  582. return -EINVAL;
  583. }
  584. EXPORT_SYMBOL_GPL(remove_memory);
  585. #endif /* CONFIG_MEMORY_HOTREMOVE */