memory_hotplug.c 25 KB

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