memory_hotplug.c 24 KB

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