memory_hotplug.c 26 KB

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