memory_hotplug.c 14 KB

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