memory_hotplug.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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 (onlined_pages) {
  426. node_set_state(zone_to_nid(zone), N_HIGH_MEMORY);
  427. if (need_zonelists_rebuild)
  428. build_all_zonelists(NULL, zone);
  429. else
  430. zone_pcp_update(zone);
  431. }
  432. mutex_unlock(&zonelists_mutex);
  433. init_per_zone_wmark_min();
  434. if (onlined_pages)
  435. kswapd_run(zone_to_nid(zone));
  436. vm_total_pages = nr_free_pagecache_pages();
  437. writeback_set_ratelimit();
  438. if (onlined_pages)
  439. memory_notify(MEM_ONLINE, &arg);
  440. unlock_memory_hotplug();
  441. return 0;
  442. }
  443. #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
  444. /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
  445. static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
  446. {
  447. struct pglist_data *pgdat;
  448. unsigned long zones_size[MAX_NR_ZONES] = {0};
  449. unsigned long zholes_size[MAX_NR_ZONES] = {0};
  450. unsigned long start_pfn = start >> PAGE_SHIFT;
  451. pgdat = arch_alloc_nodedata(nid);
  452. if (!pgdat)
  453. return NULL;
  454. arch_refresh_nodedata(nid, pgdat);
  455. /* we can use NODE_DATA(nid) from here */
  456. /* init node's zones as empty zones, we don't have any present pages.*/
  457. free_area_init_node(nid, zones_size, start_pfn, zholes_size);
  458. /*
  459. * The node we allocated has no zone fallback lists. For avoiding
  460. * to access not-initialized zonelist, build here.
  461. */
  462. mutex_lock(&zonelists_mutex);
  463. build_all_zonelists(pgdat, NULL);
  464. mutex_unlock(&zonelists_mutex);
  465. return pgdat;
  466. }
  467. static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
  468. {
  469. arch_refresh_nodedata(nid, NULL);
  470. arch_free_nodedata(pgdat);
  471. return;
  472. }
  473. /*
  474. * called by cpu_up() to online a node without onlined memory.
  475. */
  476. int mem_online_node(int nid)
  477. {
  478. pg_data_t *pgdat;
  479. int ret;
  480. lock_memory_hotplug();
  481. pgdat = hotadd_new_pgdat(nid, 0);
  482. if (!pgdat) {
  483. ret = -ENOMEM;
  484. goto out;
  485. }
  486. node_set_online(nid);
  487. ret = register_one_node(nid);
  488. BUG_ON(ret);
  489. out:
  490. unlock_memory_hotplug();
  491. return ret;
  492. }
  493. /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
  494. int __ref add_memory(int nid, u64 start, u64 size)
  495. {
  496. pg_data_t *pgdat = NULL;
  497. int new_pgdat = 0;
  498. struct resource *res;
  499. int ret;
  500. lock_memory_hotplug();
  501. res = register_memory_resource(start, size);
  502. ret = -EEXIST;
  503. if (!res)
  504. goto out;
  505. if (!node_online(nid)) {
  506. pgdat = hotadd_new_pgdat(nid, start);
  507. ret = -ENOMEM;
  508. if (!pgdat)
  509. goto error;
  510. new_pgdat = 1;
  511. }
  512. /* call arch's memory hotadd */
  513. ret = arch_add_memory(nid, start, size);
  514. if (ret < 0)
  515. goto error;
  516. /* we online node here. we can't roll back from here. */
  517. node_set_online(nid);
  518. if (new_pgdat) {
  519. ret = register_one_node(nid);
  520. /*
  521. * If sysfs file of new node can't create, cpu on the node
  522. * can't be hot-added. There is no rollback way now.
  523. * So, check by BUG_ON() to catch it reluctantly..
  524. */
  525. BUG_ON(ret);
  526. }
  527. /* create new memmap entry */
  528. firmware_map_add_hotplug(start, start + size, "System RAM");
  529. goto out;
  530. error:
  531. /* rollback pgdat allocation and others */
  532. if (new_pgdat)
  533. rollback_node_hotadd(nid, pgdat);
  534. if (res)
  535. release_memory_resource(res);
  536. out:
  537. unlock_memory_hotplug();
  538. return ret;
  539. }
  540. EXPORT_SYMBOL_GPL(add_memory);
  541. #ifdef CONFIG_MEMORY_HOTREMOVE
  542. /*
  543. * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
  544. * set and the size of the free page is given by page_order(). Using this,
  545. * the function determines if the pageblock contains only free pages.
  546. * Due to buddy contraints, a free page at least the size of a pageblock will
  547. * be located at the start of the pageblock
  548. */
  549. static inline int pageblock_free(struct page *page)
  550. {
  551. return PageBuddy(page) && page_order(page) >= pageblock_order;
  552. }
  553. /* Return the start of the next active pageblock after a given page */
  554. static struct page *next_active_pageblock(struct page *page)
  555. {
  556. /* Ensure the starting page is pageblock-aligned */
  557. BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
  558. /* If the entire pageblock is free, move to the end of free page */
  559. if (pageblock_free(page)) {
  560. int order;
  561. /* be careful. we don't have locks, page_order can be changed.*/
  562. order = page_order(page);
  563. if ((order < MAX_ORDER) && (order >= pageblock_order))
  564. return page + (1 << order);
  565. }
  566. return page + pageblock_nr_pages;
  567. }
  568. /* Checks if this range of memory is likely to be hot-removable. */
  569. int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
  570. {
  571. struct page *page = pfn_to_page(start_pfn);
  572. struct page *end_page = page + nr_pages;
  573. /* Check the starting page of each pageblock within the range */
  574. for (; page < end_page; page = next_active_pageblock(page)) {
  575. if (!is_pageblock_removable_nolock(page))
  576. return 0;
  577. cond_resched();
  578. }
  579. /* All pageblocks in the memory block are likely to be hot-removable */
  580. return 1;
  581. }
  582. /*
  583. * Confirm all pages in a range [start, end) is belongs to the same zone.
  584. */
  585. static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
  586. {
  587. unsigned long pfn;
  588. struct zone *zone = NULL;
  589. struct page *page;
  590. int i;
  591. for (pfn = start_pfn;
  592. pfn < end_pfn;
  593. pfn += MAX_ORDER_NR_PAGES) {
  594. i = 0;
  595. /* This is just a CONFIG_HOLES_IN_ZONE check.*/
  596. while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
  597. i++;
  598. if (i == MAX_ORDER_NR_PAGES)
  599. continue;
  600. page = pfn_to_page(pfn + i);
  601. if (zone && page_zone(page) != zone)
  602. return 0;
  603. zone = page_zone(page);
  604. }
  605. return 1;
  606. }
  607. /*
  608. * Scanning pfn is much easier than scanning lru list.
  609. * Scan pfn from start to end and Find LRU page.
  610. */
  611. static unsigned long scan_lru_pages(unsigned long start, unsigned long end)
  612. {
  613. unsigned long pfn;
  614. struct page *page;
  615. for (pfn = start; pfn < end; pfn++) {
  616. if (pfn_valid(pfn)) {
  617. page = pfn_to_page(pfn);
  618. if (PageLRU(page))
  619. return pfn;
  620. }
  621. }
  622. return 0;
  623. }
  624. static struct page *
  625. hotremove_migrate_alloc(struct page *page, unsigned long private, int **x)
  626. {
  627. /* This should be improooooved!! */
  628. return alloc_page(GFP_HIGHUSER_MOVABLE);
  629. }
  630. #define NR_OFFLINE_AT_ONCE_PAGES (256)
  631. static int
  632. do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
  633. {
  634. unsigned long pfn;
  635. struct page *page;
  636. int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
  637. int not_managed = 0;
  638. int ret = 0;
  639. LIST_HEAD(source);
  640. for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
  641. if (!pfn_valid(pfn))
  642. continue;
  643. page = pfn_to_page(pfn);
  644. if (!get_page_unless_zero(page))
  645. continue;
  646. /*
  647. * We can skip free pages. And we can only deal with pages on
  648. * LRU.
  649. */
  650. ret = isolate_lru_page(page);
  651. if (!ret) { /* Success */
  652. put_page(page);
  653. list_add_tail(&page->lru, &source);
  654. move_pages--;
  655. inc_zone_page_state(page, NR_ISOLATED_ANON +
  656. page_is_file_cache(page));
  657. } else {
  658. #ifdef CONFIG_DEBUG_VM
  659. printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
  660. pfn);
  661. dump_page(page);
  662. #endif
  663. put_page(page);
  664. /* Because we don't have big zone->lock. we should
  665. check this again here. */
  666. if (page_count(page)) {
  667. not_managed++;
  668. ret = -EBUSY;
  669. break;
  670. }
  671. }
  672. }
  673. if (!list_empty(&source)) {
  674. if (not_managed) {
  675. putback_lru_pages(&source);
  676. goto out;
  677. }
  678. /* this function returns # of failed pages */
  679. ret = migrate_pages(&source, hotremove_migrate_alloc, 0,
  680. true, MIGRATE_SYNC);
  681. if (ret)
  682. putback_lru_pages(&source);
  683. }
  684. out:
  685. return ret;
  686. }
  687. /*
  688. * remove from free_area[] and mark all as Reserved.
  689. */
  690. static int
  691. offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
  692. void *data)
  693. {
  694. __offline_isolated_pages(start, start + nr_pages);
  695. return 0;
  696. }
  697. static void
  698. offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
  699. {
  700. walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
  701. offline_isolated_pages_cb);
  702. }
  703. /*
  704. * Check all pages in range, recoreded as memory resource, are isolated.
  705. */
  706. static int
  707. check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
  708. void *data)
  709. {
  710. int ret;
  711. long offlined = *(long *)data;
  712. ret = test_pages_isolated(start_pfn, start_pfn + nr_pages);
  713. offlined = nr_pages;
  714. if (!ret)
  715. *(long *)data += offlined;
  716. return ret;
  717. }
  718. static long
  719. check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
  720. {
  721. long offlined = 0;
  722. int ret;
  723. ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
  724. check_pages_isolated_cb);
  725. if (ret < 0)
  726. offlined = (long)ret;
  727. return offlined;
  728. }
  729. static int __ref offline_pages(unsigned long start_pfn,
  730. unsigned long end_pfn, unsigned long timeout)
  731. {
  732. unsigned long pfn, nr_pages, expire;
  733. long offlined_pages;
  734. int ret, drain, retry_max, node;
  735. struct zone *zone;
  736. struct memory_notify arg;
  737. BUG_ON(start_pfn >= end_pfn);
  738. /* at least, alignment against pageblock is necessary */
  739. if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
  740. return -EINVAL;
  741. if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
  742. return -EINVAL;
  743. /* This makes hotplug much easier...and readable.
  744. we assume this for now. .*/
  745. if (!test_pages_in_a_zone(start_pfn, end_pfn))
  746. return -EINVAL;
  747. lock_memory_hotplug();
  748. zone = page_zone(pfn_to_page(start_pfn));
  749. node = zone_to_nid(zone);
  750. nr_pages = end_pfn - start_pfn;
  751. /* set above range as isolated */
  752. ret = start_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
  753. if (ret)
  754. goto out;
  755. arg.start_pfn = start_pfn;
  756. arg.nr_pages = nr_pages;
  757. arg.status_change_nid = -1;
  758. if (nr_pages >= node_present_pages(node))
  759. arg.status_change_nid = node;
  760. ret = memory_notify(MEM_GOING_OFFLINE, &arg);
  761. ret = notifier_to_errno(ret);
  762. if (ret)
  763. goto failed_removal;
  764. pfn = start_pfn;
  765. expire = jiffies + timeout;
  766. drain = 0;
  767. retry_max = 5;
  768. repeat:
  769. /* start memory hot removal */
  770. ret = -EAGAIN;
  771. if (time_after(jiffies, expire))
  772. goto failed_removal;
  773. ret = -EINTR;
  774. if (signal_pending(current))
  775. goto failed_removal;
  776. ret = 0;
  777. if (drain) {
  778. lru_add_drain_all();
  779. cond_resched();
  780. drain_all_pages();
  781. }
  782. pfn = scan_lru_pages(start_pfn, end_pfn);
  783. if (pfn) { /* We have page on LRU */
  784. ret = do_migrate_range(pfn, end_pfn);
  785. if (!ret) {
  786. drain = 1;
  787. goto repeat;
  788. } else {
  789. if (ret < 0)
  790. if (--retry_max == 0)
  791. goto failed_removal;
  792. yield();
  793. drain = 1;
  794. goto repeat;
  795. }
  796. }
  797. /* drain all zone's lru pagevec, this is asyncronous... */
  798. lru_add_drain_all();
  799. yield();
  800. /* drain pcp pages , this is synchrouns. */
  801. drain_all_pages();
  802. /* check again */
  803. offlined_pages = check_pages_isolated(start_pfn, end_pfn);
  804. if (offlined_pages < 0) {
  805. ret = -EBUSY;
  806. goto failed_removal;
  807. }
  808. printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
  809. /* Ok, all of our target is islaoted.
  810. We cannot do rollback at this point. */
  811. offline_isolated_pages(start_pfn, end_pfn);
  812. /* reset pagetype flags and makes migrate type to be MOVABLE */
  813. undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
  814. /* removal success */
  815. zone->present_pages -= offlined_pages;
  816. zone->zone_pgdat->node_present_pages -= offlined_pages;
  817. totalram_pages -= offlined_pages;
  818. init_per_zone_wmark_min();
  819. if (!populated_zone(zone))
  820. zone_pcp_reset(zone);
  821. if (!node_present_pages(node)) {
  822. node_clear_state(node, N_HIGH_MEMORY);
  823. kswapd_stop(node);
  824. }
  825. vm_total_pages = nr_free_pagecache_pages();
  826. writeback_set_ratelimit();
  827. memory_notify(MEM_OFFLINE, &arg);
  828. unlock_memory_hotplug();
  829. return 0;
  830. failed_removal:
  831. printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
  832. (unsigned long long) start_pfn << PAGE_SHIFT,
  833. ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
  834. memory_notify(MEM_CANCEL_OFFLINE, &arg);
  835. /* pushback to free area */
  836. undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
  837. out:
  838. unlock_memory_hotplug();
  839. return ret;
  840. }
  841. int remove_memory(u64 start, u64 size)
  842. {
  843. unsigned long start_pfn, end_pfn;
  844. start_pfn = PFN_DOWN(start);
  845. end_pfn = start_pfn + PFN_DOWN(size);
  846. return offline_pages(start_pfn, end_pfn, 120 * HZ);
  847. }
  848. #else
  849. int remove_memory(u64 start, u64 size)
  850. {
  851. return -EINVAL;
  852. }
  853. #endif /* CONFIG_MEMORY_HOTREMOVE */
  854. EXPORT_SYMBOL_GPL(remove_memory);