memory_hotplug.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022
  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/compiler.h>
  12. #include <linux/export.h>
  13. #include <linux/pagevec.h>
  14. #include <linux/writeback.h>
  15. #include <linux/slab.h>
  16. #include <linux/sysctl.h>
  17. #include <linux/cpu.h>
  18. #include <linux/memory.h>
  19. #include <linux/memory_hotplug.h>
  20. #include <linux/highmem.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/ioport.h>
  23. #include <linux/delay.h>
  24. #include <linux/migrate.h>
  25. #include <linux/page-isolation.h>
  26. #include <linux/pfn.h>
  27. #include <linux/suspend.h>
  28. #include <linux/mm_inline.h>
  29. #include <linux/firmware-map.h>
  30. #include <linux/stop_machine.h>
  31. #include <linux/hugetlb.h>
  32. #include <linux/memblock.h>
  33. #include <linux/bootmem.h>
  34. #include <asm/tlbflush.h>
  35. #include "internal.h"
  36. /*
  37. * online_page_callback contains pointer to current page onlining function.
  38. * Initially it is generic_online_page(). If it is required it could be
  39. * changed by calling set_online_page_callback() for callback registration
  40. * and restore_online_page_callback() for generic callback restore.
  41. */
  42. static void generic_online_page(struct page *page);
  43. static online_page_callback_t online_page_callback = generic_online_page;
  44. static DEFINE_MUTEX(online_page_callback_lock);
  45. /* The same as the cpu_hotplug lock, but for memory hotplug. */
  46. static struct {
  47. struct task_struct *active_writer;
  48. struct mutex lock; /* Synchronizes accesses to refcount, */
  49. /*
  50. * Also blocks the new readers during
  51. * an ongoing mem hotplug operation.
  52. */
  53. int refcount;
  54. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  55. struct lockdep_map dep_map;
  56. #endif
  57. } mem_hotplug = {
  58. .active_writer = NULL,
  59. .lock = __MUTEX_INITIALIZER(mem_hotplug.lock),
  60. .refcount = 0,
  61. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  62. .dep_map = {.name = "mem_hotplug.lock" },
  63. #endif
  64. };
  65. /* Lockdep annotations for get/put_online_mems() and mem_hotplug_begin/end() */
  66. #define memhp_lock_acquire_read() lock_map_acquire_read(&mem_hotplug.dep_map)
  67. #define memhp_lock_acquire() lock_map_acquire(&mem_hotplug.dep_map)
  68. #define memhp_lock_release() lock_map_release(&mem_hotplug.dep_map)
  69. void get_online_mems(void)
  70. {
  71. might_sleep();
  72. if (mem_hotplug.active_writer == current)
  73. return;
  74. memhp_lock_acquire_read();
  75. mutex_lock(&mem_hotplug.lock);
  76. mem_hotplug.refcount++;
  77. mutex_unlock(&mem_hotplug.lock);
  78. }
  79. void put_online_mems(void)
  80. {
  81. if (mem_hotplug.active_writer == current)
  82. return;
  83. mutex_lock(&mem_hotplug.lock);
  84. if (WARN_ON(!mem_hotplug.refcount))
  85. mem_hotplug.refcount++; /* try to fix things up */
  86. if (!--mem_hotplug.refcount && unlikely(mem_hotplug.active_writer))
  87. wake_up_process(mem_hotplug.active_writer);
  88. mutex_unlock(&mem_hotplug.lock);
  89. memhp_lock_release();
  90. }
  91. static void mem_hotplug_begin(void)
  92. {
  93. mem_hotplug.active_writer = current;
  94. memhp_lock_acquire();
  95. for (;;) {
  96. mutex_lock(&mem_hotplug.lock);
  97. if (likely(!mem_hotplug.refcount))
  98. break;
  99. __set_current_state(TASK_UNINTERRUPTIBLE);
  100. mutex_unlock(&mem_hotplug.lock);
  101. schedule();
  102. }
  103. }
  104. static void mem_hotplug_done(void)
  105. {
  106. mem_hotplug.active_writer = NULL;
  107. mutex_unlock(&mem_hotplug.lock);
  108. memhp_lock_release();
  109. }
  110. /* add this memory to iomem resource */
  111. static struct resource *register_memory_resource(u64 start, u64 size)
  112. {
  113. struct resource *res;
  114. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  115. BUG_ON(!res);
  116. res->name = "System RAM";
  117. res->start = start;
  118. res->end = start + size - 1;
  119. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  120. if (request_resource(&iomem_resource, res) < 0) {
  121. pr_debug("System RAM resource %pR cannot be added\n", res);
  122. kfree(res);
  123. res = NULL;
  124. }
  125. return res;
  126. }
  127. static void release_memory_resource(struct resource *res)
  128. {
  129. if (!res)
  130. return;
  131. release_resource(res);
  132. kfree(res);
  133. return;
  134. }
  135. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  136. void get_page_bootmem(unsigned long info, struct page *page,
  137. unsigned long type)
  138. {
  139. page->lru.next = (struct list_head *) type;
  140. SetPagePrivate(page);
  141. set_page_private(page, info);
  142. atomic_inc(&page->_count);
  143. }
  144. void put_page_bootmem(struct page *page)
  145. {
  146. unsigned long type;
  147. type = (unsigned long) page->lru.next;
  148. BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
  149. type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
  150. if (atomic_dec_return(&page->_count) == 1) {
  151. ClearPagePrivate(page);
  152. set_page_private(page, 0);
  153. INIT_LIST_HEAD(&page->lru);
  154. free_reserved_page(page);
  155. }
  156. }
  157. #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
  158. #ifndef CONFIG_SPARSEMEM_VMEMMAP
  159. static void register_page_bootmem_info_section(unsigned long start_pfn)
  160. {
  161. unsigned long *usemap, mapsize, section_nr, i;
  162. struct mem_section *ms;
  163. struct page *page, *memmap;
  164. section_nr = pfn_to_section_nr(start_pfn);
  165. ms = __nr_to_section(section_nr);
  166. /* Get section's memmap address */
  167. memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
  168. /*
  169. * Get page for the memmap's phys address
  170. * XXX: need more consideration for sparse_vmemmap...
  171. */
  172. page = virt_to_page(memmap);
  173. mapsize = sizeof(struct page) * PAGES_PER_SECTION;
  174. mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
  175. /* remember memmap's page */
  176. for (i = 0; i < mapsize; i++, page++)
  177. get_page_bootmem(section_nr, page, SECTION_INFO);
  178. usemap = __nr_to_section(section_nr)->pageblock_flags;
  179. page = virt_to_page(usemap);
  180. mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
  181. for (i = 0; i < mapsize; i++, page++)
  182. get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
  183. }
  184. #else /* CONFIG_SPARSEMEM_VMEMMAP */
  185. static void register_page_bootmem_info_section(unsigned long start_pfn)
  186. {
  187. unsigned long *usemap, mapsize, section_nr, i;
  188. struct mem_section *ms;
  189. struct page *page, *memmap;
  190. if (!pfn_valid(start_pfn))
  191. return;
  192. section_nr = pfn_to_section_nr(start_pfn);
  193. ms = __nr_to_section(section_nr);
  194. memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
  195. register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
  196. usemap = __nr_to_section(section_nr)->pageblock_flags;
  197. page = virt_to_page(usemap);
  198. mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
  199. for (i = 0; i < mapsize; i++, page++)
  200. get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
  201. }
  202. #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
  203. void register_page_bootmem_info_node(struct pglist_data *pgdat)
  204. {
  205. unsigned long i, pfn, end_pfn, nr_pages;
  206. int node = pgdat->node_id;
  207. struct page *page;
  208. struct zone *zone;
  209. nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
  210. page = virt_to_page(pgdat);
  211. for (i = 0; i < nr_pages; i++, page++)
  212. get_page_bootmem(node, page, NODE_INFO);
  213. zone = &pgdat->node_zones[0];
  214. for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
  215. if (zone_is_initialized(zone)) {
  216. nr_pages = zone->wait_table_hash_nr_entries
  217. * sizeof(wait_queue_head_t);
  218. nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
  219. page = virt_to_page(zone->wait_table);
  220. for (i = 0; i < nr_pages; i++, page++)
  221. get_page_bootmem(node, page, NODE_INFO);
  222. }
  223. }
  224. pfn = pgdat->node_start_pfn;
  225. end_pfn = pgdat_end_pfn(pgdat);
  226. /* register section info */
  227. for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  228. /*
  229. * Some platforms can assign the same pfn to multiple nodes - on
  230. * node0 as well as nodeN. To avoid registering a pfn against
  231. * multiple nodes we check that this pfn does not already
  232. * reside in some other nodes.
  233. */
  234. if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
  235. register_page_bootmem_info_section(pfn);
  236. }
  237. }
  238. #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
  239. static void __meminit grow_zone_span(struct zone *zone, unsigned long start_pfn,
  240. unsigned long end_pfn)
  241. {
  242. unsigned long old_zone_end_pfn;
  243. zone_span_writelock(zone);
  244. old_zone_end_pfn = zone_end_pfn(zone);
  245. if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
  246. zone->zone_start_pfn = start_pfn;
  247. zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
  248. zone->zone_start_pfn;
  249. zone_span_writeunlock(zone);
  250. }
  251. static void resize_zone(struct zone *zone, unsigned long start_pfn,
  252. unsigned long end_pfn)
  253. {
  254. zone_span_writelock(zone);
  255. if (end_pfn - start_pfn) {
  256. zone->zone_start_pfn = start_pfn;
  257. zone->spanned_pages = end_pfn - start_pfn;
  258. } else {
  259. /*
  260. * make it consist as free_area_init_core(),
  261. * if spanned_pages = 0, then keep start_pfn = 0
  262. */
  263. zone->zone_start_pfn = 0;
  264. zone->spanned_pages = 0;
  265. }
  266. zone_span_writeunlock(zone);
  267. }
  268. static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
  269. unsigned long end_pfn)
  270. {
  271. enum zone_type zid = zone_idx(zone);
  272. int nid = zone->zone_pgdat->node_id;
  273. unsigned long pfn;
  274. for (pfn = start_pfn; pfn < end_pfn; pfn++)
  275. set_page_links(pfn_to_page(pfn), zid, nid, pfn);
  276. }
  277. /* Can fail with -ENOMEM from allocating a wait table with vmalloc() or
  278. * alloc_bootmem_node_nopanic()/memblock_virt_alloc_node_nopanic() */
  279. static int __ref ensure_zone_is_initialized(struct zone *zone,
  280. unsigned long start_pfn, unsigned long num_pages)
  281. {
  282. if (!zone_is_initialized(zone))
  283. return init_currently_empty_zone(zone, start_pfn, num_pages,
  284. MEMMAP_HOTPLUG);
  285. return 0;
  286. }
  287. static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
  288. unsigned long start_pfn, unsigned long end_pfn)
  289. {
  290. int ret;
  291. unsigned long flags;
  292. unsigned long z1_start_pfn;
  293. ret = ensure_zone_is_initialized(z1, start_pfn, end_pfn - start_pfn);
  294. if (ret)
  295. return ret;
  296. pgdat_resize_lock(z1->zone_pgdat, &flags);
  297. /* can't move pfns which are higher than @z2 */
  298. if (end_pfn > zone_end_pfn(z2))
  299. goto out_fail;
  300. /* the move out part must be at the left most of @z2 */
  301. if (start_pfn > z2->zone_start_pfn)
  302. goto out_fail;
  303. /* must included/overlap */
  304. if (end_pfn <= z2->zone_start_pfn)
  305. goto out_fail;
  306. /* use start_pfn for z1's start_pfn if z1 is empty */
  307. if (!zone_is_empty(z1))
  308. z1_start_pfn = z1->zone_start_pfn;
  309. else
  310. z1_start_pfn = start_pfn;
  311. resize_zone(z1, z1_start_pfn, end_pfn);
  312. resize_zone(z2, end_pfn, zone_end_pfn(z2));
  313. pgdat_resize_unlock(z1->zone_pgdat, &flags);
  314. fix_zone_id(z1, start_pfn, end_pfn);
  315. return 0;
  316. out_fail:
  317. pgdat_resize_unlock(z1->zone_pgdat, &flags);
  318. return -1;
  319. }
  320. static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
  321. unsigned long start_pfn, unsigned long end_pfn)
  322. {
  323. int ret;
  324. unsigned long flags;
  325. unsigned long z2_end_pfn;
  326. ret = ensure_zone_is_initialized(z2, start_pfn, end_pfn - start_pfn);
  327. if (ret)
  328. return ret;
  329. pgdat_resize_lock(z1->zone_pgdat, &flags);
  330. /* can't move pfns which are lower than @z1 */
  331. if (z1->zone_start_pfn > start_pfn)
  332. goto out_fail;
  333. /* the move out part mast at the right most of @z1 */
  334. if (zone_end_pfn(z1) > end_pfn)
  335. goto out_fail;
  336. /* must included/overlap */
  337. if (start_pfn >= zone_end_pfn(z1))
  338. goto out_fail;
  339. /* use end_pfn for z2's end_pfn if z2 is empty */
  340. if (!zone_is_empty(z2))
  341. z2_end_pfn = zone_end_pfn(z2);
  342. else
  343. z2_end_pfn = end_pfn;
  344. resize_zone(z1, z1->zone_start_pfn, start_pfn);
  345. resize_zone(z2, start_pfn, z2_end_pfn);
  346. pgdat_resize_unlock(z1->zone_pgdat, &flags);
  347. fix_zone_id(z2, start_pfn, end_pfn);
  348. return 0;
  349. out_fail:
  350. pgdat_resize_unlock(z1->zone_pgdat, &flags);
  351. return -1;
  352. }
  353. static void __meminit grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
  354. unsigned long end_pfn)
  355. {
  356. unsigned long old_pgdat_end_pfn = pgdat_end_pfn(pgdat);
  357. if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
  358. pgdat->node_start_pfn = start_pfn;
  359. pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
  360. pgdat->node_start_pfn;
  361. }
  362. static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
  363. {
  364. struct pglist_data *pgdat = zone->zone_pgdat;
  365. int nr_pages = PAGES_PER_SECTION;
  366. int nid = pgdat->node_id;
  367. int zone_type;
  368. unsigned long flags;
  369. int ret;
  370. zone_type = zone - pgdat->node_zones;
  371. ret = ensure_zone_is_initialized(zone, phys_start_pfn, nr_pages);
  372. if (ret)
  373. return ret;
  374. pgdat_resize_lock(zone->zone_pgdat, &flags);
  375. grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
  376. grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
  377. phys_start_pfn + nr_pages);
  378. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  379. memmap_init_zone(nr_pages, nid, zone_type,
  380. phys_start_pfn, MEMMAP_HOTPLUG);
  381. return 0;
  382. }
  383. static int __meminit __add_section(int nid, struct zone *zone,
  384. unsigned long phys_start_pfn)
  385. {
  386. int ret;
  387. if (pfn_valid(phys_start_pfn))
  388. return -EEXIST;
  389. ret = sparse_add_one_section(zone, phys_start_pfn);
  390. if (ret < 0)
  391. return ret;
  392. ret = __add_zone(zone, phys_start_pfn);
  393. if (ret < 0)
  394. return ret;
  395. return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
  396. }
  397. /*
  398. * Reasonably generic function for adding memory. It is
  399. * expected that archs that support memory hotplug will
  400. * call this function after deciding the zone to which to
  401. * add the new pages.
  402. */
  403. int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
  404. unsigned long nr_pages)
  405. {
  406. unsigned long i;
  407. int err = 0;
  408. int start_sec, end_sec;
  409. /* during initialize mem_map, align hot-added range to section */
  410. start_sec = pfn_to_section_nr(phys_start_pfn);
  411. end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
  412. for (i = start_sec; i <= end_sec; i++) {
  413. err = __add_section(nid, zone, i << PFN_SECTION_SHIFT);
  414. /*
  415. * EEXIST is finally dealt with by ioresource collision
  416. * check. see add_memory() => register_memory_resource()
  417. * Warning will be printed if there is collision.
  418. */
  419. if (err && (err != -EEXIST))
  420. break;
  421. err = 0;
  422. }
  423. return err;
  424. }
  425. EXPORT_SYMBOL_GPL(__add_pages);
  426. #ifdef CONFIG_MEMORY_HOTREMOVE
  427. /* find the smallest valid pfn in the range [start_pfn, end_pfn) */
  428. static int find_smallest_section_pfn(int nid, struct zone *zone,
  429. unsigned long start_pfn,
  430. unsigned long end_pfn)
  431. {
  432. struct mem_section *ms;
  433. for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
  434. ms = __pfn_to_section(start_pfn);
  435. if (unlikely(!valid_section(ms)))
  436. continue;
  437. if (unlikely(pfn_to_nid(start_pfn) != nid))
  438. continue;
  439. if (zone && zone != page_zone(pfn_to_page(start_pfn)))
  440. continue;
  441. return start_pfn;
  442. }
  443. return 0;
  444. }
  445. /* find the biggest valid pfn in the range [start_pfn, end_pfn). */
  446. static int find_biggest_section_pfn(int nid, struct zone *zone,
  447. unsigned long start_pfn,
  448. unsigned long end_pfn)
  449. {
  450. struct mem_section *ms;
  451. unsigned long pfn;
  452. /* pfn is the end pfn of a memory section. */
  453. pfn = end_pfn - 1;
  454. for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
  455. ms = __pfn_to_section(pfn);
  456. if (unlikely(!valid_section(ms)))
  457. continue;
  458. if (unlikely(pfn_to_nid(pfn) != nid))
  459. continue;
  460. if (zone && zone != page_zone(pfn_to_page(pfn)))
  461. continue;
  462. return pfn;
  463. }
  464. return 0;
  465. }
  466. static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
  467. unsigned long end_pfn)
  468. {
  469. unsigned long zone_start_pfn = zone->zone_start_pfn;
  470. unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
  471. unsigned long zone_end_pfn = z;
  472. unsigned long pfn;
  473. struct mem_section *ms;
  474. int nid = zone_to_nid(zone);
  475. zone_span_writelock(zone);
  476. if (zone_start_pfn == start_pfn) {
  477. /*
  478. * If the section is smallest section in the zone, it need
  479. * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
  480. * In this case, we find second smallest valid mem_section
  481. * for shrinking zone.
  482. */
  483. pfn = find_smallest_section_pfn(nid, zone, end_pfn,
  484. zone_end_pfn);
  485. if (pfn) {
  486. zone->zone_start_pfn = pfn;
  487. zone->spanned_pages = zone_end_pfn - pfn;
  488. }
  489. } else if (zone_end_pfn == end_pfn) {
  490. /*
  491. * If the section is biggest section in the zone, it need
  492. * shrink zone->spanned_pages.
  493. * In this case, we find second biggest valid mem_section for
  494. * shrinking zone.
  495. */
  496. pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
  497. start_pfn);
  498. if (pfn)
  499. zone->spanned_pages = pfn - zone_start_pfn + 1;
  500. }
  501. /*
  502. * The section is not biggest or smallest mem_section in the zone, it
  503. * only creates a hole in the zone. So in this case, we need not
  504. * change the zone. But perhaps, the zone has only hole data. Thus
  505. * it check the zone has only hole or not.
  506. */
  507. pfn = zone_start_pfn;
  508. for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
  509. ms = __pfn_to_section(pfn);
  510. if (unlikely(!valid_section(ms)))
  511. continue;
  512. if (page_zone(pfn_to_page(pfn)) != zone)
  513. continue;
  514. /* If the section is current section, it continues the loop */
  515. if (start_pfn == pfn)
  516. continue;
  517. /* If we find valid section, we have nothing to do */
  518. zone_span_writeunlock(zone);
  519. return;
  520. }
  521. /* The zone has no valid section */
  522. zone->zone_start_pfn = 0;
  523. zone->spanned_pages = 0;
  524. zone_span_writeunlock(zone);
  525. }
  526. static void shrink_pgdat_span(struct pglist_data *pgdat,
  527. unsigned long start_pfn, unsigned long end_pfn)
  528. {
  529. unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
  530. unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */
  531. unsigned long pgdat_end_pfn = p;
  532. unsigned long pfn;
  533. struct mem_section *ms;
  534. int nid = pgdat->node_id;
  535. if (pgdat_start_pfn == start_pfn) {
  536. /*
  537. * If the section is smallest section in the pgdat, it need
  538. * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
  539. * In this case, we find second smallest valid mem_section
  540. * for shrinking zone.
  541. */
  542. pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
  543. pgdat_end_pfn);
  544. if (pfn) {
  545. pgdat->node_start_pfn = pfn;
  546. pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
  547. }
  548. } else if (pgdat_end_pfn == end_pfn) {
  549. /*
  550. * If the section is biggest section in the pgdat, it need
  551. * shrink pgdat->node_spanned_pages.
  552. * In this case, we find second biggest valid mem_section for
  553. * shrinking zone.
  554. */
  555. pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
  556. start_pfn);
  557. if (pfn)
  558. pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
  559. }
  560. /*
  561. * If the section is not biggest or smallest mem_section in the pgdat,
  562. * it only creates a hole in the pgdat. So in this case, we need not
  563. * change the pgdat.
  564. * But perhaps, the pgdat has only hole data. Thus it check the pgdat
  565. * has only hole or not.
  566. */
  567. pfn = pgdat_start_pfn;
  568. for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
  569. ms = __pfn_to_section(pfn);
  570. if (unlikely(!valid_section(ms)))
  571. continue;
  572. if (pfn_to_nid(pfn) != nid)
  573. continue;
  574. /* If the section is current section, it continues the loop */
  575. if (start_pfn == pfn)
  576. continue;
  577. /* If we find valid section, we have nothing to do */
  578. return;
  579. }
  580. /* The pgdat has no valid section */
  581. pgdat->node_start_pfn = 0;
  582. pgdat->node_spanned_pages = 0;
  583. }
  584. static void __remove_zone(struct zone *zone, unsigned long start_pfn)
  585. {
  586. struct pglist_data *pgdat = zone->zone_pgdat;
  587. int nr_pages = PAGES_PER_SECTION;
  588. int zone_type;
  589. unsigned long flags;
  590. zone_type = zone - pgdat->node_zones;
  591. pgdat_resize_lock(zone->zone_pgdat, &flags);
  592. shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
  593. shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
  594. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  595. }
  596. static int __remove_section(struct zone *zone, struct mem_section *ms)
  597. {
  598. unsigned long start_pfn;
  599. int scn_nr;
  600. int ret = -EINVAL;
  601. if (!valid_section(ms))
  602. return ret;
  603. ret = unregister_memory_section(ms);
  604. if (ret)
  605. return ret;
  606. scn_nr = __section_nr(ms);
  607. start_pfn = section_nr_to_pfn(scn_nr);
  608. __remove_zone(zone, start_pfn);
  609. sparse_remove_one_section(zone, ms);
  610. return 0;
  611. }
  612. /**
  613. * __remove_pages() - remove sections of pages from a zone
  614. * @zone: zone from which pages need to be removed
  615. * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
  616. * @nr_pages: number of pages to remove (must be multiple of section size)
  617. *
  618. * Generic helper function to remove section mappings and sysfs entries
  619. * for the section of the memory we are removing. Caller needs to make
  620. * sure that pages are marked reserved and zones are adjust properly by
  621. * calling offline_pages().
  622. */
  623. int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
  624. unsigned long nr_pages)
  625. {
  626. unsigned long i;
  627. int sections_to_remove;
  628. resource_size_t start, size;
  629. int ret = 0;
  630. /*
  631. * We can only remove entire sections
  632. */
  633. BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
  634. BUG_ON(nr_pages % PAGES_PER_SECTION);
  635. start = phys_start_pfn << PAGE_SHIFT;
  636. size = nr_pages * PAGE_SIZE;
  637. ret = release_mem_region_adjustable(&iomem_resource, start, size);
  638. if (ret) {
  639. resource_size_t endres = start + size - 1;
  640. pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
  641. &start, &endres, ret);
  642. }
  643. sections_to_remove = nr_pages / PAGES_PER_SECTION;
  644. for (i = 0; i < sections_to_remove; i++) {
  645. unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
  646. ret = __remove_section(zone, __pfn_to_section(pfn));
  647. if (ret)
  648. break;
  649. }
  650. return ret;
  651. }
  652. EXPORT_SYMBOL_GPL(__remove_pages);
  653. #endif /* CONFIG_MEMORY_HOTREMOVE */
  654. int set_online_page_callback(online_page_callback_t callback)
  655. {
  656. int rc = -EINVAL;
  657. get_online_mems();
  658. mutex_lock(&online_page_callback_lock);
  659. if (online_page_callback == generic_online_page) {
  660. online_page_callback = callback;
  661. rc = 0;
  662. }
  663. mutex_unlock(&online_page_callback_lock);
  664. put_online_mems();
  665. return rc;
  666. }
  667. EXPORT_SYMBOL_GPL(set_online_page_callback);
  668. int restore_online_page_callback(online_page_callback_t callback)
  669. {
  670. int rc = -EINVAL;
  671. get_online_mems();
  672. mutex_lock(&online_page_callback_lock);
  673. if (online_page_callback == callback) {
  674. online_page_callback = generic_online_page;
  675. rc = 0;
  676. }
  677. mutex_unlock(&online_page_callback_lock);
  678. put_online_mems();
  679. return rc;
  680. }
  681. EXPORT_SYMBOL_GPL(restore_online_page_callback);
  682. void __online_page_set_limits(struct page *page)
  683. {
  684. }
  685. EXPORT_SYMBOL_GPL(__online_page_set_limits);
  686. void __online_page_increment_counters(struct page *page)
  687. {
  688. adjust_managed_page_count(page, 1);
  689. }
  690. EXPORT_SYMBOL_GPL(__online_page_increment_counters);
  691. void __online_page_free(struct page *page)
  692. {
  693. __free_reserved_page(page);
  694. }
  695. EXPORT_SYMBOL_GPL(__online_page_free);
  696. static void generic_online_page(struct page *page)
  697. {
  698. __online_page_set_limits(page);
  699. __online_page_increment_counters(page);
  700. __online_page_free(page);
  701. }
  702. static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
  703. void *arg)
  704. {
  705. unsigned long i;
  706. unsigned long onlined_pages = *(unsigned long *)arg;
  707. struct page *page;
  708. if (PageReserved(pfn_to_page(start_pfn)))
  709. for (i = 0; i < nr_pages; i++) {
  710. page = pfn_to_page(start_pfn + i);
  711. (*online_page_callback)(page);
  712. onlined_pages++;
  713. }
  714. *(unsigned long *)arg = onlined_pages;
  715. return 0;
  716. }
  717. #ifdef CONFIG_MOVABLE_NODE
  718. /*
  719. * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
  720. * normal memory.
  721. */
  722. static bool can_online_high_movable(struct zone *zone)
  723. {
  724. return true;
  725. }
  726. #else /* CONFIG_MOVABLE_NODE */
  727. /* ensure every online node has NORMAL memory */
  728. static bool can_online_high_movable(struct zone *zone)
  729. {
  730. return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
  731. }
  732. #endif /* CONFIG_MOVABLE_NODE */
  733. /* check which state of node_states will be changed when online memory */
  734. static void node_states_check_changes_online(unsigned long nr_pages,
  735. struct zone *zone, struct memory_notify *arg)
  736. {
  737. int nid = zone_to_nid(zone);
  738. enum zone_type zone_last = ZONE_NORMAL;
  739. /*
  740. * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
  741. * contains nodes which have zones of 0...ZONE_NORMAL,
  742. * set zone_last to ZONE_NORMAL.
  743. *
  744. * If we don't have HIGHMEM nor movable node,
  745. * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
  746. * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
  747. */
  748. if (N_MEMORY == N_NORMAL_MEMORY)
  749. zone_last = ZONE_MOVABLE;
  750. /*
  751. * if the memory to be online is in a zone of 0...zone_last, and
  752. * the zones of 0...zone_last don't have memory before online, we will
  753. * need to set the node to node_states[N_NORMAL_MEMORY] after
  754. * the memory is online.
  755. */
  756. if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
  757. arg->status_change_nid_normal = nid;
  758. else
  759. arg->status_change_nid_normal = -1;
  760. #ifdef CONFIG_HIGHMEM
  761. /*
  762. * If we have movable node, node_states[N_HIGH_MEMORY]
  763. * contains nodes which have zones of 0...ZONE_HIGHMEM,
  764. * set zone_last to ZONE_HIGHMEM.
  765. *
  766. * If we don't have movable node, node_states[N_NORMAL_MEMORY]
  767. * contains nodes which have zones of 0...ZONE_MOVABLE,
  768. * set zone_last to ZONE_MOVABLE.
  769. */
  770. zone_last = ZONE_HIGHMEM;
  771. if (N_MEMORY == N_HIGH_MEMORY)
  772. zone_last = ZONE_MOVABLE;
  773. if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
  774. arg->status_change_nid_high = nid;
  775. else
  776. arg->status_change_nid_high = -1;
  777. #else
  778. arg->status_change_nid_high = arg->status_change_nid_normal;
  779. #endif
  780. /*
  781. * if the node don't have memory befor online, we will need to
  782. * set the node to node_states[N_MEMORY] after the memory
  783. * is online.
  784. */
  785. if (!node_state(nid, N_MEMORY))
  786. arg->status_change_nid = nid;
  787. else
  788. arg->status_change_nid = -1;
  789. }
  790. static void node_states_set_node(int node, struct memory_notify *arg)
  791. {
  792. if (arg->status_change_nid_normal >= 0)
  793. node_set_state(node, N_NORMAL_MEMORY);
  794. if (arg->status_change_nid_high >= 0)
  795. node_set_state(node, N_HIGH_MEMORY);
  796. node_set_state(node, N_MEMORY);
  797. }
  798. int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
  799. {
  800. unsigned long flags;
  801. unsigned long onlined_pages = 0;
  802. struct zone *zone;
  803. int need_zonelists_rebuild = 0;
  804. int nid;
  805. int ret;
  806. struct memory_notify arg;
  807. mem_hotplug_begin();
  808. /*
  809. * This doesn't need a lock to do pfn_to_page().
  810. * The section can't be removed here because of the
  811. * memory_block->state_mutex.
  812. */
  813. zone = page_zone(pfn_to_page(pfn));
  814. ret = -EINVAL;
  815. if ((zone_idx(zone) > ZONE_NORMAL ||
  816. online_type == MMOP_ONLINE_MOVABLE) &&
  817. !can_online_high_movable(zone))
  818. goto out;
  819. if (online_type == MMOP_ONLINE_KERNEL &&
  820. zone_idx(zone) == ZONE_MOVABLE) {
  821. if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages))
  822. goto out;
  823. }
  824. if (online_type == MMOP_ONLINE_MOVABLE &&
  825. zone_idx(zone) == ZONE_MOVABLE - 1) {
  826. if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages))
  827. goto out;
  828. }
  829. /* Previous code may changed the zone of the pfn range */
  830. zone = page_zone(pfn_to_page(pfn));
  831. arg.start_pfn = pfn;
  832. arg.nr_pages = nr_pages;
  833. node_states_check_changes_online(nr_pages, zone, &arg);
  834. nid = pfn_to_nid(pfn);
  835. ret = memory_notify(MEM_GOING_ONLINE, &arg);
  836. ret = notifier_to_errno(ret);
  837. if (ret) {
  838. memory_notify(MEM_CANCEL_ONLINE, &arg);
  839. goto out;
  840. }
  841. /*
  842. * If this zone is not populated, then it is not in zonelist.
  843. * This means the page allocator ignores this zone.
  844. * So, zonelist must be updated after online.
  845. */
  846. mutex_lock(&zonelists_mutex);
  847. if (!populated_zone(zone)) {
  848. need_zonelists_rebuild = 1;
  849. build_all_zonelists(NULL, zone);
  850. }
  851. ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
  852. online_pages_range);
  853. if (ret) {
  854. if (need_zonelists_rebuild)
  855. zone_pcp_reset(zone);
  856. mutex_unlock(&zonelists_mutex);
  857. printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
  858. (unsigned long long) pfn << PAGE_SHIFT,
  859. (((unsigned long long) pfn + nr_pages)
  860. << PAGE_SHIFT) - 1);
  861. memory_notify(MEM_CANCEL_ONLINE, &arg);
  862. goto out;
  863. }
  864. zone->present_pages += onlined_pages;
  865. pgdat_resize_lock(zone->zone_pgdat, &flags);
  866. zone->zone_pgdat->node_present_pages += onlined_pages;
  867. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  868. if (onlined_pages) {
  869. node_states_set_node(zone_to_nid(zone), &arg);
  870. if (need_zonelists_rebuild)
  871. build_all_zonelists(NULL, NULL);
  872. else
  873. zone_pcp_update(zone);
  874. }
  875. mutex_unlock(&zonelists_mutex);
  876. init_per_zone_wmark_min();
  877. if (onlined_pages)
  878. kswapd_run(zone_to_nid(zone));
  879. vm_total_pages = nr_free_pagecache_pages();
  880. writeback_set_ratelimit();
  881. if (onlined_pages)
  882. memory_notify(MEM_ONLINE, &arg);
  883. out:
  884. mem_hotplug_done();
  885. return ret;
  886. }
  887. #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
  888. static void reset_node_present_pages(pg_data_t *pgdat)
  889. {
  890. struct zone *z;
  891. for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
  892. z->present_pages = 0;
  893. pgdat->node_present_pages = 0;
  894. }
  895. /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
  896. static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
  897. {
  898. struct pglist_data *pgdat;
  899. unsigned long zones_size[MAX_NR_ZONES] = {0};
  900. unsigned long zholes_size[MAX_NR_ZONES] = {0};
  901. unsigned long start_pfn = PFN_DOWN(start);
  902. pgdat = NODE_DATA(nid);
  903. if (!pgdat) {
  904. pgdat = arch_alloc_nodedata(nid);
  905. if (!pgdat)
  906. return NULL;
  907. arch_refresh_nodedata(nid, pgdat);
  908. } else {
  909. /* Reset the nr_zones and classzone_idx to 0 before reuse */
  910. pgdat->nr_zones = 0;
  911. pgdat->classzone_idx = 0;
  912. }
  913. /* we can use NODE_DATA(nid) from here */
  914. /* init node's zones as empty zones, we don't have any present pages.*/
  915. free_area_init_node(nid, zones_size, start_pfn, zholes_size);
  916. /*
  917. * The node we allocated has no zone fallback lists. For avoiding
  918. * to access not-initialized zonelist, build here.
  919. */
  920. mutex_lock(&zonelists_mutex);
  921. build_all_zonelists(pgdat, NULL);
  922. mutex_unlock(&zonelists_mutex);
  923. /*
  924. * zone->managed_pages is set to an approximate value in
  925. * free_area_init_core(), which will cause
  926. * /sys/device/system/node/nodeX/meminfo has wrong data.
  927. * So reset it to 0 before any memory is onlined.
  928. */
  929. reset_node_managed_pages(pgdat);
  930. /*
  931. * When memory is hot-added, all the memory is in offline state. So
  932. * clear all zones' present_pages because they will be updated in
  933. * online_pages() and offline_pages().
  934. */
  935. reset_node_present_pages(pgdat);
  936. return pgdat;
  937. }
  938. static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
  939. {
  940. arch_refresh_nodedata(nid, NULL);
  941. arch_free_nodedata(pgdat);
  942. return;
  943. }
  944. /**
  945. * try_online_node - online a node if offlined
  946. *
  947. * called by cpu_up() to online a node without onlined memory.
  948. */
  949. int try_online_node(int nid)
  950. {
  951. pg_data_t *pgdat;
  952. int ret;
  953. if (node_online(nid))
  954. return 0;
  955. mem_hotplug_begin();
  956. pgdat = hotadd_new_pgdat(nid, 0);
  957. if (!pgdat) {
  958. pr_err("Cannot online node %d due to NULL pgdat\n", nid);
  959. ret = -ENOMEM;
  960. goto out;
  961. }
  962. node_set_online(nid);
  963. ret = register_one_node(nid);
  964. BUG_ON(ret);
  965. if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
  966. mutex_lock(&zonelists_mutex);
  967. build_all_zonelists(NULL, NULL);
  968. mutex_unlock(&zonelists_mutex);
  969. }
  970. out:
  971. mem_hotplug_done();
  972. return ret;
  973. }
  974. static int check_hotplug_memory_range(u64 start, u64 size)
  975. {
  976. u64 start_pfn = PFN_DOWN(start);
  977. u64 nr_pages = size >> PAGE_SHIFT;
  978. /* Memory range must be aligned with section */
  979. if ((start_pfn & ~PAGE_SECTION_MASK) ||
  980. (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
  981. pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
  982. (unsigned long long)start,
  983. (unsigned long long)size);
  984. return -EINVAL;
  985. }
  986. return 0;
  987. }
  988. /*
  989. * If movable zone has already been setup, newly added memory should be check.
  990. * If its address is higher than movable zone, it should be added as movable.
  991. * Without this check, movable zone may overlap with other zone.
  992. */
  993. static int should_add_memory_movable(int nid, u64 start, u64 size)
  994. {
  995. unsigned long start_pfn = start >> PAGE_SHIFT;
  996. pg_data_t *pgdat = NODE_DATA(nid);
  997. struct zone *movable_zone = pgdat->node_zones + ZONE_MOVABLE;
  998. if (zone_is_empty(movable_zone))
  999. return 0;
  1000. if (movable_zone->zone_start_pfn <= start_pfn)
  1001. return 1;
  1002. return 0;
  1003. }
  1004. int zone_for_memory(int nid, u64 start, u64 size, int zone_default)
  1005. {
  1006. if (should_add_memory_movable(nid, start, size))
  1007. return ZONE_MOVABLE;
  1008. return zone_default;
  1009. }
  1010. /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
  1011. int __ref add_memory(int nid, u64 start, u64 size)
  1012. {
  1013. pg_data_t *pgdat = NULL;
  1014. bool new_pgdat;
  1015. bool new_node;
  1016. struct resource *res;
  1017. int ret;
  1018. ret = check_hotplug_memory_range(start, size);
  1019. if (ret)
  1020. return ret;
  1021. res = register_memory_resource(start, size);
  1022. ret = -EEXIST;
  1023. if (!res)
  1024. return ret;
  1025. { /* Stupid hack to suppress address-never-null warning */
  1026. void *p = NODE_DATA(nid);
  1027. new_pgdat = !p;
  1028. }
  1029. mem_hotplug_begin();
  1030. new_node = !node_online(nid);
  1031. if (new_node) {
  1032. pgdat = hotadd_new_pgdat(nid, start);
  1033. ret = -ENOMEM;
  1034. if (!pgdat)
  1035. goto error;
  1036. }
  1037. /* call arch's memory hotadd */
  1038. ret = arch_add_memory(nid, start, size);
  1039. if (ret < 0)
  1040. goto error;
  1041. /* we online node here. we can't roll back from here. */
  1042. node_set_online(nid);
  1043. if (new_node) {
  1044. ret = register_one_node(nid);
  1045. /*
  1046. * If sysfs file of new node can't create, cpu on the node
  1047. * can't be hot-added. There is no rollback way now.
  1048. * So, check by BUG_ON() to catch it reluctantly..
  1049. */
  1050. BUG_ON(ret);
  1051. }
  1052. /* create new memmap entry */
  1053. firmware_map_add_hotplug(start, start + size, "System RAM");
  1054. goto out;
  1055. error:
  1056. /* rollback pgdat allocation and others */
  1057. if (new_pgdat)
  1058. rollback_node_hotadd(nid, pgdat);
  1059. release_memory_resource(res);
  1060. out:
  1061. mem_hotplug_done();
  1062. return ret;
  1063. }
  1064. EXPORT_SYMBOL_GPL(add_memory);
  1065. #ifdef CONFIG_MEMORY_HOTREMOVE
  1066. /*
  1067. * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
  1068. * set and the size of the free page is given by page_order(). Using this,
  1069. * the function determines if the pageblock contains only free pages.
  1070. * Due to buddy contraints, a free page at least the size of a pageblock will
  1071. * be located at the start of the pageblock
  1072. */
  1073. static inline int pageblock_free(struct page *page)
  1074. {
  1075. return PageBuddy(page) && page_order(page) >= pageblock_order;
  1076. }
  1077. /* Return the start of the next active pageblock after a given page */
  1078. static struct page *next_active_pageblock(struct page *page)
  1079. {
  1080. /* Ensure the starting page is pageblock-aligned */
  1081. BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
  1082. /* If the entire pageblock is free, move to the end of free page */
  1083. if (pageblock_free(page)) {
  1084. int order;
  1085. /* be careful. we don't have locks, page_order can be changed.*/
  1086. order = page_order(page);
  1087. if ((order < MAX_ORDER) && (order >= pageblock_order))
  1088. return page + (1 << order);
  1089. }
  1090. return page + pageblock_nr_pages;
  1091. }
  1092. /* Checks if this range of memory is likely to be hot-removable. */
  1093. int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
  1094. {
  1095. struct page *page = pfn_to_page(start_pfn);
  1096. struct page *end_page = page + nr_pages;
  1097. /* Check the starting page of each pageblock within the range */
  1098. for (; page < end_page; page = next_active_pageblock(page)) {
  1099. if (!is_pageblock_removable_nolock(page))
  1100. return 0;
  1101. cond_resched();
  1102. }
  1103. /* All pageblocks in the memory block are likely to be hot-removable */
  1104. return 1;
  1105. }
  1106. /*
  1107. * Confirm all pages in a range [start, end) is belongs to the same zone.
  1108. */
  1109. int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
  1110. {
  1111. unsigned long pfn;
  1112. struct zone *zone = NULL;
  1113. struct page *page;
  1114. int i;
  1115. for (pfn = start_pfn;
  1116. pfn < end_pfn;
  1117. pfn += MAX_ORDER_NR_PAGES) {
  1118. i = 0;
  1119. /* This is just a CONFIG_HOLES_IN_ZONE check.*/
  1120. while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
  1121. i++;
  1122. if (i == MAX_ORDER_NR_PAGES)
  1123. continue;
  1124. page = pfn_to_page(pfn + i);
  1125. if (zone && page_zone(page) != zone)
  1126. return 0;
  1127. zone = page_zone(page);
  1128. }
  1129. return 1;
  1130. }
  1131. /*
  1132. * Scan pfn range [start,end) to find movable/migratable pages (LRU pages
  1133. * and hugepages). We scan pfn because it's much easier than scanning over
  1134. * linked list. This function returns the pfn of the first found movable
  1135. * page if it's found, otherwise 0.
  1136. */
  1137. static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
  1138. {
  1139. unsigned long pfn;
  1140. struct page *page;
  1141. for (pfn = start; pfn < end; pfn++) {
  1142. if (pfn_valid(pfn)) {
  1143. page = pfn_to_page(pfn);
  1144. if (PageLRU(page))
  1145. return pfn;
  1146. if (PageHuge(page)) {
  1147. if (is_hugepage_active(page))
  1148. return pfn;
  1149. else
  1150. pfn = round_up(pfn + 1,
  1151. 1 << compound_order(page)) - 1;
  1152. }
  1153. }
  1154. }
  1155. return 0;
  1156. }
  1157. #define NR_OFFLINE_AT_ONCE_PAGES (256)
  1158. static int
  1159. do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
  1160. {
  1161. unsigned long pfn;
  1162. struct page *page;
  1163. int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
  1164. int not_managed = 0;
  1165. int ret = 0;
  1166. LIST_HEAD(source);
  1167. for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
  1168. if (!pfn_valid(pfn))
  1169. continue;
  1170. page = pfn_to_page(pfn);
  1171. if (PageHuge(page)) {
  1172. struct page *head = compound_head(page);
  1173. pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
  1174. if (compound_order(head) > PFN_SECTION_SHIFT) {
  1175. ret = -EBUSY;
  1176. break;
  1177. }
  1178. if (isolate_huge_page(page, &source))
  1179. move_pages -= 1 << compound_order(head);
  1180. continue;
  1181. }
  1182. if (!get_page_unless_zero(page))
  1183. continue;
  1184. /*
  1185. * We can skip free pages. And we can only deal with pages on
  1186. * LRU.
  1187. */
  1188. ret = isolate_lru_page(page);
  1189. if (!ret) { /* Success */
  1190. put_page(page);
  1191. list_add_tail(&page->lru, &source);
  1192. move_pages--;
  1193. inc_zone_page_state(page, NR_ISOLATED_ANON +
  1194. page_is_file_cache(page));
  1195. } else {
  1196. #ifdef CONFIG_DEBUG_VM
  1197. printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
  1198. pfn);
  1199. dump_page(page, "failed to remove from LRU");
  1200. #endif
  1201. put_page(page);
  1202. /* Because we don't have big zone->lock. we should
  1203. check this again here. */
  1204. if (page_count(page)) {
  1205. not_managed++;
  1206. ret = -EBUSY;
  1207. break;
  1208. }
  1209. }
  1210. }
  1211. if (!list_empty(&source)) {
  1212. if (not_managed) {
  1213. putback_movable_pages(&source);
  1214. goto out;
  1215. }
  1216. /*
  1217. * alloc_migrate_target should be improooooved!!
  1218. * migrate_pages returns # of failed pages.
  1219. */
  1220. ret = migrate_pages(&source, alloc_migrate_target, NULL, 0,
  1221. MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
  1222. if (ret)
  1223. putback_movable_pages(&source);
  1224. }
  1225. out:
  1226. return ret;
  1227. }
  1228. /*
  1229. * remove from free_area[] and mark all as Reserved.
  1230. */
  1231. static int
  1232. offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
  1233. void *data)
  1234. {
  1235. __offline_isolated_pages(start, start + nr_pages);
  1236. return 0;
  1237. }
  1238. static void
  1239. offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
  1240. {
  1241. walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
  1242. offline_isolated_pages_cb);
  1243. }
  1244. /*
  1245. * Check all pages in range, recoreded as memory resource, are isolated.
  1246. */
  1247. static int
  1248. check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
  1249. void *data)
  1250. {
  1251. int ret;
  1252. long offlined = *(long *)data;
  1253. ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
  1254. offlined = nr_pages;
  1255. if (!ret)
  1256. *(long *)data += offlined;
  1257. return ret;
  1258. }
  1259. static long
  1260. check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
  1261. {
  1262. long offlined = 0;
  1263. int ret;
  1264. ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
  1265. check_pages_isolated_cb);
  1266. if (ret < 0)
  1267. offlined = (long)ret;
  1268. return offlined;
  1269. }
  1270. #ifdef CONFIG_MOVABLE_NODE
  1271. /*
  1272. * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
  1273. * normal memory.
  1274. */
  1275. static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
  1276. {
  1277. return true;
  1278. }
  1279. #else /* CONFIG_MOVABLE_NODE */
  1280. /* ensure the node has NORMAL memory if it is still online */
  1281. static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
  1282. {
  1283. struct pglist_data *pgdat = zone->zone_pgdat;
  1284. unsigned long present_pages = 0;
  1285. enum zone_type zt;
  1286. for (zt = 0; zt <= ZONE_NORMAL; zt++)
  1287. present_pages += pgdat->node_zones[zt].present_pages;
  1288. if (present_pages > nr_pages)
  1289. return true;
  1290. present_pages = 0;
  1291. for (; zt <= ZONE_MOVABLE; zt++)
  1292. present_pages += pgdat->node_zones[zt].present_pages;
  1293. /*
  1294. * we can't offline the last normal memory until all
  1295. * higher memory is offlined.
  1296. */
  1297. return present_pages == 0;
  1298. }
  1299. #endif /* CONFIG_MOVABLE_NODE */
  1300. static int __init cmdline_parse_movable_node(char *p)
  1301. {
  1302. #ifdef CONFIG_MOVABLE_NODE
  1303. /*
  1304. * Memory used by the kernel cannot be hot-removed because Linux
  1305. * cannot migrate the kernel pages. When memory hotplug is
  1306. * enabled, we should prevent memblock from allocating memory
  1307. * for the kernel.
  1308. *
  1309. * ACPI SRAT records all hotpluggable memory ranges. But before
  1310. * SRAT is parsed, we don't know about it.
  1311. *
  1312. * The kernel image is loaded into memory at very early time. We
  1313. * cannot prevent this anyway. So on NUMA system, we set any
  1314. * node the kernel resides in as un-hotpluggable.
  1315. *
  1316. * Since on modern servers, one node could have double-digit
  1317. * gigabytes memory, we can assume the memory around the kernel
  1318. * image is also un-hotpluggable. So before SRAT is parsed, just
  1319. * allocate memory near the kernel image to try the best to keep
  1320. * the kernel away from hotpluggable memory.
  1321. */
  1322. memblock_set_bottom_up(true);
  1323. movable_node_enabled = true;
  1324. #else
  1325. pr_warn("movable_node option not supported\n");
  1326. #endif
  1327. return 0;
  1328. }
  1329. early_param("movable_node", cmdline_parse_movable_node);
  1330. /* check which state of node_states will be changed when offline memory */
  1331. static void node_states_check_changes_offline(unsigned long nr_pages,
  1332. struct zone *zone, struct memory_notify *arg)
  1333. {
  1334. struct pglist_data *pgdat = zone->zone_pgdat;
  1335. unsigned long present_pages = 0;
  1336. enum zone_type zt, zone_last = ZONE_NORMAL;
  1337. /*
  1338. * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
  1339. * contains nodes which have zones of 0...ZONE_NORMAL,
  1340. * set zone_last to ZONE_NORMAL.
  1341. *
  1342. * If we don't have HIGHMEM nor movable node,
  1343. * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
  1344. * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
  1345. */
  1346. if (N_MEMORY == N_NORMAL_MEMORY)
  1347. zone_last = ZONE_MOVABLE;
  1348. /*
  1349. * check whether node_states[N_NORMAL_MEMORY] will be changed.
  1350. * If the memory to be offline is in a zone of 0...zone_last,
  1351. * and it is the last present memory, 0...zone_last will
  1352. * become empty after offline , thus we can determind we will
  1353. * need to clear the node from node_states[N_NORMAL_MEMORY].
  1354. */
  1355. for (zt = 0; zt <= zone_last; zt++)
  1356. present_pages += pgdat->node_zones[zt].present_pages;
  1357. if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
  1358. arg->status_change_nid_normal = zone_to_nid(zone);
  1359. else
  1360. arg->status_change_nid_normal = -1;
  1361. #ifdef CONFIG_HIGHMEM
  1362. /*
  1363. * If we have movable node, node_states[N_HIGH_MEMORY]
  1364. * contains nodes which have zones of 0...ZONE_HIGHMEM,
  1365. * set zone_last to ZONE_HIGHMEM.
  1366. *
  1367. * If we don't have movable node, node_states[N_NORMAL_MEMORY]
  1368. * contains nodes which have zones of 0...ZONE_MOVABLE,
  1369. * set zone_last to ZONE_MOVABLE.
  1370. */
  1371. zone_last = ZONE_HIGHMEM;
  1372. if (N_MEMORY == N_HIGH_MEMORY)
  1373. zone_last = ZONE_MOVABLE;
  1374. for (; zt <= zone_last; zt++)
  1375. present_pages += pgdat->node_zones[zt].present_pages;
  1376. if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
  1377. arg->status_change_nid_high = zone_to_nid(zone);
  1378. else
  1379. arg->status_change_nid_high = -1;
  1380. #else
  1381. arg->status_change_nid_high = arg->status_change_nid_normal;
  1382. #endif
  1383. /*
  1384. * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
  1385. */
  1386. zone_last = ZONE_MOVABLE;
  1387. /*
  1388. * check whether node_states[N_HIGH_MEMORY] will be changed
  1389. * If we try to offline the last present @nr_pages from the node,
  1390. * we can determind we will need to clear the node from
  1391. * node_states[N_HIGH_MEMORY].
  1392. */
  1393. for (; zt <= zone_last; zt++)
  1394. present_pages += pgdat->node_zones[zt].present_pages;
  1395. if (nr_pages >= present_pages)
  1396. arg->status_change_nid = zone_to_nid(zone);
  1397. else
  1398. arg->status_change_nid = -1;
  1399. }
  1400. static void node_states_clear_node(int node, struct memory_notify *arg)
  1401. {
  1402. if (arg->status_change_nid_normal >= 0)
  1403. node_clear_state(node, N_NORMAL_MEMORY);
  1404. if ((N_MEMORY != N_NORMAL_MEMORY) &&
  1405. (arg->status_change_nid_high >= 0))
  1406. node_clear_state(node, N_HIGH_MEMORY);
  1407. if ((N_MEMORY != N_HIGH_MEMORY) &&
  1408. (arg->status_change_nid >= 0))
  1409. node_clear_state(node, N_MEMORY);
  1410. }
  1411. static int __ref __offline_pages(unsigned long start_pfn,
  1412. unsigned long end_pfn, unsigned long timeout)
  1413. {
  1414. unsigned long pfn, nr_pages, expire;
  1415. long offlined_pages;
  1416. int ret, drain, retry_max, node;
  1417. unsigned long flags;
  1418. struct zone *zone;
  1419. struct memory_notify arg;
  1420. /* at least, alignment against pageblock is necessary */
  1421. if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
  1422. return -EINVAL;
  1423. if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
  1424. return -EINVAL;
  1425. /* This makes hotplug much easier...and readable.
  1426. we assume this for now. .*/
  1427. if (!test_pages_in_a_zone(start_pfn, end_pfn))
  1428. return -EINVAL;
  1429. mem_hotplug_begin();
  1430. zone = page_zone(pfn_to_page(start_pfn));
  1431. node = zone_to_nid(zone);
  1432. nr_pages = end_pfn - start_pfn;
  1433. ret = -EINVAL;
  1434. if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
  1435. goto out;
  1436. /* set above range as isolated */
  1437. ret = start_isolate_page_range(start_pfn, end_pfn,
  1438. MIGRATE_MOVABLE, true);
  1439. if (ret)
  1440. goto out;
  1441. arg.start_pfn = start_pfn;
  1442. arg.nr_pages = nr_pages;
  1443. node_states_check_changes_offline(nr_pages, zone, &arg);
  1444. ret = memory_notify(MEM_GOING_OFFLINE, &arg);
  1445. ret = notifier_to_errno(ret);
  1446. if (ret)
  1447. goto failed_removal;
  1448. pfn = start_pfn;
  1449. expire = jiffies + timeout;
  1450. drain = 0;
  1451. retry_max = 5;
  1452. repeat:
  1453. /* start memory hot removal */
  1454. ret = -EAGAIN;
  1455. if (time_after(jiffies, expire))
  1456. goto failed_removal;
  1457. ret = -EINTR;
  1458. if (signal_pending(current))
  1459. goto failed_removal;
  1460. ret = 0;
  1461. if (drain) {
  1462. lru_add_drain_all();
  1463. cond_resched();
  1464. drain_all_pages(zone);
  1465. }
  1466. pfn = scan_movable_pages(start_pfn, end_pfn);
  1467. if (pfn) { /* We have movable pages */
  1468. ret = do_migrate_range(pfn, end_pfn);
  1469. if (!ret) {
  1470. drain = 1;
  1471. goto repeat;
  1472. } else {
  1473. if (ret < 0)
  1474. if (--retry_max == 0)
  1475. goto failed_removal;
  1476. yield();
  1477. drain = 1;
  1478. goto repeat;
  1479. }
  1480. }
  1481. /* drain all zone's lru pagevec, this is asynchronous... */
  1482. lru_add_drain_all();
  1483. yield();
  1484. /* drain pcp pages, this is synchronous. */
  1485. drain_all_pages(zone);
  1486. /*
  1487. * dissolve free hugepages in the memory block before doing offlining
  1488. * actually in order to make hugetlbfs's object counting consistent.
  1489. */
  1490. dissolve_free_huge_pages(start_pfn, end_pfn);
  1491. /* check again */
  1492. offlined_pages = check_pages_isolated(start_pfn, end_pfn);
  1493. if (offlined_pages < 0) {
  1494. ret = -EBUSY;
  1495. goto failed_removal;
  1496. }
  1497. printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
  1498. /* Ok, all of our target is isolated.
  1499. We cannot do rollback at this point. */
  1500. offline_isolated_pages(start_pfn, end_pfn);
  1501. /* reset pagetype flags and makes migrate type to be MOVABLE */
  1502. undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
  1503. /* removal success */
  1504. adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
  1505. zone->present_pages -= offlined_pages;
  1506. pgdat_resize_lock(zone->zone_pgdat, &flags);
  1507. zone->zone_pgdat->node_present_pages -= offlined_pages;
  1508. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  1509. init_per_zone_wmark_min();
  1510. if (!populated_zone(zone)) {
  1511. zone_pcp_reset(zone);
  1512. mutex_lock(&zonelists_mutex);
  1513. build_all_zonelists(NULL, NULL);
  1514. mutex_unlock(&zonelists_mutex);
  1515. } else
  1516. zone_pcp_update(zone);
  1517. node_states_clear_node(node, &arg);
  1518. if (arg.status_change_nid >= 0)
  1519. kswapd_stop(node);
  1520. vm_total_pages = nr_free_pagecache_pages();
  1521. writeback_set_ratelimit();
  1522. memory_notify(MEM_OFFLINE, &arg);
  1523. mem_hotplug_done();
  1524. return 0;
  1525. failed_removal:
  1526. printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
  1527. (unsigned long long) start_pfn << PAGE_SHIFT,
  1528. ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
  1529. memory_notify(MEM_CANCEL_OFFLINE, &arg);
  1530. /* pushback to free area */
  1531. undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
  1532. out:
  1533. mem_hotplug_done();
  1534. return ret;
  1535. }
  1536. int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
  1537. {
  1538. return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
  1539. }
  1540. #endif /* CONFIG_MEMORY_HOTREMOVE */
  1541. /**
  1542. * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
  1543. * @start_pfn: start pfn of the memory range
  1544. * @end_pfn: end pfn of the memory range
  1545. * @arg: argument passed to func
  1546. * @func: callback for each memory section walked
  1547. *
  1548. * This function walks through all present mem sections in range
  1549. * [start_pfn, end_pfn) and call func on each mem section.
  1550. *
  1551. * Returns the return value of func.
  1552. */
  1553. int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
  1554. void *arg, int (*func)(struct memory_block *, void *))
  1555. {
  1556. struct memory_block *mem = NULL;
  1557. struct mem_section *section;
  1558. unsigned long pfn, section_nr;
  1559. int ret;
  1560. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  1561. section_nr = pfn_to_section_nr(pfn);
  1562. if (!present_section_nr(section_nr))
  1563. continue;
  1564. section = __nr_to_section(section_nr);
  1565. /* same memblock? */
  1566. if (mem)
  1567. if ((section_nr >= mem->start_section_nr) &&
  1568. (section_nr <= mem->end_section_nr))
  1569. continue;
  1570. mem = find_memory_block_hinted(section, mem);
  1571. if (!mem)
  1572. continue;
  1573. ret = func(mem, arg);
  1574. if (ret) {
  1575. kobject_put(&mem->dev.kobj);
  1576. return ret;
  1577. }
  1578. }
  1579. if (mem)
  1580. kobject_put(&mem->dev.kobj);
  1581. return 0;
  1582. }
  1583. #ifdef CONFIG_MEMORY_HOTREMOVE
  1584. static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
  1585. {
  1586. int ret = !is_memblock_offlined(mem);
  1587. if (unlikely(ret)) {
  1588. phys_addr_t beginpa, endpa;
  1589. beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
  1590. endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
  1591. pr_warn("removing memory fails, because memory "
  1592. "[%pa-%pa] is onlined\n",
  1593. &beginpa, &endpa);
  1594. }
  1595. return ret;
  1596. }
  1597. static int check_cpu_on_node(pg_data_t *pgdat)
  1598. {
  1599. int cpu;
  1600. for_each_present_cpu(cpu) {
  1601. if (cpu_to_node(cpu) == pgdat->node_id)
  1602. /*
  1603. * the cpu on this node isn't removed, and we can't
  1604. * offline this node.
  1605. */
  1606. return -EBUSY;
  1607. }
  1608. return 0;
  1609. }
  1610. static void unmap_cpu_on_node(pg_data_t *pgdat)
  1611. {
  1612. #ifdef CONFIG_ACPI_NUMA
  1613. int cpu;
  1614. for_each_possible_cpu(cpu)
  1615. if (cpu_to_node(cpu) == pgdat->node_id)
  1616. numa_clear_node(cpu);
  1617. #endif
  1618. }
  1619. static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
  1620. {
  1621. int ret;
  1622. ret = check_cpu_on_node(pgdat);
  1623. if (ret)
  1624. return ret;
  1625. /*
  1626. * the node will be offlined when we come here, so we can clear
  1627. * the cpu_to_node() now.
  1628. */
  1629. unmap_cpu_on_node(pgdat);
  1630. return 0;
  1631. }
  1632. /**
  1633. * try_offline_node
  1634. *
  1635. * Offline a node if all memory sections and cpus of the node are removed.
  1636. *
  1637. * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
  1638. * and online/offline operations before this call.
  1639. */
  1640. void try_offline_node(int nid)
  1641. {
  1642. pg_data_t *pgdat = NODE_DATA(nid);
  1643. unsigned long start_pfn = pgdat->node_start_pfn;
  1644. unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
  1645. unsigned long pfn;
  1646. int i;
  1647. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  1648. unsigned long section_nr = pfn_to_section_nr(pfn);
  1649. if (!present_section_nr(section_nr))
  1650. continue;
  1651. if (pfn_to_nid(pfn) != nid)
  1652. continue;
  1653. /*
  1654. * some memory sections of this node are not removed, and we
  1655. * can't offline node now.
  1656. */
  1657. return;
  1658. }
  1659. if (check_and_unmap_cpu_on_node(pgdat))
  1660. return;
  1661. /*
  1662. * all memory/cpu of this node are removed, we can offline this
  1663. * node now.
  1664. */
  1665. node_set_offline(nid);
  1666. unregister_one_node(nid);
  1667. /* free waittable in each zone */
  1668. for (i = 0; i < MAX_NR_ZONES; i++) {
  1669. struct zone *zone = pgdat->node_zones + i;
  1670. /*
  1671. * wait_table may be allocated from boot memory,
  1672. * here only free if it's allocated by vmalloc.
  1673. */
  1674. if (is_vmalloc_addr(zone->wait_table))
  1675. vfree(zone->wait_table);
  1676. }
  1677. }
  1678. EXPORT_SYMBOL(try_offline_node);
  1679. /**
  1680. * remove_memory
  1681. *
  1682. * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
  1683. * and online/offline operations before this call, as required by
  1684. * try_offline_node().
  1685. */
  1686. void __ref remove_memory(int nid, u64 start, u64 size)
  1687. {
  1688. int ret;
  1689. BUG_ON(check_hotplug_memory_range(start, size));
  1690. mem_hotplug_begin();
  1691. /*
  1692. * All memory blocks must be offlined before removing memory. Check
  1693. * whether all memory blocks in question are offline and trigger a BUG()
  1694. * if this is not the case.
  1695. */
  1696. ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
  1697. check_memblock_offlined_cb);
  1698. if (ret)
  1699. BUG();
  1700. /* remove memmap entry */
  1701. firmware_map_remove(start, start + size, "System RAM");
  1702. arch_remove_memory(start, size);
  1703. try_offline_node(nid);
  1704. mem_hotplug_done();
  1705. }
  1706. EXPORT_SYMBOL_GPL(remove_memory);
  1707. #endif /* CONFIG_MEMORY_HOTREMOVE */