memory_hotplug.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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. sections_to_remove = nr_pages / PAGES_PER_SECTION;
  302. for (i = 0; i < sections_to_remove; i++) {
  303. unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
  304. release_mem_region(pfn << PAGE_SHIFT,
  305. PAGES_PER_SECTION << PAGE_SHIFT);
  306. ret = __remove_section(zone, __pfn_to_section(pfn));
  307. if (ret)
  308. break;
  309. }
  310. return ret;
  311. }
  312. EXPORT_SYMBOL_GPL(__remove_pages);
  313. int set_online_page_callback(online_page_callback_t callback)
  314. {
  315. int rc = -EINVAL;
  316. lock_memory_hotplug();
  317. if (online_page_callback == generic_online_page) {
  318. online_page_callback = callback;
  319. rc = 0;
  320. }
  321. unlock_memory_hotplug();
  322. return rc;
  323. }
  324. EXPORT_SYMBOL_GPL(set_online_page_callback);
  325. int restore_online_page_callback(online_page_callback_t callback)
  326. {
  327. int rc = -EINVAL;
  328. lock_memory_hotplug();
  329. if (online_page_callback == callback) {
  330. online_page_callback = generic_online_page;
  331. rc = 0;
  332. }
  333. unlock_memory_hotplug();
  334. return rc;
  335. }
  336. EXPORT_SYMBOL_GPL(restore_online_page_callback);
  337. void __online_page_set_limits(struct page *page)
  338. {
  339. unsigned long pfn = page_to_pfn(page);
  340. if (pfn >= num_physpages)
  341. num_physpages = pfn + 1;
  342. }
  343. EXPORT_SYMBOL_GPL(__online_page_set_limits);
  344. void __online_page_increment_counters(struct page *page)
  345. {
  346. totalram_pages++;
  347. #ifdef CONFIG_HIGHMEM
  348. if (PageHighMem(page))
  349. totalhigh_pages++;
  350. #endif
  351. }
  352. EXPORT_SYMBOL_GPL(__online_page_increment_counters);
  353. void __online_page_free(struct page *page)
  354. {
  355. ClearPageReserved(page);
  356. init_page_count(page);
  357. __free_page(page);
  358. }
  359. EXPORT_SYMBOL_GPL(__online_page_free);
  360. static void generic_online_page(struct page *page)
  361. {
  362. __online_page_set_limits(page);
  363. __online_page_increment_counters(page);
  364. __online_page_free(page);
  365. }
  366. static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
  367. void *arg)
  368. {
  369. unsigned long i;
  370. unsigned long onlined_pages = *(unsigned long *)arg;
  371. struct page *page;
  372. if (PageReserved(pfn_to_page(start_pfn)))
  373. for (i = 0; i < nr_pages; i++) {
  374. page = pfn_to_page(start_pfn + i);
  375. (*online_page_callback)(page);
  376. onlined_pages++;
  377. }
  378. *(unsigned long *)arg = onlined_pages;
  379. return 0;
  380. }
  381. int __ref online_pages(unsigned long pfn, unsigned long nr_pages)
  382. {
  383. unsigned long onlined_pages = 0;
  384. struct zone *zone;
  385. int need_zonelists_rebuild = 0;
  386. int nid;
  387. int ret;
  388. struct memory_notify arg;
  389. lock_memory_hotplug();
  390. arg.start_pfn = pfn;
  391. arg.nr_pages = nr_pages;
  392. arg.status_change_nid = -1;
  393. nid = page_to_nid(pfn_to_page(pfn));
  394. if (node_present_pages(nid) == 0)
  395. arg.status_change_nid = nid;
  396. ret = memory_notify(MEM_GOING_ONLINE, &arg);
  397. ret = notifier_to_errno(ret);
  398. if (ret) {
  399. memory_notify(MEM_CANCEL_ONLINE, &arg);
  400. unlock_memory_hotplug();
  401. return ret;
  402. }
  403. /*
  404. * This doesn't need a lock to do pfn_to_page().
  405. * The section can't be removed here because of the
  406. * memory_block->state_mutex.
  407. */
  408. zone = page_zone(pfn_to_page(pfn));
  409. /*
  410. * If this zone is not populated, then it is not in zonelist.
  411. * This means the page allocator ignores this zone.
  412. * So, zonelist must be updated after online.
  413. */
  414. mutex_lock(&zonelists_mutex);
  415. if (!populated_zone(zone))
  416. need_zonelists_rebuild = 1;
  417. ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
  418. online_pages_range);
  419. if (ret) {
  420. mutex_unlock(&zonelists_mutex);
  421. printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
  422. (unsigned long long) pfn << PAGE_SHIFT,
  423. (((unsigned long long) pfn + nr_pages)
  424. << PAGE_SHIFT) - 1);
  425. memory_notify(MEM_CANCEL_ONLINE, &arg);
  426. unlock_memory_hotplug();
  427. return ret;
  428. }
  429. zone->present_pages += onlined_pages;
  430. zone->zone_pgdat->node_present_pages += onlined_pages;
  431. if (onlined_pages) {
  432. node_set_state(zone_to_nid(zone), N_HIGH_MEMORY);
  433. if (need_zonelists_rebuild)
  434. build_all_zonelists(NULL, zone);
  435. else
  436. zone_pcp_update(zone);
  437. }
  438. mutex_unlock(&zonelists_mutex);
  439. init_per_zone_wmark_min();
  440. if (onlined_pages)
  441. kswapd_run(zone_to_nid(zone));
  442. vm_total_pages = nr_free_pagecache_pages();
  443. writeback_set_ratelimit();
  444. if (onlined_pages)
  445. memory_notify(MEM_ONLINE, &arg);
  446. unlock_memory_hotplug();
  447. return 0;
  448. }
  449. #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
  450. /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
  451. static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
  452. {
  453. struct pglist_data *pgdat;
  454. unsigned long zones_size[MAX_NR_ZONES] = {0};
  455. unsigned long zholes_size[MAX_NR_ZONES] = {0};
  456. unsigned long start_pfn = start >> PAGE_SHIFT;
  457. pgdat = arch_alloc_nodedata(nid);
  458. if (!pgdat)
  459. return NULL;
  460. arch_refresh_nodedata(nid, pgdat);
  461. /* we can use NODE_DATA(nid) from here */
  462. /* init node's zones as empty zones, we don't have any present pages.*/
  463. free_area_init_node(nid, zones_size, start_pfn, zholes_size);
  464. /*
  465. * The node we allocated has no zone fallback lists. For avoiding
  466. * to access not-initialized zonelist, build here.
  467. */
  468. mutex_lock(&zonelists_mutex);
  469. build_all_zonelists(pgdat, NULL);
  470. mutex_unlock(&zonelists_mutex);
  471. return pgdat;
  472. }
  473. static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
  474. {
  475. arch_refresh_nodedata(nid, NULL);
  476. arch_free_nodedata(pgdat);
  477. return;
  478. }
  479. /*
  480. * called by cpu_up() to online a node without onlined memory.
  481. */
  482. int mem_online_node(int nid)
  483. {
  484. pg_data_t *pgdat;
  485. int ret;
  486. lock_memory_hotplug();
  487. pgdat = hotadd_new_pgdat(nid, 0);
  488. if (!pgdat) {
  489. ret = -ENOMEM;
  490. goto out;
  491. }
  492. node_set_online(nid);
  493. ret = register_one_node(nid);
  494. BUG_ON(ret);
  495. out:
  496. unlock_memory_hotplug();
  497. return ret;
  498. }
  499. /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
  500. int __ref add_memory(int nid, u64 start, u64 size)
  501. {
  502. pg_data_t *pgdat = NULL;
  503. int new_pgdat = 0;
  504. struct resource *res;
  505. int ret;
  506. lock_memory_hotplug();
  507. res = register_memory_resource(start, size);
  508. ret = -EEXIST;
  509. if (!res)
  510. goto out;
  511. if (!node_online(nid)) {
  512. pgdat = hotadd_new_pgdat(nid, start);
  513. ret = -ENOMEM;
  514. if (!pgdat)
  515. goto error;
  516. new_pgdat = 1;
  517. }
  518. /* call arch's memory hotadd */
  519. ret = arch_add_memory(nid, start, size);
  520. if (ret < 0)
  521. goto error;
  522. /* we online node here. we can't roll back from here. */
  523. node_set_online(nid);
  524. if (new_pgdat) {
  525. ret = register_one_node(nid);
  526. /*
  527. * If sysfs file of new node can't create, cpu on the node
  528. * can't be hot-added. There is no rollback way now.
  529. * So, check by BUG_ON() to catch it reluctantly..
  530. */
  531. BUG_ON(ret);
  532. }
  533. /* create new memmap entry */
  534. firmware_map_add_hotplug(start, start + size, "System RAM");
  535. goto out;
  536. error:
  537. /* rollback pgdat allocation and others */
  538. if (new_pgdat)
  539. rollback_node_hotadd(nid, pgdat);
  540. if (res)
  541. release_memory_resource(res);
  542. out:
  543. unlock_memory_hotplug();
  544. return ret;
  545. }
  546. EXPORT_SYMBOL_GPL(add_memory);
  547. #ifdef CONFIG_MEMORY_HOTREMOVE
  548. /*
  549. * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
  550. * set and the size of the free page is given by page_order(). Using this,
  551. * the function determines if the pageblock contains only free pages.
  552. * Due to buddy contraints, a free page at least the size of a pageblock will
  553. * be located at the start of the pageblock
  554. */
  555. static inline int pageblock_free(struct page *page)
  556. {
  557. return PageBuddy(page) && page_order(page) >= pageblock_order;
  558. }
  559. /* Return the start of the next active pageblock after a given page */
  560. static struct page *next_active_pageblock(struct page *page)
  561. {
  562. /* Ensure the starting page is pageblock-aligned */
  563. BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
  564. /* If the entire pageblock is free, move to the end of free page */
  565. if (pageblock_free(page)) {
  566. int order;
  567. /* be careful. we don't have locks, page_order can be changed.*/
  568. order = page_order(page);
  569. if ((order < MAX_ORDER) && (order >= pageblock_order))
  570. return page + (1 << order);
  571. }
  572. return page + pageblock_nr_pages;
  573. }
  574. /* Checks if this range of memory is likely to be hot-removable. */
  575. int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
  576. {
  577. struct page *page = pfn_to_page(start_pfn);
  578. struct page *end_page = page + nr_pages;
  579. /* Check the starting page of each pageblock within the range */
  580. for (; page < end_page; page = next_active_pageblock(page)) {
  581. if (!is_pageblock_removable_nolock(page))
  582. return 0;
  583. cond_resched();
  584. }
  585. /* All pageblocks in the memory block are likely to be hot-removable */
  586. return 1;
  587. }
  588. /*
  589. * Confirm all pages in a range [start, end) is belongs to the same zone.
  590. */
  591. static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
  592. {
  593. unsigned long pfn;
  594. struct zone *zone = NULL;
  595. struct page *page;
  596. int i;
  597. for (pfn = start_pfn;
  598. pfn < end_pfn;
  599. pfn += MAX_ORDER_NR_PAGES) {
  600. i = 0;
  601. /* This is just a CONFIG_HOLES_IN_ZONE check.*/
  602. while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
  603. i++;
  604. if (i == MAX_ORDER_NR_PAGES)
  605. continue;
  606. page = pfn_to_page(pfn + i);
  607. if (zone && page_zone(page) != zone)
  608. return 0;
  609. zone = page_zone(page);
  610. }
  611. return 1;
  612. }
  613. /*
  614. * Scanning pfn is much easier than scanning lru list.
  615. * Scan pfn from start to end and Find LRU page.
  616. */
  617. static unsigned long scan_lru_pages(unsigned long start, unsigned long end)
  618. {
  619. unsigned long pfn;
  620. struct page *page;
  621. for (pfn = start; pfn < end; pfn++) {
  622. if (pfn_valid(pfn)) {
  623. page = pfn_to_page(pfn);
  624. if (PageLRU(page))
  625. return pfn;
  626. }
  627. }
  628. return 0;
  629. }
  630. static struct page *
  631. hotremove_migrate_alloc(struct page *page, unsigned long private, int **x)
  632. {
  633. /* This should be improooooved!! */
  634. return alloc_page(GFP_HIGHUSER_MOVABLE);
  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. /* this function returns # of failed pages */
  685. ret = migrate_pages(&source, hotremove_migrate_alloc, 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);
  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, MIGRATE_MOVABLE);
  759. if (ret)
  760. goto out;
  761. arg.start_pfn = start_pfn;
  762. arg.nr_pages = nr_pages;
  763. arg.status_change_nid = -1;
  764. if (nr_pages >= node_present_pages(node))
  765. arg.status_change_nid = node;
  766. ret = memory_notify(MEM_GOING_OFFLINE, &arg);
  767. ret = notifier_to_errno(ret);
  768. if (ret)
  769. goto failed_removal;
  770. pfn = start_pfn;
  771. expire = jiffies + timeout;
  772. drain = 0;
  773. retry_max = 5;
  774. repeat:
  775. /* start memory hot removal */
  776. ret = -EAGAIN;
  777. if (time_after(jiffies, expire))
  778. goto failed_removal;
  779. ret = -EINTR;
  780. if (signal_pending(current))
  781. goto failed_removal;
  782. ret = 0;
  783. if (drain) {
  784. lru_add_drain_all();
  785. cond_resched();
  786. drain_all_pages();
  787. }
  788. pfn = scan_lru_pages(start_pfn, end_pfn);
  789. if (pfn) { /* We have page on LRU */
  790. ret = do_migrate_range(pfn, end_pfn);
  791. if (!ret) {
  792. drain = 1;
  793. goto repeat;
  794. } else {
  795. if (ret < 0)
  796. if (--retry_max == 0)
  797. goto failed_removal;
  798. yield();
  799. drain = 1;
  800. goto repeat;
  801. }
  802. }
  803. /* drain all zone's lru pagevec, this is asyncronous... */
  804. lru_add_drain_all();
  805. yield();
  806. /* drain pcp pages , this is synchrouns. */
  807. drain_all_pages();
  808. /* check again */
  809. offlined_pages = check_pages_isolated(start_pfn, end_pfn);
  810. if (offlined_pages < 0) {
  811. ret = -EBUSY;
  812. goto failed_removal;
  813. }
  814. printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
  815. /* Ok, all of our target is islaoted.
  816. We cannot do rollback at this point. */
  817. offline_isolated_pages(start_pfn, end_pfn);
  818. /* reset pagetype flags and makes migrate type to be MOVABLE */
  819. undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
  820. /* removal success */
  821. zone->present_pages -= offlined_pages;
  822. zone->zone_pgdat->node_present_pages -= offlined_pages;
  823. totalram_pages -= offlined_pages;
  824. init_per_zone_wmark_min();
  825. if (!populated_zone(zone))
  826. zone_pcp_reset(zone);
  827. if (!node_present_pages(node)) {
  828. node_clear_state(node, N_HIGH_MEMORY);
  829. kswapd_stop(node);
  830. }
  831. vm_total_pages = nr_free_pagecache_pages();
  832. writeback_set_ratelimit();
  833. memory_notify(MEM_OFFLINE, &arg);
  834. unlock_memory_hotplug();
  835. return 0;
  836. failed_removal:
  837. printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
  838. (unsigned long long) start_pfn << PAGE_SHIFT,
  839. ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
  840. memory_notify(MEM_CANCEL_OFFLINE, &arg);
  841. /* pushback to free area */
  842. undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
  843. out:
  844. unlock_memory_hotplug();
  845. return ret;
  846. }
  847. int remove_memory(u64 start, u64 size)
  848. {
  849. unsigned long start_pfn, end_pfn;
  850. start_pfn = PFN_DOWN(start);
  851. end_pfn = start_pfn + PFN_DOWN(size);
  852. return offline_pages(start_pfn, end_pfn, 120 * HZ);
  853. }
  854. #else
  855. int remove_memory(u64 start, u64 size)
  856. {
  857. return -EINVAL;
  858. }
  859. #endif /* CONFIG_MEMORY_HOTREMOVE */
  860. EXPORT_SYMBOL_GPL(remove_memory);