memory_hotplug.c 29 KB

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