memory_hotplug.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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. release_mem_region(phys_start_pfn << PAGE_SHIFT, nr_pages * PAGE_SIZE);
  308. sections_to_remove = nr_pages / PAGES_PER_SECTION;
  309. for (i = 0; i < sections_to_remove; i++) {
  310. unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
  311. ret = __remove_section(zone, __pfn_to_section(pfn));
  312. if (ret)
  313. break;
  314. }
  315. return ret;
  316. }
  317. EXPORT_SYMBOL_GPL(__remove_pages);
  318. int set_online_page_callback(online_page_callback_t callback)
  319. {
  320. int rc = -EINVAL;
  321. lock_memory_hotplug();
  322. if (online_page_callback == generic_online_page) {
  323. online_page_callback = callback;
  324. rc = 0;
  325. }
  326. unlock_memory_hotplug();
  327. return rc;
  328. }
  329. EXPORT_SYMBOL_GPL(set_online_page_callback);
  330. int restore_online_page_callback(online_page_callback_t callback)
  331. {
  332. int rc = -EINVAL;
  333. lock_memory_hotplug();
  334. if (online_page_callback == callback) {
  335. online_page_callback = generic_online_page;
  336. rc = 0;
  337. }
  338. unlock_memory_hotplug();
  339. return rc;
  340. }
  341. EXPORT_SYMBOL_GPL(restore_online_page_callback);
  342. void __online_page_set_limits(struct page *page)
  343. {
  344. unsigned long pfn = page_to_pfn(page);
  345. if (pfn >= num_physpages)
  346. num_physpages = pfn + 1;
  347. }
  348. EXPORT_SYMBOL_GPL(__online_page_set_limits);
  349. void __online_page_increment_counters(struct page *page)
  350. {
  351. totalram_pages++;
  352. #ifdef CONFIG_HIGHMEM
  353. if (PageHighMem(page))
  354. totalhigh_pages++;
  355. #endif
  356. }
  357. EXPORT_SYMBOL_GPL(__online_page_increment_counters);
  358. void __online_page_free(struct page *page)
  359. {
  360. ClearPageReserved(page);
  361. init_page_count(page);
  362. __free_page(page);
  363. }
  364. EXPORT_SYMBOL_GPL(__online_page_free);
  365. static void generic_online_page(struct page *page)
  366. {
  367. __online_page_set_limits(page);
  368. __online_page_increment_counters(page);
  369. __online_page_free(page);
  370. }
  371. static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
  372. void *arg)
  373. {
  374. unsigned long i;
  375. unsigned long onlined_pages = *(unsigned long *)arg;
  376. struct page *page;
  377. if (PageReserved(pfn_to_page(start_pfn)))
  378. for (i = 0; i < nr_pages; i++) {
  379. page = pfn_to_page(start_pfn + i);
  380. (*online_page_callback)(page);
  381. onlined_pages++;
  382. }
  383. *(unsigned long *)arg = onlined_pages;
  384. return 0;
  385. }
  386. int __ref online_pages(unsigned long pfn, unsigned long nr_pages)
  387. {
  388. unsigned long onlined_pages = 0;
  389. struct zone *zone;
  390. int need_zonelists_rebuild = 0;
  391. int nid;
  392. int ret;
  393. struct memory_notify arg;
  394. lock_memory_hotplug();
  395. arg.start_pfn = pfn;
  396. arg.nr_pages = nr_pages;
  397. arg.status_change_nid = -1;
  398. nid = page_to_nid(pfn_to_page(pfn));
  399. if (node_present_pages(nid) == 0)
  400. arg.status_change_nid = nid;
  401. ret = memory_notify(MEM_GOING_ONLINE, &arg);
  402. ret = notifier_to_errno(ret);
  403. if (ret) {
  404. memory_notify(MEM_CANCEL_ONLINE, &arg);
  405. unlock_memory_hotplug();
  406. return ret;
  407. }
  408. /*
  409. * This doesn't need a lock to do pfn_to_page().
  410. * The section can't be removed here because of the
  411. * memory_block->state_mutex.
  412. */
  413. zone = page_zone(pfn_to_page(pfn));
  414. /*
  415. * If this zone is not populated, then it is not in zonelist.
  416. * This means the page allocator ignores this zone.
  417. * So, zonelist must be updated after online.
  418. */
  419. mutex_lock(&zonelists_mutex);
  420. if (!populated_zone(zone))
  421. need_zonelists_rebuild = 1;
  422. ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
  423. online_pages_range);
  424. if (ret) {
  425. mutex_unlock(&zonelists_mutex);
  426. printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
  427. (unsigned long long) pfn << PAGE_SHIFT,
  428. (((unsigned long long) pfn + nr_pages)
  429. << PAGE_SHIFT) - 1);
  430. memory_notify(MEM_CANCEL_ONLINE, &arg);
  431. unlock_memory_hotplug();
  432. return ret;
  433. }
  434. zone->present_pages += onlined_pages;
  435. zone->zone_pgdat->node_present_pages += onlined_pages;
  436. if (onlined_pages) {
  437. node_set_state(zone_to_nid(zone), N_HIGH_MEMORY);
  438. if (need_zonelists_rebuild)
  439. build_all_zonelists(NULL, zone);
  440. else
  441. zone_pcp_update(zone);
  442. }
  443. mutex_unlock(&zonelists_mutex);
  444. init_per_zone_wmark_min();
  445. if (onlined_pages)
  446. kswapd_run(zone_to_nid(zone));
  447. vm_total_pages = nr_free_pagecache_pages();
  448. writeback_set_ratelimit();
  449. if (onlined_pages)
  450. memory_notify(MEM_ONLINE, &arg);
  451. unlock_memory_hotplug();
  452. return 0;
  453. }
  454. #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
  455. /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
  456. static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
  457. {
  458. struct pglist_data *pgdat;
  459. unsigned long zones_size[MAX_NR_ZONES] = {0};
  460. unsigned long zholes_size[MAX_NR_ZONES] = {0};
  461. unsigned long start_pfn = start >> PAGE_SHIFT;
  462. pgdat = arch_alloc_nodedata(nid);
  463. if (!pgdat)
  464. return NULL;
  465. arch_refresh_nodedata(nid, pgdat);
  466. /* we can use NODE_DATA(nid) from here */
  467. /* init node's zones as empty zones, we don't have any present pages.*/
  468. free_area_init_node(nid, zones_size, start_pfn, zholes_size);
  469. /*
  470. * The node we allocated has no zone fallback lists. For avoiding
  471. * to access not-initialized zonelist, build here.
  472. */
  473. mutex_lock(&zonelists_mutex);
  474. build_all_zonelists(pgdat, NULL);
  475. mutex_unlock(&zonelists_mutex);
  476. return pgdat;
  477. }
  478. static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
  479. {
  480. arch_refresh_nodedata(nid, NULL);
  481. arch_free_nodedata(pgdat);
  482. return;
  483. }
  484. /*
  485. * called by cpu_up() to online a node without onlined memory.
  486. */
  487. int mem_online_node(int nid)
  488. {
  489. pg_data_t *pgdat;
  490. int ret;
  491. lock_memory_hotplug();
  492. pgdat = hotadd_new_pgdat(nid, 0);
  493. if (!pgdat) {
  494. ret = -ENOMEM;
  495. goto out;
  496. }
  497. node_set_online(nid);
  498. ret = register_one_node(nid);
  499. BUG_ON(ret);
  500. out:
  501. unlock_memory_hotplug();
  502. return ret;
  503. }
  504. /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
  505. int __ref add_memory(int nid, u64 start, u64 size)
  506. {
  507. pg_data_t *pgdat = NULL;
  508. int new_pgdat = 0;
  509. struct resource *res;
  510. int ret;
  511. lock_memory_hotplug();
  512. res = register_memory_resource(start, size);
  513. ret = -EEXIST;
  514. if (!res)
  515. goto out;
  516. if (!node_online(nid)) {
  517. pgdat = hotadd_new_pgdat(nid, start);
  518. ret = -ENOMEM;
  519. if (!pgdat)
  520. goto error;
  521. new_pgdat = 1;
  522. }
  523. /* call arch's memory hotadd */
  524. ret = arch_add_memory(nid, start, size);
  525. if (ret < 0)
  526. goto error;
  527. /* we online node here. we can't roll back from here. */
  528. node_set_online(nid);
  529. if (new_pgdat) {
  530. ret = register_one_node(nid);
  531. /*
  532. * If sysfs file of new node can't create, cpu on the node
  533. * can't be hot-added. There is no rollback way now.
  534. * So, check by BUG_ON() to catch it reluctantly..
  535. */
  536. BUG_ON(ret);
  537. }
  538. /* create new memmap entry */
  539. firmware_map_add_hotplug(start, start + size, "System RAM");
  540. goto out;
  541. error:
  542. /* rollback pgdat allocation and others */
  543. if (new_pgdat)
  544. rollback_node_hotadd(nid, pgdat);
  545. if (res)
  546. release_memory_resource(res);
  547. out:
  548. unlock_memory_hotplug();
  549. return ret;
  550. }
  551. EXPORT_SYMBOL_GPL(add_memory);
  552. #ifdef CONFIG_MEMORY_HOTREMOVE
  553. /*
  554. * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
  555. * set and the size of the free page is given by page_order(). Using this,
  556. * the function determines if the pageblock contains only free pages.
  557. * Due to buddy contraints, a free page at least the size of a pageblock will
  558. * be located at the start of the pageblock
  559. */
  560. static inline int pageblock_free(struct page *page)
  561. {
  562. return PageBuddy(page) && page_order(page) >= pageblock_order;
  563. }
  564. /* Return the start of the next active pageblock after a given page */
  565. static struct page *next_active_pageblock(struct page *page)
  566. {
  567. /* Ensure the starting page is pageblock-aligned */
  568. BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
  569. /* If the entire pageblock is free, move to the end of free page */
  570. if (pageblock_free(page)) {
  571. int order;
  572. /* be careful. we don't have locks, page_order can be changed.*/
  573. order = page_order(page);
  574. if ((order < MAX_ORDER) && (order >= pageblock_order))
  575. return page + (1 << order);
  576. }
  577. return page + pageblock_nr_pages;
  578. }
  579. /* Checks if this range of memory is likely to be hot-removable. */
  580. int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
  581. {
  582. struct page *page = pfn_to_page(start_pfn);
  583. struct page *end_page = page + nr_pages;
  584. /* Check the starting page of each pageblock within the range */
  585. for (; page < end_page; page = next_active_pageblock(page)) {
  586. if (!is_pageblock_removable_nolock(page))
  587. return 0;
  588. cond_resched();
  589. }
  590. /* All pageblocks in the memory block are likely to be hot-removable */
  591. return 1;
  592. }
  593. /*
  594. * Confirm all pages in a range [start, end) is belongs to the same zone.
  595. */
  596. static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
  597. {
  598. unsigned long pfn;
  599. struct zone *zone = NULL;
  600. struct page *page;
  601. int i;
  602. for (pfn = start_pfn;
  603. pfn < end_pfn;
  604. pfn += MAX_ORDER_NR_PAGES) {
  605. i = 0;
  606. /* This is just a CONFIG_HOLES_IN_ZONE check.*/
  607. while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
  608. i++;
  609. if (i == MAX_ORDER_NR_PAGES)
  610. continue;
  611. page = pfn_to_page(pfn + i);
  612. if (zone && page_zone(page) != zone)
  613. return 0;
  614. zone = page_zone(page);
  615. }
  616. return 1;
  617. }
  618. /*
  619. * Scanning pfn is much easier than scanning lru list.
  620. * Scan pfn from start to end and Find LRU page.
  621. */
  622. static unsigned long scan_lru_pages(unsigned long start, unsigned long end)
  623. {
  624. unsigned long pfn;
  625. struct page *page;
  626. for (pfn = start; pfn < end; pfn++) {
  627. if (pfn_valid(pfn)) {
  628. page = pfn_to_page(pfn);
  629. if (PageLRU(page))
  630. return pfn;
  631. }
  632. }
  633. return 0;
  634. }
  635. #define NR_OFFLINE_AT_ONCE_PAGES (256)
  636. static int
  637. do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
  638. {
  639. unsigned long pfn;
  640. struct page *page;
  641. int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
  642. int not_managed = 0;
  643. int ret = 0;
  644. LIST_HEAD(source);
  645. for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
  646. if (!pfn_valid(pfn))
  647. continue;
  648. page = pfn_to_page(pfn);
  649. if (!get_page_unless_zero(page))
  650. continue;
  651. /*
  652. * We can skip free pages. And we can only deal with pages on
  653. * LRU.
  654. */
  655. ret = isolate_lru_page(page);
  656. if (!ret) { /* Success */
  657. put_page(page);
  658. list_add_tail(&page->lru, &source);
  659. move_pages--;
  660. inc_zone_page_state(page, NR_ISOLATED_ANON +
  661. page_is_file_cache(page));
  662. } else {
  663. #ifdef CONFIG_DEBUG_VM
  664. printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
  665. pfn);
  666. dump_page(page);
  667. #endif
  668. put_page(page);
  669. /* Because we don't have big zone->lock. we should
  670. check this again here. */
  671. if (page_count(page)) {
  672. not_managed++;
  673. ret = -EBUSY;
  674. break;
  675. }
  676. }
  677. }
  678. if (!list_empty(&source)) {
  679. if (not_managed) {
  680. putback_lru_pages(&source);
  681. goto out;
  682. }
  683. /*
  684. * alloc_migrate_target should be improooooved!!
  685. * migrate_pages returns # of failed pages.
  686. */
  687. ret = migrate_pages(&source, alloc_migrate_target, 0,
  688. true, MIGRATE_SYNC);
  689. if (ret)
  690. putback_lru_pages(&source);
  691. }
  692. out:
  693. return ret;
  694. }
  695. /*
  696. * remove from free_area[] and mark all as Reserved.
  697. */
  698. static int
  699. offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
  700. void *data)
  701. {
  702. __offline_isolated_pages(start, start + nr_pages);
  703. return 0;
  704. }
  705. static void
  706. offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
  707. {
  708. walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
  709. offline_isolated_pages_cb);
  710. }
  711. /*
  712. * Check all pages in range, recoreded as memory resource, are isolated.
  713. */
  714. static int
  715. check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
  716. void *data)
  717. {
  718. int ret;
  719. long offlined = *(long *)data;
  720. ret = test_pages_isolated(start_pfn, start_pfn + nr_pages);
  721. offlined = nr_pages;
  722. if (!ret)
  723. *(long *)data += offlined;
  724. return ret;
  725. }
  726. static long
  727. check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
  728. {
  729. long offlined = 0;
  730. int ret;
  731. ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
  732. check_pages_isolated_cb);
  733. if (ret < 0)
  734. offlined = (long)ret;
  735. return offlined;
  736. }
  737. static int __ref __offline_pages(unsigned long start_pfn,
  738. unsigned long end_pfn, unsigned long timeout)
  739. {
  740. unsigned long pfn, nr_pages, expire;
  741. long offlined_pages;
  742. int ret, drain, retry_max, node;
  743. struct zone *zone;
  744. struct memory_notify arg;
  745. BUG_ON(start_pfn >= end_pfn);
  746. /* at least, alignment against pageblock is necessary */
  747. if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
  748. return -EINVAL;
  749. if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
  750. return -EINVAL;
  751. /* This makes hotplug much easier...and readable.
  752. we assume this for now. .*/
  753. if (!test_pages_in_a_zone(start_pfn, end_pfn))
  754. return -EINVAL;
  755. lock_memory_hotplug();
  756. zone = page_zone(pfn_to_page(start_pfn));
  757. node = zone_to_nid(zone);
  758. nr_pages = end_pfn - start_pfn;
  759. /* set above range as isolated */
  760. ret = start_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
  761. if (ret)
  762. goto out;
  763. arg.start_pfn = start_pfn;
  764. arg.nr_pages = nr_pages;
  765. arg.status_change_nid = -1;
  766. if (nr_pages >= node_present_pages(node))
  767. arg.status_change_nid = node;
  768. ret = memory_notify(MEM_GOING_OFFLINE, &arg);
  769. ret = notifier_to_errno(ret);
  770. if (ret)
  771. goto failed_removal;
  772. pfn = start_pfn;
  773. expire = jiffies + timeout;
  774. drain = 0;
  775. retry_max = 5;
  776. repeat:
  777. /* start memory hot removal */
  778. ret = -EAGAIN;
  779. if (time_after(jiffies, expire))
  780. goto failed_removal;
  781. ret = -EINTR;
  782. if (signal_pending(current))
  783. goto failed_removal;
  784. ret = 0;
  785. if (drain) {
  786. lru_add_drain_all();
  787. cond_resched();
  788. drain_all_pages();
  789. }
  790. pfn = scan_lru_pages(start_pfn, end_pfn);
  791. if (pfn) { /* We have page on LRU */
  792. ret = do_migrate_range(pfn, end_pfn);
  793. if (!ret) {
  794. drain = 1;
  795. goto repeat;
  796. } else {
  797. if (ret < 0)
  798. if (--retry_max == 0)
  799. goto failed_removal;
  800. yield();
  801. drain = 1;
  802. goto repeat;
  803. }
  804. }
  805. /* drain all zone's lru pagevec, this is asyncronous... */
  806. lru_add_drain_all();
  807. yield();
  808. /* drain pcp pages , this is synchrouns. */
  809. drain_all_pages();
  810. /* check again */
  811. offlined_pages = check_pages_isolated(start_pfn, end_pfn);
  812. if (offlined_pages < 0) {
  813. ret = -EBUSY;
  814. goto failed_removal;
  815. }
  816. printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
  817. /* Ok, all of our target is islaoted.
  818. We cannot do rollback at this point. */
  819. offline_isolated_pages(start_pfn, end_pfn);
  820. /* reset pagetype flags and makes migrate type to be MOVABLE */
  821. undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
  822. /* removal success */
  823. zone->present_pages -= offlined_pages;
  824. zone->zone_pgdat->node_present_pages -= offlined_pages;
  825. totalram_pages -= offlined_pages;
  826. init_per_zone_wmark_min();
  827. if (!populated_zone(zone)) {
  828. zone_pcp_reset(zone);
  829. mutex_lock(&zonelists_mutex);
  830. build_all_zonelists(NULL, NULL);
  831. mutex_unlock(&zonelists_mutex);
  832. } else
  833. zone_pcp_update(zone);
  834. if (!node_present_pages(node)) {
  835. node_clear_state(node, N_HIGH_MEMORY);
  836. kswapd_stop(node);
  837. }
  838. vm_total_pages = nr_free_pagecache_pages();
  839. writeback_set_ratelimit();
  840. memory_notify(MEM_OFFLINE, &arg);
  841. unlock_memory_hotplug();
  842. return 0;
  843. failed_removal:
  844. printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
  845. (unsigned long long) start_pfn << PAGE_SHIFT,
  846. ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
  847. memory_notify(MEM_CANCEL_OFFLINE, &arg);
  848. /* pushback to free area */
  849. undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
  850. out:
  851. unlock_memory_hotplug();
  852. return ret;
  853. }
  854. int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
  855. {
  856. return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
  857. }
  858. int remove_memory(u64 start, u64 size)
  859. {
  860. struct memory_block *mem = NULL;
  861. struct mem_section *section;
  862. unsigned long start_pfn, end_pfn;
  863. unsigned long pfn, section_nr;
  864. int ret;
  865. start_pfn = PFN_DOWN(start);
  866. end_pfn = start_pfn + PFN_DOWN(size);
  867. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  868. section_nr = pfn_to_section_nr(pfn);
  869. if (!present_section_nr(section_nr))
  870. continue;
  871. section = __nr_to_section(section_nr);
  872. /* same memblock? */
  873. if (mem)
  874. if ((section_nr >= mem->start_section_nr) &&
  875. (section_nr <= mem->end_section_nr))
  876. continue;
  877. mem = find_memory_block_hinted(section, mem);
  878. if (!mem)
  879. continue;
  880. ret = offline_memory_block(mem);
  881. if (ret) {
  882. kobject_put(&mem->dev.kobj);
  883. return ret;
  884. }
  885. }
  886. if (mem)
  887. kobject_put(&mem->dev.kobj);
  888. return 0;
  889. }
  890. #else
  891. int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
  892. {
  893. return -EINVAL;
  894. }
  895. int remove_memory(u64 start, u64 size)
  896. {
  897. return -EINVAL;
  898. }
  899. #endif /* CONFIG_MEMORY_HOTREMOVE */
  900. EXPORT_SYMBOL_GPL(remove_memory);