memory_hotplug.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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;
  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. /*
  89. * Reasonably generic function for adding memory. It is
  90. * expected that archs that support memory hotplug will
  91. * call this function after deciding the zone to which to
  92. * add the new pages.
  93. */
  94. int __add_pages(struct zone *zone, unsigned long phys_start_pfn,
  95. unsigned long nr_pages)
  96. {
  97. unsigned long i;
  98. int err = 0;
  99. int start_sec, end_sec;
  100. /* during initialize mem_map, align hot-added range to section */
  101. start_sec = pfn_to_section_nr(phys_start_pfn);
  102. end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
  103. for (i = start_sec; i <= end_sec; i++) {
  104. err = __add_section(zone, i << PFN_SECTION_SHIFT);
  105. /*
  106. * EEXIST is finally dealed with by ioresource collision
  107. * check. see add_memory() => register_memory_resource()
  108. * Warning will be printed if there is collision.
  109. */
  110. if (err && (err != -EEXIST))
  111. break;
  112. err = 0;
  113. }
  114. return err;
  115. }
  116. EXPORT_SYMBOL_GPL(__add_pages);
  117. static void grow_zone_span(struct zone *zone,
  118. unsigned long start_pfn, unsigned long end_pfn)
  119. {
  120. unsigned long old_zone_end_pfn;
  121. zone_span_writelock(zone);
  122. old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
  123. if (start_pfn < zone->zone_start_pfn)
  124. zone->zone_start_pfn = start_pfn;
  125. zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
  126. zone->zone_start_pfn;
  127. zone_span_writeunlock(zone);
  128. }
  129. static void grow_pgdat_span(struct pglist_data *pgdat,
  130. unsigned long start_pfn, unsigned long end_pfn)
  131. {
  132. unsigned long old_pgdat_end_pfn =
  133. pgdat->node_start_pfn + pgdat->node_spanned_pages;
  134. if (start_pfn < pgdat->node_start_pfn)
  135. pgdat->node_start_pfn = start_pfn;
  136. pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
  137. pgdat->node_start_pfn;
  138. }
  139. static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
  140. void *arg)
  141. {
  142. unsigned long i;
  143. unsigned long onlined_pages = *(unsigned long *)arg;
  144. struct page *page;
  145. if (PageReserved(pfn_to_page(start_pfn)))
  146. for (i = 0; i < nr_pages; i++) {
  147. page = pfn_to_page(start_pfn + i);
  148. online_page(page);
  149. onlined_pages++;
  150. }
  151. *(unsigned long *)arg = onlined_pages;
  152. return 0;
  153. }
  154. int online_pages(unsigned long pfn, unsigned long nr_pages)
  155. {
  156. unsigned long flags;
  157. unsigned long onlined_pages = 0;
  158. struct zone *zone;
  159. int need_zonelists_rebuild = 0;
  160. /*
  161. * This doesn't need a lock to do pfn_to_page().
  162. * The section can't be removed here because of the
  163. * memory_block->state_sem.
  164. */
  165. zone = page_zone(pfn_to_page(pfn));
  166. pgdat_resize_lock(zone->zone_pgdat, &flags);
  167. grow_zone_span(zone, pfn, pfn + nr_pages);
  168. grow_pgdat_span(zone->zone_pgdat, pfn, pfn + nr_pages);
  169. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  170. /*
  171. * If this zone is not populated, then it is not in zonelist.
  172. * This means the page allocator ignores this zone.
  173. * So, zonelist must be updated after online.
  174. */
  175. if (!populated_zone(zone))
  176. need_zonelists_rebuild = 1;
  177. walk_memory_resource(pfn, nr_pages, &onlined_pages,
  178. online_pages_range);
  179. zone->present_pages += onlined_pages;
  180. zone->zone_pgdat->node_present_pages += onlined_pages;
  181. setup_per_zone_pages_min();
  182. if (onlined_pages) {
  183. kswapd_run(zone_to_nid(zone));
  184. node_set_state(zone_to_nid(zone), N_HIGH_MEMORY);
  185. }
  186. if (need_zonelists_rebuild)
  187. build_all_zonelists();
  188. vm_total_pages = nr_free_pagecache_pages();
  189. writeback_set_ratelimit();
  190. return 0;
  191. }
  192. #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
  193. static pg_data_t *hotadd_new_pgdat(int nid, u64 start)
  194. {
  195. struct pglist_data *pgdat;
  196. unsigned long zones_size[MAX_NR_ZONES] = {0};
  197. unsigned long zholes_size[MAX_NR_ZONES] = {0};
  198. unsigned long start_pfn = start >> PAGE_SHIFT;
  199. pgdat = arch_alloc_nodedata(nid);
  200. if (!pgdat)
  201. return NULL;
  202. arch_refresh_nodedata(nid, pgdat);
  203. /* we can use NODE_DATA(nid) from here */
  204. /* init node's zones as empty zones, we don't have any present pages.*/
  205. free_area_init_node(nid, pgdat, zones_size, start_pfn, zholes_size);
  206. return pgdat;
  207. }
  208. static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
  209. {
  210. arch_refresh_nodedata(nid, NULL);
  211. arch_free_nodedata(pgdat);
  212. return;
  213. }
  214. int add_memory(int nid, u64 start, u64 size)
  215. {
  216. pg_data_t *pgdat = NULL;
  217. int new_pgdat = 0;
  218. struct resource *res;
  219. int ret;
  220. res = register_memory_resource(start, size);
  221. if (!res)
  222. return -EEXIST;
  223. if (!node_online(nid)) {
  224. pgdat = hotadd_new_pgdat(nid, start);
  225. if (!pgdat)
  226. return -ENOMEM;
  227. new_pgdat = 1;
  228. }
  229. /* call arch's memory hotadd */
  230. ret = arch_add_memory(nid, start, size);
  231. if (ret < 0)
  232. goto error;
  233. /* we online node here. we can't roll back from here. */
  234. node_set_online(nid);
  235. cpuset_track_online_nodes();
  236. if (new_pgdat) {
  237. ret = register_one_node(nid);
  238. /*
  239. * If sysfs file of new node can't create, cpu on the node
  240. * can't be hot-added. There is no rollback way now.
  241. * So, check by BUG_ON() to catch it reluctantly..
  242. */
  243. BUG_ON(ret);
  244. }
  245. return ret;
  246. error:
  247. /* rollback pgdat allocation and others */
  248. if (new_pgdat)
  249. rollback_node_hotadd(nid, pgdat);
  250. if (res)
  251. release_memory_resource(res);
  252. return ret;
  253. }
  254. EXPORT_SYMBOL_GPL(add_memory);
  255. #ifdef CONFIG_MEMORY_HOTREMOVE
  256. /*
  257. * Confirm all pages in a range [start, end) is belongs to the same zone.
  258. */
  259. static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
  260. {
  261. unsigned long pfn;
  262. struct zone *zone = NULL;
  263. struct page *page;
  264. int i;
  265. for (pfn = start_pfn;
  266. pfn < end_pfn;
  267. pfn += MAX_ORDER_NR_PAGES) {
  268. i = 0;
  269. /* This is just a CONFIG_HOLES_IN_ZONE check.*/
  270. while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
  271. i++;
  272. if (i == MAX_ORDER_NR_PAGES)
  273. continue;
  274. page = pfn_to_page(pfn + i);
  275. if (zone && page_zone(page) != zone)
  276. return 0;
  277. zone = page_zone(page);
  278. }
  279. return 1;
  280. }
  281. /*
  282. * Scanning pfn is much easier than scanning lru list.
  283. * Scan pfn from start to end and Find LRU page.
  284. */
  285. int scan_lru_pages(unsigned long start, unsigned long end)
  286. {
  287. unsigned long pfn;
  288. struct page *page;
  289. for (pfn = start; pfn < end; pfn++) {
  290. if (pfn_valid(pfn)) {
  291. page = pfn_to_page(pfn);
  292. if (PageLRU(page))
  293. return pfn;
  294. }
  295. }
  296. return 0;
  297. }
  298. static struct page *
  299. hotremove_migrate_alloc(struct page *page,
  300. unsigned long private,
  301. int **x)
  302. {
  303. /* This should be improoooooved!! */
  304. return alloc_page(GFP_HIGHUSER_PAGECACHE);
  305. }
  306. #define NR_OFFLINE_AT_ONCE_PAGES (256)
  307. static int
  308. do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
  309. {
  310. unsigned long pfn;
  311. struct page *page;
  312. int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
  313. int not_managed = 0;
  314. int ret = 0;
  315. LIST_HEAD(source);
  316. for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
  317. if (!pfn_valid(pfn))
  318. continue;
  319. page = pfn_to_page(pfn);
  320. if (!page_count(page))
  321. continue;
  322. /*
  323. * We can skip free pages. And we can only deal with pages on
  324. * LRU.
  325. */
  326. ret = isolate_lru_page(page, &source);
  327. if (!ret) { /* Success */
  328. move_pages--;
  329. } else {
  330. /* Becasue we don't have big zone->lock. we should
  331. check this again here. */
  332. if (page_count(page))
  333. not_managed++;
  334. #ifdef CONFIG_DEBUG_VM
  335. printk(KERN_INFO "removing from LRU failed"
  336. " %lx/%d/%lx\n",
  337. pfn, page_count(page), page->flags);
  338. #endif
  339. }
  340. }
  341. ret = -EBUSY;
  342. if (not_managed) {
  343. if (!list_empty(&source))
  344. putback_lru_pages(&source);
  345. goto out;
  346. }
  347. ret = 0;
  348. if (list_empty(&source))
  349. goto out;
  350. /* this function returns # of failed pages */
  351. ret = migrate_pages(&source, hotremove_migrate_alloc, 0);
  352. out:
  353. return ret;
  354. }
  355. /*
  356. * remove from free_area[] and mark all as Reserved.
  357. */
  358. static int
  359. offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
  360. void *data)
  361. {
  362. __offline_isolated_pages(start, start + nr_pages);
  363. return 0;
  364. }
  365. static void
  366. offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
  367. {
  368. walk_memory_resource(start_pfn, end_pfn - start_pfn, NULL,
  369. offline_isolated_pages_cb);
  370. }
  371. /*
  372. * Check all pages in range, recoreded as memory resource, are isolated.
  373. */
  374. static int
  375. check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
  376. void *data)
  377. {
  378. int ret;
  379. long offlined = *(long *)data;
  380. ret = test_pages_isolated(start_pfn, start_pfn + nr_pages);
  381. offlined = nr_pages;
  382. if (!ret)
  383. *(long *)data += offlined;
  384. return ret;
  385. }
  386. static long
  387. check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
  388. {
  389. long offlined = 0;
  390. int ret;
  391. ret = walk_memory_resource(start_pfn, end_pfn - start_pfn, &offlined,
  392. check_pages_isolated_cb);
  393. if (ret < 0)
  394. offlined = (long)ret;
  395. return offlined;
  396. }
  397. extern void drain_all_local_pages(void);
  398. int offline_pages(unsigned long start_pfn,
  399. unsigned long end_pfn, unsigned long timeout)
  400. {
  401. unsigned long pfn, nr_pages, expire;
  402. long offlined_pages;
  403. int ret, drain, retry_max;
  404. struct zone *zone;
  405. BUG_ON(start_pfn >= end_pfn);
  406. /* at least, alignment against pageblock is necessary */
  407. if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
  408. return -EINVAL;
  409. if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
  410. return -EINVAL;
  411. /* This makes hotplug much easier...and readable.
  412. we assume this for now. .*/
  413. if (!test_pages_in_a_zone(start_pfn, end_pfn))
  414. return -EINVAL;
  415. /* set above range as isolated */
  416. ret = start_isolate_page_range(start_pfn, end_pfn);
  417. if (ret)
  418. return ret;
  419. nr_pages = end_pfn - start_pfn;
  420. pfn = start_pfn;
  421. expire = jiffies + timeout;
  422. drain = 0;
  423. retry_max = 5;
  424. repeat:
  425. /* start memory hot removal */
  426. ret = -EAGAIN;
  427. if (time_after(jiffies, expire))
  428. goto failed_removal;
  429. ret = -EINTR;
  430. if (signal_pending(current))
  431. goto failed_removal;
  432. ret = 0;
  433. if (drain) {
  434. lru_add_drain_all();
  435. flush_scheduled_work();
  436. cond_resched();
  437. drain_all_local_pages();
  438. }
  439. pfn = scan_lru_pages(start_pfn, end_pfn);
  440. if (pfn) { /* We have page on LRU */
  441. ret = do_migrate_range(pfn, end_pfn);
  442. if (!ret) {
  443. drain = 1;
  444. goto repeat;
  445. } else {
  446. if (ret < 0)
  447. if (--retry_max == 0)
  448. goto failed_removal;
  449. yield();
  450. drain = 1;
  451. goto repeat;
  452. }
  453. }
  454. /* drain all zone's lru pagevec, this is asyncronous... */
  455. lru_add_drain_all();
  456. flush_scheduled_work();
  457. yield();
  458. /* drain pcp pages , this is synchrouns. */
  459. drain_all_local_pages();
  460. /* check again */
  461. offlined_pages = check_pages_isolated(start_pfn, end_pfn);
  462. if (offlined_pages < 0) {
  463. ret = -EBUSY;
  464. goto failed_removal;
  465. }
  466. printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
  467. /* Ok, all of our target is islaoted.
  468. We cannot do rollback at this point. */
  469. offline_isolated_pages(start_pfn, end_pfn);
  470. /* reset pagetype flags */
  471. start_isolate_page_range(start_pfn, end_pfn);
  472. /* removal success */
  473. zone = page_zone(pfn_to_page(start_pfn));
  474. zone->present_pages -= offlined_pages;
  475. zone->zone_pgdat->node_present_pages -= offlined_pages;
  476. totalram_pages -= offlined_pages;
  477. num_physpages -= offlined_pages;
  478. vm_total_pages = nr_free_pagecache_pages();
  479. writeback_set_ratelimit();
  480. return 0;
  481. failed_removal:
  482. printk(KERN_INFO "memory offlining %lx to %lx failed\n",
  483. start_pfn, end_pfn);
  484. /* pushback to free area */
  485. undo_isolate_page_range(start_pfn, end_pfn);
  486. return ret;
  487. }
  488. #else
  489. int remove_memory(u64 start, u64 size)
  490. {
  491. return -EINVAL;
  492. }
  493. EXPORT_SYMBOL_GPL(remove_memory);
  494. #endif /* CONFIG_MEMORY_HOTREMOVE */