page_isolation.c 20 KB

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