memory_hotplug.c 25 KB

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