page_isolation.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/mm/page_isolation.c
  4. */
  5. #include <linux/mm.h>
  6. #include <linux/page-isolation.h>
  7. #include <linux/pageblock-flags.h>
  8. #include <linux/memory.h>
  9. #include <linux/hugetlb.h>
  10. #include <linux/page_owner.h>
  11. #include <linux/page_pinner.h>
  12. #include <linux/migrate.h>
  13. #include "internal.h"
  14. #define CREATE_TRACE_POINTS
  15. #include <trace/events/page_isolation.h>
  16. /*
  17. * This function checks whether the range [start_pfn, end_pfn) includes
  18. * unmovable pages or not. The range must fall into a single pageblock and
  19. * consequently belong to a single zone.
  20. *
  21. * PageLRU check without isolation or lru_lock could race so that
  22. * MIGRATE_MOVABLE block might include unmovable pages. And __PageMovable
  23. * check without lock_page also may miss some movable non-lru pages at
  24. * race condition. So you can't expect this function should be exact.
  25. *
  26. * Returns a page without holding a reference. If the caller wants to
  27. * dereference that page (e.g., dumping), it has to make sure that it
  28. * cannot get removed (e.g., via memory unplug) concurrently.
  29. *
  30. */
  31. static struct page *has_unmovable_pages(unsigned long start_pfn, unsigned long end_pfn,
  32. int migratetype, int flags)
  33. {
  34. struct page *page = pfn_to_page(start_pfn);
  35. struct zone *zone = page_zone(page);
  36. unsigned long pfn;
  37. VM_BUG_ON(pageblock_start_pfn(start_pfn) !=
  38. pageblock_start_pfn(end_pfn - 1));
  39. if (is_migrate_cma_page(page)) {
  40. /*
  41. * CMA allocations (alloc_contig_range) really need to mark
  42. * isolate CMA pageblocks even when they are not movable in fact
  43. * so consider them movable here.
  44. */
  45. if (is_migrate_cma(migratetype))
  46. return NULL;
  47. return page;
  48. }
  49. for (pfn = start_pfn; pfn < end_pfn; pfn++) {
  50. page = pfn_to_page(pfn);
  51. /*
  52. * Both, bootmem allocations and memory holes are marked
  53. * PG_reserved and are unmovable. We can even have unmovable
  54. * allocations inside ZONE_MOVABLE, for example when
  55. * specifying "movablecore".
  56. */
  57. if (PageReserved(page))
  58. return page;
  59. /*
  60. * If the zone is movable and we have ruled out all reserved
  61. * pages then it should be reasonably safe to assume the rest
  62. * is movable.
  63. */
  64. if (zone_idx(zone) == ZONE_MOVABLE)
  65. continue;
  66. /*
  67. * Hugepages are not in LRU lists, but they're movable.
  68. * THPs are on the LRU, but need to be counted as #small pages.
  69. * We need not scan over tail pages because we don't
  70. * handle each tail page individually in migration.
  71. */
  72. if (PageHuge(page) || PageTransCompound(page)) {
  73. struct page *head = compound_head(page);
  74. unsigned int skip_pages;
  75. if (PageHuge(page)) {
  76. if (!hugepage_migration_supported(page_hstate(head)))
  77. return page;
  78. } else if (!PageLRU(head) && !__PageMovable(head)) {
  79. return page;
  80. }
  81. skip_pages = compound_nr(head) - (page - head);
  82. pfn += skip_pages - 1;
  83. continue;
  84. }
  85. /*
  86. * We can't use page_count without pin a page
  87. * because another CPU can free compound page.
  88. * This check already skips compound tails of THP
  89. * because their page->_refcount is zero at all time.
  90. */
  91. if (!page_ref_count(page)) {
  92. if (PageBuddy(page))
  93. pfn += (1 << buddy_order(page)) - 1;
  94. continue;
  95. }
  96. /*
  97. * The HWPoisoned page may be not in buddy system, and
  98. * page_count() is not 0.
  99. */
  100. if ((flags & MEMORY_OFFLINE) && PageHWPoison(page))
  101. continue;
  102. /*
  103. * We treat all PageOffline() pages as movable when offlining
  104. * to give drivers a chance to decrement their reference count
  105. * in MEM_GOING_OFFLINE in order to indicate that these pages
  106. * can be offlined as there are no direct references anymore.
  107. * For actually unmovable PageOffline() where the driver does
  108. * not support this, we will fail later when trying to actually
  109. * move these pages that still have a reference count > 0.
  110. * (false negatives in this function only)
  111. */
  112. if ((flags & MEMORY_OFFLINE) && PageOffline(page))
  113. continue;
  114. if (__PageMovable(page) || PageLRU(page))
  115. continue;
  116. /*
  117. * If there are RECLAIMABLE pages, we need to check
  118. * it. But now, memory offline itself doesn't call
  119. * shrink_node_slabs() and it still to be fixed.
  120. */
  121. return page;
  122. }
  123. return NULL;
  124. }
  125. /*
  126. * This function set pageblock migratetype to isolate if no unmovable page is
  127. * present in [start_pfn, end_pfn). The pageblock must intersect with
  128. * [start_pfn, end_pfn).
  129. */
  130. static int set_migratetype_isolate(struct page *page, int migratetype, int isol_flags,
  131. unsigned long start_pfn, unsigned long end_pfn)
  132. {
  133. struct zone *zone = page_zone(page);
  134. struct page *unmovable;
  135. unsigned long flags;
  136. unsigned long check_unmovable_start, check_unmovable_end;
  137. spin_lock_irqsave(&zone->lock, flags);
  138. /*
  139. * We assume the caller intended to SET migrate type to isolate.
  140. * If it is already set, then someone else must have raced and
  141. * set it before us.
  142. */
  143. if (is_migrate_isolate_page(page)) {
  144. spin_unlock_irqrestore(&zone->lock, flags);
  145. return -EBUSY;
  146. }
  147. /*
  148. * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself.
  149. * We just check MOVABLE pages.
  150. *
  151. * Pass the intersection of [start_pfn, end_pfn) and the page's pageblock
  152. * to avoid redundant checks.
  153. */
  154. check_unmovable_start = max(page_to_pfn(page), start_pfn);
  155. check_unmovable_end = min(pageblock_end_pfn(page_to_pfn(page)),
  156. end_pfn);
  157. unmovable = has_unmovable_pages(check_unmovable_start, check_unmovable_end,
  158. migratetype, isol_flags);
  159. if (!unmovable) {
  160. unsigned long nr_pages;
  161. int mt = get_pageblock_migratetype(page);
  162. set_pageblock_migratetype(page, MIGRATE_ISOLATE);
  163. zone->nr_isolate_pageblock++;
  164. nr_pages = move_freepages_block(zone, page, MIGRATE_ISOLATE,
  165. NULL);
  166. __mod_zone_freepage_state(zone, -nr_pages, mt);
  167. spin_unlock_irqrestore(&zone->lock, flags);
  168. return 0;
  169. }
  170. spin_unlock_irqrestore(&zone->lock, flags);
  171. if (isol_flags & REPORT_FAILURE) {
  172. /*
  173. * printk() with zone->lock held will likely trigger a
  174. * lockdep splat, so defer it here.
  175. */
  176. dump_page(unmovable, "unmovable page");
  177. }
  178. return -EBUSY;
  179. }
  180. static void unset_migratetype_isolate(struct page *page, int migratetype)
  181. {
  182. struct zone *zone;
  183. unsigned long flags, nr_pages;
  184. bool isolated_page = false;
  185. unsigned int order;
  186. struct page *buddy;
  187. zone = page_zone(page);
  188. spin_lock_irqsave(&zone->lock, flags);
  189. if (!is_migrate_isolate_page(page))
  190. goto out;
  191. /*
  192. * Because freepage with more than pageblock_order on isolated
  193. * pageblock is restricted to merge due to freepage counting problem,
  194. * it is possible that there is free buddy page.
  195. * move_freepages_block() doesn't care of merge so we need other
  196. * approach in order to merge them. Isolation and free will make
  197. * these pages to be merged.
  198. */
  199. if (PageBuddy(page)) {
  200. order = buddy_order(page);
  201. if (order >= pageblock_order && order < MAX_ORDER - 1) {
  202. buddy = find_buddy_page_pfn(page, page_to_pfn(page),
  203. order, NULL);
  204. if (buddy && !is_migrate_isolate_page(buddy)) {
  205. isolated_page = !!__isolate_free_page(page, order);
  206. /*
  207. * Isolating a free page in an isolated pageblock
  208. * is expected to always work as watermarks don't
  209. * apply here.
  210. */
  211. VM_WARN_ON(!isolated_page);
  212. }
  213. }
  214. }
  215. /*
  216. * If we isolate freepage with more than pageblock_order, there
  217. * should be no freepage in the range, so we could avoid costly
  218. * pageblock scanning for freepage moving.
  219. *
  220. * We didn't actually touch any of the isolated pages, so place them
  221. * to the tail of the freelist. This is an optimization for memory
  222. * onlining - just onlined memory won't immediately be considered for
  223. * allocation.
  224. */
  225. if (!isolated_page) {
  226. nr_pages = move_freepages_block(zone, page, migratetype, NULL);
  227. __mod_zone_freepage_state(zone, nr_pages, migratetype);
  228. }
  229. set_pageblock_migratetype(page, migratetype);
  230. if (isolated_page)
  231. __putback_isolated_page(page, order, migratetype);
  232. zone->nr_isolate_pageblock--;
  233. out:
  234. spin_unlock_irqrestore(&zone->lock, flags);
  235. }
  236. static inline struct page *
  237. __first_valid_page(unsigned long pfn, unsigned long nr_pages)
  238. {
  239. int i;
  240. for (i = 0; i < nr_pages; i++) {
  241. struct page *page;
  242. page = pfn_to_online_page(pfn + i);
  243. if (!page)
  244. continue;
  245. return page;
  246. }
  247. return NULL;
  248. }
  249. /**
  250. * isolate_single_pageblock() -- tries to isolate a pageblock that might be
  251. * within a free or in-use page.
  252. * @boundary_pfn: pageblock-aligned pfn that a page might cross
  253. * @flags: isolation flags
  254. * @gfp_flags: GFP flags used for migrating pages
  255. * @isolate_before: isolate the pageblock before the boundary_pfn
  256. * @skip_isolation: the flag to skip the pageblock isolation in second
  257. * isolate_single_pageblock()
  258. * @migratetype: migrate type to set in error recovery.
  259. *
  260. * Free and in-use pages can be as big as MAX_ORDER-1 and contain more than one
  261. * pageblock. When not all pageblocks within a page are isolated at the same
  262. * time, free page accounting can go wrong. For example, in the case of
  263. * MAX_ORDER-1 = pageblock_order + 1, a MAX_ORDER-1 page has two pagelbocks.
  264. * [ MAX_ORDER-1 ]
  265. * [ pageblock0 | pageblock1 ]
  266. * When either pageblock is isolated, if it is a free page, the page is not
  267. * split into separate migratetype lists, which is supposed to; if it is an
  268. * in-use page and freed later, __free_one_page() does not split the free page
  269. * either. The function handles this by splitting the free page or migrating
  270. * the in-use page then splitting the free page.
  271. */
  272. static int isolate_single_pageblock(unsigned long boundary_pfn, int flags,
  273. gfp_t gfp_flags, bool isolate_before, bool skip_isolation,
  274. int migratetype)
  275. {
  276. unsigned long start_pfn;
  277. unsigned long isolate_pageblock;
  278. unsigned long pfn;
  279. struct zone *zone;
  280. int ret;
  281. VM_BUG_ON(!pageblock_aligned(boundary_pfn));
  282. if (isolate_before)
  283. isolate_pageblock = boundary_pfn - pageblock_nr_pages;
  284. else
  285. isolate_pageblock = boundary_pfn;
  286. /*
  287. * scan at the beginning of MAX_ORDER_NR_PAGES aligned range to avoid
  288. * only isolating a subset of pageblocks from a bigger than pageblock
  289. * free or in-use page. Also make sure all to-be-isolated pageblocks
  290. * are within the same zone.
  291. */
  292. zone = page_zone(pfn_to_page(isolate_pageblock));
  293. start_pfn = max(ALIGN_DOWN(isolate_pageblock, MAX_ORDER_NR_PAGES),
  294. zone->zone_start_pfn);
  295. if (skip_isolation) {
  296. int mt __maybe_unused = get_pageblock_migratetype(pfn_to_page(isolate_pageblock));
  297. VM_BUG_ON(!is_migrate_isolate(mt));
  298. } else {
  299. ret = set_migratetype_isolate(pfn_to_page(isolate_pageblock), migratetype,
  300. flags, isolate_pageblock, isolate_pageblock + pageblock_nr_pages);
  301. if (ret)
  302. return ret;
  303. }
  304. /*
  305. * Bail out early when the to-be-isolated pageblock does not form
  306. * a free or in-use page across boundary_pfn:
  307. *
  308. * 1. isolate before boundary_pfn: the page after is not online
  309. * 2. isolate after boundary_pfn: the page before is not online
  310. *
  311. * This also ensures correctness. Without it, when isolate after
  312. * boundary_pfn and [start_pfn, boundary_pfn) are not online,
  313. * __first_valid_page() will return unexpected NULL in the for loop
  314. * below.
  315. */
  316. if (isolate_before) {
  317. if (!pfn_to_online_page(boundary_pfn))
  318. return 0;
  319. } else {
  320. if (!pfn_to_online_page(boundary_pfn - 1))
  321. return 0;
  322. }
  323. for (pfn = start_pfn; pfn < boundary_pfn;) {
  324. struct page *page = __first_valid_page(pfn, boundary_pfn - pfn);
  325. VM_BUG_ON(!page);
  326. pfn = page_to_pfn(page);
  327. /*
  328. * start_pfn is MAX_ORDER_NR_PAGES aligned, if there is any
  329. * free pages in [start_pfn, boundary_pfn), its head page will
  330. * always be in the range.
  331. */
  332. if (PageBuddy(page)) {
  333. int order = buddy_order(page);
  334. if (pfn + (1UL << order) > boundary_pfn) {
  335. /* free page changed before split, check it again */
  336. if (split_free_page(page, order, boundary_pfn - pfn))
  337. continue;
  338. }
  339. pfn += 1UL << order;
  340. continue;
  341. }
  342. /*
  343. * migrate compound pages then let the free page handling code
  344. * above do the rest. If migration is not possible, just fail.
  345. */
  346. if (PageCompound(page)) {
  347. struct page *head = compound_head(page);
  348. unsigned long head_pfn = page_to_pfn(head);
  349. unsigned long nr_pages = compound_nr(head);
  350. if (head_pfn + nr_pages <= boundary_pfn) {
  351. pfn = head_pfn + nr_pages;
  352. continue;
  353. }
  354. #if defined CONFIG_COMPACTION || defined CONFIG_CMA
  355. /*
  356. * hugetlb, lru compound (THP), and movable compound pages
  357. * can be migrated. Otherwise, fail the isolation.
  358. */
  359. if (PageHuge(page) || PageLRU(page) || __PageMovable(page)) {
  360. int order;
  361. unsigned long outer_pfn;
  362. int page_mt = get_pageblock_migratetype(page);
  363. bool isolate_page = !is_migrate_isolate_page(page);
  364. struct compact_control cc = {
  365. .nr_migratepages = 0,
  366. .order = -1,
  367. .zone = page_zone(pfn_to_page(head_pfn)),
  368. .mode = MIGRATE_SYNC,
  369. .ignore_skip_hint = true,
  370. .no_set_skip_hint = true,
  371. .gfp_mask = gfp_flags,
  372. .alloc_contig = true,
  373. };
  374. INIT_LIST_HEAD(&cc.migratepages);
  375. /*
  376. * XXX: mark the page as MIGRATE_ISOLATE so that
  377. * no one else can grab the freed page after migration.
  378. * Ideally, the page should be freed as two separate
  379. * pages to be added into separate migratetype free
  380. * lists.
  381. */
  382. if (isolate_page) {
  383. ret = set_migratetype_isolate(page, page_mt,
  384. flags, head_pfn, head_pfn + nr_pages);
  385. if (ret)
  386. goto failed;
  387. }
  388. ret = __alloc_contig_migrate_range(&cc, head_pfn,
  389. head_pfn + nr_pages, page_mt);
  390. /*
  391. * restore the page's migratetype so that it can
  392. * be split into separate migratetype free lists
  393. * later.
  394. */
  395. if (isolate_page)
  396. unset_migratetype_isolate(page, page_mt);
  397. if (ret)
  398. goto failed;
  399. /*
  400. * reset pfn to the head of the free page, so
  401. * that the free page handling code above can split
  402. * the free page to the right migratetype list.
  403. *
  404. * head_pfn is not used here as a hugetlb page order
  405. * can be bigger than MAX_ORDER-1, but after it is
  406. * freed, the free page order is not. Use pfn within
  407. * the range to find the head of the free page.
  408. */
  409. order = 0;
  410. outer_pfn = pfn;
  411. while (!PageBuddy(pfn_to_page(outer_pfn))) {
  412. /* stop if we cannot find the free page */
  413. if (++order >= MAX_ORDER)
  414. goto failed;
  415. outer_pfn &= ~0UL << order;
  416. }
  417. pfn = outer_pfn;
  418. continue;
  419. } else
  420. #endif
  421. goto failed;
  422. }
  423. pfn++;
  424. }
  425. return 0;
  426. failed:
  427. /* restore the original migratetype */
  428. if (!skip_isolation)
  429. unset_migratetype_isolate(pfn_to_page(isolate_pageblock), migratetype);
  430. return -EBUSY;
  431. }
  432. /**
  433. * start_isolate_page_range() - make page-allocation-type of range of pages to
  434. * be MIGRATE_ISOLATE.
  435. * @start_pfn: The lower PFN of the range to be isolated.
  436. * @end_pfn: The upper PFN of the range to be isolated.
  437. * @migratetype: Migrate type to set in error recovery.
  438. * @flags: The following flags are allowed (they can be combined in
  439. * a bit mask)
  440. * MEMORY_OFFLINE - isolate to offline (!allocate) memory
  441. * e.g., skip over PageHWPoison() pages
  442. * and PageOffline() pages.
  443. * REPORT_FAILURE - report details about the failure to
  444. * isolate the range
  445. * @gfp_flags: GFP flags used for migrating pages that sit across the
  446. * range boundaries.
  447. *
  448. * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in
  449. * the range will never be allocated. Any free pages and pages freed in the
  450. * future will not be allocated again. If specified range includes migrate types
  451. * other than MOVABLE or CMA, this will fail with -EBUSY. For isolating all
  452. * pages in the range finally, the caller have to free all pages in the range.
  453. * test_page_isolated() can be used for test it.
  454. *
  455. * The function first tries to isolate the pageblocks at the beginning and end
  456. * of the range, since there might be pages across the range boundaries.
  457. * Afterwards, it isolates the rest of the range.
  458. *
  459. * There is no high level synchronization mechanism that prevents two threads
  460. * from trying to isolate overlapping ranges. If this happens, one thread
  461. * will notice pageblocks in the overlapping range already set to isolate.
  462. * This happens in set_migratetype_isolate, and set_migratetype_isolate
  463. * returns an error. We then clean up by restoring the migration type on
  464. * pageblocks we may have modified and return -EBUSY to caller. This
  465. * prevents two threads from simultaneously working on overlapping ranges.
  466. *
  467. * Please note that there is no strong synchronization with the page allocator
  468. * either. Pages might be freed while their page blocks are marked ISOLATED.
  469. * A call to drain_all_pages() after isolation can flush most of them. However
  470. * in some cases pages might still end up on pcp lists and that would allow
  471. * for their allocation even when they are in fact isolated already. Depending
  472. * on how strong of a guarantee the caller needs, zone_pcp_disable/enable()
  473. * might be used to flush and disable pcplist before isolation and enable after
  474. * unisolation.
  475. *
  476. * Return: 0 on success and -EBUSY if any part of range cannot be isolated.
  477. */
  478. int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
  479. int migratetype, int flags, gfp_t gfp_flags)
  480. {
  481. unsigned long pfn;
  482. struct page *page;
  483. /* isolation is done at page block granularity */
  484. unsigned long isolate_start = pageblock_start_pfn(start_pfn);
  485. unsigned long isolate_end = pageblock_align(end_pfn);
  486. int ret;
  487. bool skip_isolation = false;
  488. /* isolate [isolate_start, isolate_start + pageblock_nr_pages) pageblock */
  489. ret = isolate_single_pageblock(isolate_start, flags, gfp_flags, false,
  490. skip_isolation, migratetype);
  491. if (ret)
  492. return ret;
  493. if (isolate_start == isolate_end - pageblock_nr_pages)
  494. skip_isolation = true;
  495. /* isolate [isolate_end - pageblock_nr_pages, isolate_end) pageblock */
  496. ret = isolate_single_pageblock(isolate_end, flags, gfp_flags, true,
  497. skip_isolation, migratetype);
  498. if (ret) {
  499. unset_migratetype_isolate(pfn_to_page(isolate_start), migratetype);
  500. return ret;
  501. }
  502. /* skip isolated pageblocks at the beginning and end */
  503. for (pfn = isolate_start + pageblock_nr_pages;
  504. pfn < isolate_end - pageblock_nr_pages;
  505. pfn += pageblock_nr_pages) {
  506. page = __first_valid_page(pfn, pageblock_nr_pages);
  507. if (page && set_migratetype_isolate(page, migratetype, flags,
  508. start_pfn, end_pfn)) {
  509. undo_isolate_page_range(isolate_start, pfn, migratetype);
  510. unset_migratetype_isolate(
  511. pfn_to_page(isolate_end - pageblock_nr_pages),
  512. migratetype);
  513. return -EBUSY;
  514. }
  515. }
  516. return 0;
  517. }
  518. /*
  519. * Make isolated pages available again.
  520. */
  521. void undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
  522. int migratetype)
  523. {
  524. unsigned long pfn;
  525. struct page *page;
  526. unsigned long isolate_start = pageblock_start_pfn(start_pfn);
  527. unsigned long isolate_end = pageblock_align(end_pfn);
  528. for (pfn = isolate_start;
  529. pfn < isolate_end;
  530. pfn += pageblock_nr_pages) {
  531. page = __first_valid_page(pfn, pageblock_nr_pages);
  532. if (!page || !is_migrate_isolate_page(page))
  533. continue;
  534. unset_migratetype_isolate(page, migratetype);
  535. }
  536. }
  537. /*
  538. * Test all pages in the range is free(means isolated) or not.
  539. * all pages in [start_pfn...end_pfn) must be in the same zone.
  540. * zone->lock must be held before call this.
  541. *
  542. * Returns the last tested pfn.
  543. */
  544. static unsigned long
  545. __test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn,
  546. int flags)
  547. {
  548. struct page *page;
  549. while (pfn < end_pfn) {
  550. page = pfn_to_page(pfn);
  551. if (PageBuddy(page))
  552. /*
  553. * If the page is on a free list, it has to be on
  554. * the correct MIGRATE_ISOLATE freelist. There is no
  555. * simple way to verify that as VM_BUG_ON(), though.
  556. */
  557. pfn += 1 << buddy_order(page);
  558. else if ((flags & MEMORY_OFFLINE) && PageHWPoison(page))
  559. /* A HWPoisoned page cannot be also PageBuddy */
  560. pfn++;
  561. else if ((flags & MEMORY_OFFLINE) && PageOffline(page) &&
  562. !page_count(page))
  563. /*
  564. * The responsible driver agreed to skip PageOffline()
  565. * pages when offlining memory by dropping its
  566. * reference in MEM_GOING_OFFLINE.
  567. */
  568. pfn++;
  569. else
  570. break;
  571. }
  572. return pfn;
  573. }
  574. /* Caller should ensure that requested range is in a single zone */
  575. int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn,
  576. int isol_flags)
  577. {
  578. unsigned long pfn, flags;
  579. struct page *page;
  580. struct zone *zone;
  581. int ret;
  582. /*
  583. * Note: pageblock_nr_pages != MAX_ORDER. Then, chunks of free pages
  584. * are not aligned to pageblock_nr_pages.
  585. * Then we just check migratetype first.
  586. */
  587. for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
  588. page = __first_valid_page(pfn, pageblock_nr_pages);
  589. if (page && !is_migrate_isolate_page(page))
  590. break;
  591. }
  592. page = __first_valid_page(start_pfn, end_pfn - start_pfn);
  593. if ((pfn < end_pfn) || !page) {
  594. ret = -EBUSY;
  595. goto out;
  596. }
  597. /* Check all pages are free or marked as ISOLATED */
  598. zone = page_zone(page);
  599. spin_lock_irqsave(&zone->lock, flags);
  600. pfn = __test_page_isolated_in_pageblock(start_pfn, end_pfn, isol_flags);
  601. spin_unlock_irqrestore(&zone->lock, flags);
  602. ret = pfn < end_pfn ? -EBUSY : 0;
  603. out:
  604. trace_test_pages_isolated(start_pfn, end_pfn, pfn);
  605. if (pfn < end_pfn)
  606. page_pinner_failure_detect(pfn_to_page(pfn));
  607. return ret;
  608. }