page_isolation.c 20 KB

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