compaction.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  1. /*
  2. * linux/mm/compaction.c
  3. *
  4. * Memory compaction for the reduction of external fragmentation. Note that
  5. * this heavily depends upon page migration to do all the real heavy
  6. * lifting
  7. *
  8. * Copyright IBM Corp. 2007-2010 Mel Gorman <mel@csn.ul.ie>
  9. */
  10. #include <linux/swap.h>
  11. #include <linux/migrate.h>
  12. #include <linux/compaction.h>
  13. #include <linux/mm_inline.h>
  14. #include <linux/backing-dev.h>
  15. #include <linux/sysctl.h>
  16. #include <linux/sysfs.h>
  17. #include <linux/balloon_compaction.h>
  18. #include <linux/page-isolation.h>
  19. #include "internal.h"
  20. #ifdef CONFIG_COMPACTION
  21. static inline void count_compact_event(enum vm_event_item item)
  22. {
  23. count_vm_event(item);
  24. }
  25. static inline void count_compact_events(enum vm_event_item item, long delta)
  26. {
  27. count_vm_events(item, delta);
  28. }
  29. #else
  30. #define count_compact_event(item) do { } while (0)
  31. #define count_compact_events(item, delta) do { } while (0)
  32. #endif
  33. #if defined CONFIG_COMPACTION || defined CONFIG_CMA
  34. #define CREATE_TRACE_POINTS
  35. #include <trace/events/compaction.h>
  36. static unsigned long release_freepages(struct list_head *freelist)
  37. {
  38. struct page *page, *next;
  39. unsigned long count = 0;
  40. list_for_each_entry_safe(page, next, freelist, lru) {
  41. list_del(&page->lru);
  42. __free_page(page);
  43. count++;
  44. }
  45. return count;
  46. }
  47. static void map_pages(struct list_head *list)
  48. {
  49. struct page *page;
  50. list_for_each_entry(page, list, lru) {
  51. arch_alloc_page(page, 0);
  52. kernel_map_pages(page, 1, 1);
  53. }
  54. }
  55. static inline bool migrate_async_suitable(int migratetype)
  56. {
  57. return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
  58. }
  59. #ifdef CONFIG_COMPACTION
  60. /* Returns true if the pageblock should be scanned for pages to isolate. */
  61. static inline bool isolation_suitable(struct compact_control *cc,
  62. struct page *page)
  63. {
  64. if (cc->ignore_skip_hint)
  65. return true;
  66. return !get_pageblock_skip(page);
  67. }
  68. /*
  69. * This function is called to clear all cached information on pageblocks that
  70. * should be skipped for page isolation when the migrate and free page scanner
  71. * meet.
  72. */
  73. static void __reset_isolation_suitable(struct zone *zone)
  74. {
  75. unsigned long start_pfn = zone->zone_start_pfn;
  76. unsigned long end_pfn = zone_end_pfn(zone);
  77. unsigned long pfn;
  78. zone->compact_cached_migrate_pfn = start_pfn;
  79. zone->compact_cached_free_pfn = end_pfn;
  80. zone->compact_blockskip_flush = false;
  81. /* Walk the zone and mark every pageblock as suitable for isolation */
  82. for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
  83. struct page *page;
  84. cond_resched();
  85. if (!pfn_valid(pfn))
  86. continue;
  87. page = pfn_to_page(pfn);
  88. if (zone != page_zone(page))
  89. continue;
  90. clear_pageblock_skip(page);
  91. }
  92. }
  93. void reset_isolation_suitable(pg_data_t *pgdat)
  94. {
  95. int zoneid;
  96. for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
  97. struct zone *zone = &pgdat->node_zones[zoneid];
  98. if (!populated_zone(zone))
  99. continue;
  100. /* Only flush if a full compaction finished recently */
  101. if (zone->compact_blockskip_flush)
  102. __reset_isolation_suitable(zone);
  103. }
  104. }
  105. /*
  106. * If no pages were isolated then mark this pageblock to be skipped in the
  107. * future. The information is later cleared by __reset_isolation_suitable().
  108. */
  109. static void update_pageblock_skip(struct compact_control *cc,
  110. struct page *page, unsigned long nr_isolated,
  111. bool migrate_scanner)
  112. {
  113. struct zone *zone = cc->zone;
  114. if (cc->ignore_skip_hint)
  115. return;
  116. if (!page)
  117. return;
  118. if (!nr_isolated) {
  119. unsigned long pfn = page_to_pfn(page);
  120. set_pageblock_skip(page);
  121. /* Update where compaction should restart */
  122. if (migrate_scanner) {
  123. if (!cc->finished_update_migrate &&
  124. pfn > zone->compact_cached_migrate_pfn)
  125. zone->compact_cached_migrate_pfn = pfn;
  126. } else {
  127. if (!cc->finished_update_free &&
  128. pfn < zone->compact_cached_free_pfn)
  129. zone->compact_cached_free_pfn = pfn;
  130. }
  131. }
  132. }
  133. #else
  134. static inline bool isolation_suitable(struct compact_control *cc,
  135. struct page *page)
  136. {
  137. return true;
  138. }
  139. static void update_pageblock_skip(struct compact_control *cc,
  140. struct page *page, unsigned long nr_isolated,
  141. bool migrate_scanner)
  142. {
  143. }
  144. #endif /* CONFIG_COMPACTION */
  145. static inline bool should_release_lock(spinlock_t *lock)
  146. {
  147. return need_resched() || spin_is_contended(lock);
  148. }
  149. /*
  150. * Compaction requires the taking of some coarse locks that are potentially
  151. * very heavily contended. Check if the process needs to be scheduled or
  152. * if the lock is contended. For async compaction, back out in the event
  153. * if contention is severe. For sync compaction, schedule.
  154. *
  155. * Returns true if the lock is held.
  156. * Returns false if the lock is released and compaction should abort
  157. */
  158. static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags,
  159. bool locked, struct compact_control *cc)
  160. {
  161. if (should_release_lock(lock)) {
  162. if (locked) {
  163. spin_unlock_irqrestore(lock, *flags);
  164. locked = false;
  165. }
  166. /* async aborts if taking too long or contended */
  167. if (!cc->sync) {
  168. cc->contended = true;
  169. return false;
  170. }
  171. cond_resched();
  172. }
  173. if (!locked)
  174. spin_lock_irqsave(lock, *flags);
  175. return true;
  176. }
  177. static inline bool compact_trylock_irqsave(spinlock_t *lock,
  178. unsigned long *flags, struct compact_control *cc)
  179. {
  180. return compact_checklock_irqsave(lock, flags, false, cc);
  181. }
  182. /* Returns true if the page is within a block suitable for migration to */
  183. static bool suitable_migration_target(struct page *page)
  184. {
  185. /* If the page is a large free page, then disallow migration */
  186. if (PageBuddy(page) && page_order(page) >= pageblock_order)
  187. return false;
  188. /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
  189. if (migrate_async_suitable(get_pageblock_migratetype(page)))
  190. return true;
  191. /* Otherwise skip the block */
  192. return false;
  193. }
  194. /*
  195. * Isolate free pages onto a private freelist. If @strict is true, will abort
  196. * returning 0 on any invalid PFNs or non-free pages inside of the pageblock
  197. * (even though it may still end up isolating some pages).
  198. */
  199. static unsigned long isolate_freepages_block(struct compact_control *cc,
  200. unsigned long blockpfn,
  201. unsigned long end_pfn,
  202. struct list_head *freelist,
  203. bool strict)
  204. {
  205. int nr_scanned = 0, total_isolated = 0;
  206. struct page *cursor, *valid_page = NULL;
  207. unsigned long flags;
  208. bool locked = false;
  209. bool checked_pageblock = false;
  210. cursor = pfn_to_page(blockpfn);
  211. /* Isolate free pages. */
  212. for (; blockpfn < end_pfn; blockpfn++, cursor++) {
  213. int isolated, i;
  214. struct page *page = cursor;
  215. nr_scanned++;
  216. if (!pfn_valid_within(blockpfn))
  217. goto isolate_fail;
  218. if (!valid_page)
  219. valid_page = page;
  220. if (!PageBuddy(page))
  221. goto isolate_fail;
  222. /*
  223. * The zone lock must be held to isolate freepages.
  224. * Unfortunately this is a very coarse lock and can be
  225. * heavily contended if there are parallel allocations
  226. * or parallel compactions. For async compaction do not
  227. * spin on the lock and we acquire the lock as late as
  228. * possible.
  229. */
  230. locked = compact_checklock_irqsave(&cc->zone->lock, &flags,
  231. locked, cc);
  232. if (!locked)
  233. break;
  234. /* Recheck this is a suitable migration target under lock */
  235. if (!strict && !checked_pageblock) {
  236. /*
  237. * We need to check suitability of pageblock only once
  238. * and this isolate_freepages_block() is called with
  239. * pageblock range, so just check once is sufficient.
  240. */
  241. checked_pageblock = true;
  242. if (!suitable_migration_target(page))
  243. break;
  244. }
  245. /* Recheck this is a buddy page under lock */
  246. if (!PageBuddy(page))
  247. goto isolate_fail;
  248. /* Found a free page, break it into order-0 pages */
  249. isolated = split_free_page(page);
  250. total_isolated += isolated;
  251. for (i = 0; i < isolated; i++) {
  252. list_add(&page->lru, freelist);
  253. page++;
  254. }
  255. /* If a page was split, advance to the end of it */
  256. if (isolated) {
  257. blockpfn += isolated - 1;
  258. cursor += isolated - 1;
  259. continue;
  260. }
  261. isolate_fail:
  262. if (strict)
  263. break;
  264. else
  265. continue;
  266. }
  267. trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
  268. /*
  269. * If strict isolation is requested by CMA then check that all the
  270. * pages requested were isolated. If there were any failures, 0 is
  271. * returned and CMA will fail.
  272. */
  273. if (strict && blockpfn < end_pfn)
  274. total_isolated = 0;
  275. if (locked)
  276. spin_unlock_irqrestore(&cc->zone->lock, flags);
  277. /* Update the pageblock-skip if the whole pageblock was scanned */
  278. if (blockpfn == end_pfn)
  279. update_pageblock_skip(cc, valid_page, total_isolated, false);
  280. count_compact_events(COMPACTFREE_SCANNED, nr_scanned);
  281. if (total_isolated)
  282. count_compact_events(COMPACTISOLATED, total_isolated);
  283. return total_isolated;
  284. }
  285. /**
  286. * isolate_freepages_range() - isolate free pages.
  287. * @start_pfn: The first PFN to start isolating.
  288. * @end_pfn: The one-past-last PFN.
  289. *
  290. * Non-free pages, invalid PFNs, or zone boundaries within the
  291. * [start_pfn, end_pfn) range are considered errors, cause function to
  292. * undo its actions and return zero.
  293. *
  294. * Otherwise, function returns one-past-the-last PFN of isolated page
  295. * (which may be greater then end_pfn if end fell in a middle of
  296. * a free page).
  297. */
  298. unsigned long
  299. isolate_freepages_range(struct compact_control *cc,
  300. unsigned long start_pfn, unsigned long end_pfn)
  301. {
  302. unsigned long isolated, pfn, block_end_pfn;
  303. LIST_HEAD(freelist);
  304. for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
  305. if (!pfn_valid(pfn) || cc->zone != page_zone(pfn_to_page(pfn)))
  306. break;
  307. /*
  308. * On subsequent iterations ALIGN() is actually not needed,
  309. * but we keep it that we not to complicate the code.
  310. */
  311. block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
  312. block_end_pfn = min(block_end_pfn, end_pfn);
  313. isolated = isolate_freepages_block(cc, pfn, block_end_pfn,
  314. &freelist, true);
  315. /*
  316. * In strict mode, isolate_freepages_block() returns 0 if
  317. * there are any holes in the block (ie. invalid PFNs or
  318. * non-free pages).
  319. */
  320. if (!isolated)
  321. break;
  322. /*
  323. * If we managed to isolate pages, it is always (1 << n) *
  324. * pageblock_nr_pages for some non-negative n. (Max order
  325. * page may span two pageblocks).
  326. */
  327. }
  328. /* split_free_page does not map the pages */
  329. map_pages(&freelist);
  330. if (pfn < end_pfn) {
  331. /* Loop terminated early, cleanup. */
  332. release_freepages(&freelist);
  333. return 0;
  334. }
  335. /* We don't use freelists for anything. */
  336. return pfn;
  337. }
  338. /* Update the number of anon and file isolated pages in the zone */
  339. static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc)
  340. {
  341. struct page *page;
  342. unsigned int count[2] = { 0, };
  343. list_for_each_entry(page, &cc->migratepages, lru)
  344. count[!!page_is_file_cache(page)]++;
  345. /* If locked we can use the interrupt unsafe versions */
  346. if (locked) {
  347. __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
  348. __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
  349. } else {
  350. mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
  351. mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
  352. }
  353. }
  354. /* Similar to reclaim, but different enough that they don't share logic */
  355. static bool too_many_isolated(struct zone *zone)
  356. {
  357. unsigned long active, inactive, isolated;
  358. inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
  359. zone_page_state(zone, NR_INACTIVE_ANON);
  360. active = zone_page_state(zone, NR_ACTIVE_FILE) +
  361. zone_page_state(zone, NR_ACTIVE_ANON);
  362. isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
  363. zone_page_state(zone, NR_ISOLATED_ANON);
  364. return isolated > (inactive + active) / 2;
  365. }
  366. /**
  367. * isolate_migratepages_range() - isolate all migrate-able pages in range.
  368. * @zone: Zone pages are in.
  369. * @cc: Compaction control structure.
  370. * @low_pfn: The first PFN of the range.
  371. * @end_pfn: The one-past-the-last PFN of the range.
  372. * @unevictable: true if it allows to isolate unevictable pages
  373. *
  374. * Isolate all pages that can be migrated from the range specified by
  375. * [low_pfn, end_pfn). Returns zero if there is a fatal signal
  376. * pending), otherwise PFN of the first page that was not scanned
  377. * (which may be both less, equal to or more then end_pfn).
  378. *
  379. * Assumes that cc->migratepages is empty and cc->nr_migratepages is
  380. * zero.
  381. *
  382. * Apart from cc->migratepages and cc->nr_migratetypes this function
  383. * does not modify any cc's fields, in particular it does not modify
  384. * (or read for that matter) cc->migrate_pfn.
  385. */
  386. unsigned long
  387. isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
  388. unsigned long low_pfn, unsigned long end_pfn, bool unevictable)
  389. {
  390. unsigned long last_pageblock_nr = 0, pageblock_nr;
  391. unsigned long nr_scanned = 0, nr_isolated = 0;
  392. struct list_head *migratelist = &cc->migratepages;
  393. struct lruvec *lruvec;
  394. unsigned long flags;
  395. bool locked = false;
  396. struct page *page = NULL, *valid_page = NULL;
  397. bool skipped_async_unsuitable = false;
  398. const isolate_mode_t mode = (!cc->sync ? ISOLATE_ASYNC_MIGRATE : 0) |
  399. (unevictable ? ISOLATE_UNEVICTABLE : 0);
  400. /*
  401. * Ensure that there are not too many pages isolated from the LRU
  402. * list by either parallel reclaimers or compaction. If there are,
  403. * delay for some time until fewer pages are isolated
  404. */
  405. while (unlikely(too_many_isolated(zone))) {
  406. /* async migration should just abort */
  407. if (!cc->sync)
  408. return 0;
  409. congestion_wait(BLK_RW_ASYNC, HZ/10);
  410. if (fatal_signal_pending(current))
  411. return 0;
  412. }
  413. /* Time to isolate some pages for migration */
  414. cond_resched();
  415. for (; low_pfn < end_pfn; low_pfn++) {
  416. /* give a chance to irqs before checking need_resched() */
  417. if (locked && !(low_pfn % SWAP_CLUSTER_MAX)) {
  418. if (should_release_lock(&zone->lru_lock)) {
  419. spin_unlock_irqrestore(&zone->lru_lock, flags);
  420. locked = false;
  421. }
  422. }
  423. /*
  424. * migrate_pfn does not necessarily start aligned to a
  425. * pageblock. Ensure that pfn_valid is called when moving
  426. * into a new MAX_ORDER_NR_PAGES range in case of large
  427. * memory holes within the zone
  428. */
  429. if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
  430. if (!pfn_valid(low_pfn)) {
  431. low_pfn += MAX_ORDER_NR_PAGES - 1;
  432. continue;
  433. }
  434. }
  435. if (!pfn_valid_within(low_pfn))
  436. continue;
  437. nr_scanned++;
  438. /*
  439. * Get the page and ensure the page is within the same zone.
  440. * See the comment in isolate_freepages about overlapping
  441. * nodes. It is deliberate that the new zone lock is not taken
  442. * as memory compaction should not move pages between nodes.
  443. */
  444. page = pfn_to_page(low_pfn);
  445. if (page_zone(page) != zone)
  446. continue;
  447. if (!valid_page)
  448. valid_page = page;
  449. /* If isolation recently failed, do not retry */
  450. pageblock_nr = low_pfn >> pageblock_order;
  451. if (last_pageblock_nr != pageblock_nr) {
  452. int mt;
  453. last_pageblock_nr = pageblock_nr;
  454. if (!isolation_suitable(cc, page))
  455. goto next_pageblock;
  456. /*
  457. * For async migration, also only scan in MOVABLE
  458. * blocks. Async migration is optimistic to see if
  459. * the minimum amount of work satisfies the allocation
  460. */
  461. mt = get_pageblock_migratetype(page);
  462. if (!cc->sync && !migrate_async_suitable(mt)) {
  463. cc->finished_update_migrate = true;
  464. skipped_async_unsuitable = true;
  465. goto next_pageblock;
  466. }
  467. }
  468. /*
  469. * Skip if free. page_order cannot be used without zone->lock
  470. * as nothing prevents parallel allocations or buddy merging.
  471. */
  472. if (PageBuddy(page))
  473. continue;
  474. /*
  475. * Check may be lockless but that's ok as we recheck later.
  476. * It's possible to migrate LRU pages and balloon pages
  477. * Skip any other type of page
  478. */
  479. if (!PageLRU(page)) {
  480. if (unlikely(balloon_page_movable(page))) {
  481. if (locked && balloon_page_isolate(page)) {
  482. /* Successfully isolated */
  483. goto isolate_success;
  484. }
  485. }
  486. continue;
  487. }
  488. /*
  489. * PageLRU is set. lru_lock normally excludes isolation
  490. * splitting and collapsing (collapsing has already happened
  491. * if PageLRU is set) but the lock is not necessarily taken
  492. * here and it is wasteful to take it just to check transhuge.
  493. * Check TransHuge without lock and skip the whole pageblock if
  494. * it's either a transhuge or hugetlbfs page, as calling
  495. * compound_order() without preventing THP from splitting the
  496. * page underneath us may return surprising results.
  497. */
  498. if (PageTransHuge(page)) {
  499. if (!locked)
  500. goto next_pageblock;
  501. low_pfn += (1 << compound_order(page)) - 1;
  502. continue;
  503. }
  504. /*
  505. * Migration will fail if an anonymous page is pinned in memory,
  506. * so avoid taking lru_lock and isolating it unnecessarily in an
  507. * admittedly racy check.
  508. */
  509. if (!page_mapping(page) &&
  510. page_count(page) > page_mapcount(page))
  511. continue;
  512. /* Check if it is ok to still hold the lock */
  513. locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
  514. locked, cc);
  515. if (!locked || fatal_signal_pending(current))
  516. break;
  517. /* Recheck PageLRU and PageTransHuge under lock */
  518. if (!PageLRU(page))
  519. continue;
  520. if (PageTransHuge(page)) {
  521. low_pfn += (1 << compound_order(page)) - 1;
  522. continue;
  523. }
  524. lruvec = mem_cgroup_page_lruvec(page, zone);
  525. /* Try isolate the page */
  526. if (__isolate_lru_page(page, mode) != 0)
  527. continue;
  528. VM_BUG_ON_PAGE(PageTransCompound(page), page);
  529. /* Successfully isolated */
  530. del_page_from_lru_list(page, lruvec, page_lru(page));
  531. isolate_success:
  532. cc->finished_update_migrate = true;
  533. list_add(&page->lru, migratelist);
  534. cc->nr_migratepages++;
  535. nr_isolated++;
  536. /* Avoid isolating too much */
  537. if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
  538. ++low_pfn;
  539. break;
  540. }
  541. continue;
  542. next_pageblock:
  543. low_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages) - 1;
  544. }
  545. acct_isolated(zone, locked, cc);
  546. if (locked)
  547. spin_unlock_irqrestore(&zone->lru_lock, flags);
  548. /*
  549. * Update the pageblock-skip information and cached scanner pfn,
  550. * if the whole pageblock was scanned without isolating any page.
  551. * This is not done when pageblock was skipped due to being unsuitable
  552. * for async compaction, so that eventual sync compaction can try.
  553. */
  554. if (low_pfn == end_pfn && !skipped_async_unsuitable)
  555. update_pageblock_skip(cc, valid_page, nr_isolated, true);
  556. trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
  557. count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned);
  558. if (nr_isolated)
  559. count_compact_events(COMPACTISOLATED, nr_isolated);
  560. return low_pfn;
  561. }
  562. #endif /* CONFIG_COMPACTION || CONFIG_CMA */
  563. #ifdef CONFIG_COMPACTION
  564. /*
  565. * Based on information in the current compact_control, find blocks
  566. * suitable for isolating free pages from and then isolate them.
  567. */
  568. static void isolate_freepages(struct zone *zone,
  569. struct compact_control *cc)
  570. {
  571. struct page *page;
  572. unsigned long high_pfn, low_pfn, pfn, z_end_pfn;
  573. int nr_freepages = cc->nr_freepages;
  574. struct list_head *freelist = &cc->freepages;
  575. /*
  576. * Initialise the free scanner. The starting point is where we last
  577. * successfully isolated from, zone-cached value, or the end of the
  578. * zone when isolating for the first time. We need this aligned to
  579. * the pageblock boundary, because we do pfn -= pageblock_nr_pages
  580. * in the for loop.
  581. * The low boundary is the end of the pageblock the migration scanner
  582. * is using.
  583. */
  584. pfn = cc->free_pfn & ~(pageblock_nr_pages-1);
  585. low_pfn = ALIGN(cc->migrate_pfn + 1, pageblock_nr_pages);
  586. /*
  587. * Take care that if the migration scanner is at the end of the zone
  588. * that the free scanner does not accidentally move to the next zone
  589. * in the next isolation cycle.
  590. */
  591. high_pfn = min(low_pfn, pfn);
  592. z_end_pfn = zone_end_pfn(zone);
  593. /*
  594. * Isolate free pages until enough are available to migrate the
  595. * pages on cc->migratepages. We stop searching if the migrate
  596. * and free page scanners meet or enough free pages are isolated.
  597. */
  598. for (; pfn >= low_pfn && cc->nr_migratepages > nr_freepages;
  599. pfn -= pageblock_nr_pages) {
  600. unsigned long isolated;
  601. unsigned long end_pfn;
  602. /*
  603. * This can iterate a massively long zone without finding any
  604. * suitable migration targets, so periodically check if we need
  605. * to schedule.
  606. */
  607. cond_resched();
  608. if (!pfn_valid(pfn))
  609. continue;
  610. /*
  611. * Check for overlapping nodes/zones. It's possible on some
  612. * configurations to have a setup like
  613. * node0 node1 node0
  614. * i.e. it's possible that all pages within a zones range of
  615. * pages do not belong to a single zone.
  616. */
  617. page = pfn_to_page(pfn);
  618. if (page_zone(page) != zone)
  619. continue;
  620. /* Check the block is suitable for migration */
  621. if (!suitable_migration_target(page))
  622. continue;
  623. /* If isolation recently failed, do not retry */
  624. if (!isolation_suitable(cc, page))
  625. continue;
  626. /* Found a block suitable for isolating free pages from */
  627. isolated = 0;
  628. /*
  629. * Take care when isolating in last pageblock of a zone which
  630. * ends in the middle of a pageblock.
  631. */
  632. end_pfn = min(pfn + pageblock_nr_pages, z_end_pfn);
  633. isolated = isolate_freepages_block(cc, pfn, end_pfn,
  634. freelist, false);
  635. nr_freepages += isolated;
  636. /*
  637. * Record the highest PFN we isolated pages from. When next
  638. * looking for free pages, the search will restart here as
  639. * page migration may have returned some pages to the allocator
  640. */
  641. if (isolated) {
  642. cc->finished_update_free = true;
  643. high_pfn = max(high_pfn, pfn);
  644. }
  645. }
  646. /* split_free_page does not map the pages */
  647. map_pages(freelist);
  648. /*
  649. * If we crossed the migrate scanner, we want to keep it that way
  650. * so that compact_finished() may detect this
  651. */
  652. if (pfn < low_pfn)
  653. cc->free_pfn = max(pfn, zone->zone_start_pfn);
  654. else
  655. cc->free_pfn = high_pfn;
  656. cc->nr_freepages = nr_freepages;
  657. }
  658. /*
  659. * This is a migrate-callback that "allocates" freepages by taking pages
  660. * from the isolated freelists in the block we are migrating to.
  661. */
  662. static struct page *compaction_alloc(struct page *migratepage,
  663. unsigned long data,
  664. int **result)
  665. {
  666. struct compact_control *cc = (struct compact_control *)data;
  667. struct page *freepage;
  668. /* Isolate free pages if necessary */
  669. if (list_empty(&cc->freepages)) {
  670. isolate_freepages(cc->zone, cc);
  671. if (list_empty(&cc->freepages))
  672. return NULL;
  673. }
  674. freepage = list_entry(cc->freepages.next, struct page, lru);
  675. list_del(&freepage->lru);
  676. cc->nr_freepages--;
  677. return freepage;
  678. }
  679. /*
  680. * We cannot control nr_migratepages and nr_freepages fully when migration is
  681. * running as migrate_pages() has no knowledge of compact_control. When
  682. * migration is complete, we count the number of pages on the lists by hand.
  683. */
  684. static void update_nr_listpages(struct compact_control *cc)
  685. {
  686. int nr_migratepages = 0;
  687. int nr_freepages = 0;
  688. struct page *page;
  689. list_for_each_entry(page, &cc->migratepages, lru)
  690. nr_migratepages++;
  691. list_for_each_entry(page, &cc->freepages, lru)
  692. nr_freepages++;
  693. cc->nr_migratepages = nr_migratepages;
  694. cc->nr_freepages = nr_freepages;
  695. }
  696. /* possible outcome of isolate_migratepages */
  697. typedef enum {
  698. ISOLATE_ABORT, /* Abort compaction now */
  699. ISOLATE_NONE, /* No pages isolated, continue scanning */
  700. ISOLATE_SUCCESS, /* Pages isolated, migrate */
  701. } isolate_migrate_t;
  702. /*
  703. * Isolate all pages that can be migrated from the block pointed to by
  704. * the migrate scanner within compact_control.
  705. */
  706. static isolate_migrate_t isolate_migratepages(struct zone *zone,
  707. struct compact_control *cc)
  708. {
  709. unsigned long low_pfn, end_pfn;
  710. /* Do not scan outside zone boundaries */
  711. low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
  712. /* Only scan within a pageblock boundary */
  713. end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages);
  714. /* Do not cross the free scanner or scan within a memory hole */
  715. if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
  716. cc->migrate_pfn = end_pfn;
  717. return ISOLATE_NONE;
  718. }
  719. /* Perform the isolation */
  720. low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn, false);
  721. if (!low_pfn || cc->contended)
  722. return ISOLATE_ABORT;
  723. cc->migrate_pfn = low_pfn;
  724. return ISOLATE_SUCCESS;
  725. }
  726. static int compact_finished(struct zone *zone,
  727. struct compact_control *cc)
  728. {
  729. unsigned int order;
  730. unsigned long watermark;
  731. if (fatal_signal_pending(current))
  732. return COMPACT_PARTIAL;
  733. /* Compaction run completes if the migrate and free scanner meet */
  734. if (cc->free_pfn <= cc->migrate_pfn) {
  735. /* Let the next compaction start anew. */
  736. zone->compact_cached_migrate_pfn = zone->zone_start_pfn;
  737. zone->compact_cached_free_pfn = zone_end_pfn(zone);
  738. /*
  739. * Mark that the PG_migrate_skip information should be cleared
  740. * by kswapd when it goes to sleep. kswapd does not set the
  741. * flag itself as the decision to be clear should be directly
  742. * based on an allocation request.
  743. */
  744. if (!current_is_kswapd())
  745. zone->compact_blockskip_flush = true;
  746. return COMPACT_COMPLETE;
  747. }
  748. /*
  749. * order == -1 is expected when compacting via
  750. * /proc/sys/vm/compact_memory
  751. */
  752. if (cc->order == -1)
  753. return COMPACT_CONTINUE;
  754. /* Compaction run is not finished if the watermark is not met */
  755. watermark = low_wmark_pages(zone);
  756. watermark += (1 << cc->order);
  757. if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
  758. return COMPACT_CONTINUE;
  759. /* Direct compactor: Is a suitable page free? */
  760. for (order = cc->order; order < MAX_ORDER; order++) {
  761. struct free_area *area = &zone->free_area[order];
  762. /* Job done if page is free of the right migratetype */
  763. if (!list_empty(&area->free_list[cc->migratetype]))
  764. return COMPACT_PARTIAL;
  765. /* Job done if allocation would set block type */
  766. if (cc->order >= pageblock_order && area->nr_free)
  767. return COMPACT_PARTIAL;
  768. }
  769. return COMPACT_CONTINUE;
  770. }
  771. /*
  772. * compaction_suitable: Is this suitable to run compaction on this zone now?
  773. * Returns
  774. * COMPACT_SKIPPED - If there are too few free pages for compaction
  775. * COMPACT_PARTIAL - If the allocation would succeed without compaction
  776. * COMPACT_CONTINUE - If compaction should run now
  777. */
  778. unsigned long compaction_suitable(struct zone *zone, int order)
  779. {
  780. int fragindex;
  781. unsigned long watermark;
  782. /*
  783. * order == -1 is expected when compacting via
  784. * /proc/sys/vm/compact_memory
  785. */
  786. if (order == -1)
  787. return COMPACT_CONTINUE;
  788. /*
  789. * Watermarks for order-0 must be met for compaction. Note the 2UL.
  790. * This is because during migration, copies of pages need to be
  791. * allocated and for a short time, the footprint is higher
  792. */
  793. watermark = low_wmark_pages(zone) + (2UL << order);
  794. if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
  795. return COMPACT_SKIPPED;
  796. /*
  797. * fragmentation index determines if allocation failures are due to
  798. * low memory or external fragmentation
  799. *
  800. * index of -1000 implies allocations might succeed depending on
  801. * watermarks
  802. * index towards 0 implies failure is due to lack of memory
  803. * index towards 1000 implies failure is due to fragmentation
  804. *
  805. * Only compact if a failure would be due to fragmentation.
  806. */
  807. fragindex = fragmentation_index(zone, order);
  808. if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
  809. return COMPACT_SKIPPED;
  810. if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
  811. 0, 0))
  812. return COMPACT_PARTIAL;
  813. return COMPACT_CONTINUE;
  814. }
  815. static int compact_zone(struct zone *zone, struct compact_control *cc)
  816. {
  817. int ret;
  818. unsigned long start_pfn = zone->zone_start_pfn;
  819. unsigned long end_pfn = zone_end_pfn(zone);
  820. ret = compaction_suitable(zone, cc->order);
  821. switch (ret) {
  822. case COMPACT_PARTIAL:
  823. case COMPACT_SKIPPED:
  824. /* Compaction is likely to fail */
  825. return ret;
  826. case COMPACT_CONTINUE:
  827. /* Fall through to compaction */
  828. ;
  829. }
  830. /*
  831. * Clear pageblock skip if there were failures recently and compaction
  832. * is about to be retried after being deferred. kswapd does not do
  833. * this reset as it'll reset the cached information when going to sleep.
  834. */
  835. if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
  836. __reset_isolation_suitable(zone);
  837. /*
  838. * Setup to move all movable pages to the end of the zone. Used cached
  839. * information on where the scanners should start but check that it
  840. * is initialised by ensuring the values are within zone boundaries.
  841. */
  842. cc->migrate_pfn = zone->compact_cached_migrate_pfn;
  843. cc->free_pfn = zone->compact_cached_free_pfn;
  844. if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
  845. cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
  846. zone->compact_cached_free_pfn = cc->free_pfn;
  847. }
  848. if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
  849. cc->migrate_pfn = start_pfn;
  850. zone->compact_cached_migrate_pfn = cc->migrate_pfn;
  851. }
  852. trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, cc->free_pfn, end_pfn);
  853. migrate_prep_local();
  854. while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
  855. unsigned long nr_migrate, nr_remaining;
  856. int err;
  857. switch (isolate_migratepages(zone, cc)) {
  858. case ISOLATE_ABORT:
  859. ret = COMPACT_PARTIAL;
  860. putback_movable_pages(&cc->migratepages);
  861. cc->nr_migratepages = 0;
  862. goto out;
  863. case ISOLATE_NONE:
  864. continue;
  865. case ISOLATE_SUCCESS:
  866. ;
  867. }
  868. nr_migrate = cc->nr_migratepages;
  869. err = migrate_pages(&cc->migratepages, compaction_alloc,
  870. (unsigned long)cc,
  871. cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC,
  872. MR_COMPACTION);
  873. update_nr_listpages(cc);
  874. nr_remaining = cc->nr_migratepages;
  875. trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
  876. nr_remaining);
  877. /* Release isolated pages not migrated */
  878. if (err) {
  879. putback_movable_pages(&cc->migratepages);
  880. cc->nr_migratepages = 0;
  881. /*
  882. * migrate_pages() may return -ENOMEM when scanners meet
  883. * and we want compact_finished() to detect it
  884. */
  885. if (err == -ENOMEM && cc->free_pfn > cc->migrate_pfn) {
  886. ret = COMPACT_PARTIAL;
  887. goto out;
  888. }
  889. }
  890. }
  891. out:
  892. /* Release free pages and check accounting */
  893. cc->nr_freepages -= release_freepages(&cc->freepages);
  894. VM_BUG_ON(cc->nr_freepages != 0);
  895. trace_mm_compaction_end(ret);
  896. return ret;
  897. }
  898. static unsigned long compact_zone_order(struct zone *zone,
  899. int order, gfp_t gfp_mask,
  900. bool sync, bool *contended)
  901. {
  902. unsigned long ret;
  903. struct compact_control cc = {
  904. .nr_freepages = 0,
  905. .nr_migratepages = 0,
  906. .order = order,
  907. .migratetype = allocflags_to_migratetype(gfp_mask),
  908. .zone = zone,
  909. .sync = sync,
  910. };
  911. INIT_LIST_HEAD(&cc.freepages);
  912. INIT_LIST_HEAD(&cc.migratepages);
  913. ret = compact_zone(zone, &cc);
  914. VM_BUG_ON(!list_empty(&cc.freepages));
  915. VM_BUG_ON(!list_empty(&cc.migratepages));
  916. *contended = cc.contended;
  917. return ret;
  918. }
  919. int sysctl_extfrag_threshold = 500;
  920. /**
  921. * try_to_compact_pages - Direct compact to satisfy a high-order allocation
  922. * @zonelist: The zonelist used for the current allocation
  923. * @order: The order of the current allocation
  924. * @gfp_mask: The GFP mask of the current allocation
  925. * @nodemask: The allowed nodes to allocate from
  926. * @sync: Whether migration is synchronous or not
  927. * @contended: Return value that is true if compaction was aborted due to lock contention
  928. * @page: Optionally capture a free page of the requested order during compaction
  929. *
  930. * This is the main entry point for direct page compaction.
  931. */
  932. unsigned long try_to_compact_pages(struct zonelist *zonelist,
  933. int order, gfp_t gfp_mask, nodemask_t *nodemask,
  934. bool sync, bool *contended)
  935. {
  936. enum zone_type high_zoneidx = gfp_zone(gfp_mask);
  937. int may_enter_fs = gfp_mask & __GFP_FS;
  938. int may_perform_io = gfp_mask & __GFP_IO;
  939. struct zoneref *z;
  940. struct zone *zone;
  941. int rc = COMPACT_SKIPPED;
  942. int alloc_flags = 0;
  943. /* Check if the GFP flags allow compaction */
  944. if (!order || !may_enter_fs || !may_perform_io)
  945. return rc;
  946. count_compact_event(COMPACTSTALL);
  947. #ifdef CONFIG_CMA
  948. if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
  949. alloc_flags |= ALLOC_CMA;
  950. #endif
  951. /* Compact each zone in the list */
  952. for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
  953. nodemask) {
  954. int status;
  955. status = compact_zone_order(zone, order, gfp_mask, sync,
  956. contended);
  957. rc = max(status, rc);
  958. /* If a normal allocation would succeed, stop compacting */
  959. if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
  960. alloc_flags))
  961. break;
  962. }
  963. return rc;
  964. }
  965. /* Compact all zones within a node */
  966. static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
  967. {
  968. int zoneid;
  969. struct zone *zone;
  970. for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
  971. zone = &pgdat->node_zones[zoneid];
  972. if (!populated_zone(zone))
  973. continue;
  974. cc->nr_freepages = 0;
  975. cc->nr_migratepages = 0;
  976. cc->zone = zone;
  977. INIT_LIST_HEAD(&cc->freepages);
  978. INIT_LIST_HEAD(&cc->migratepages);
  979. if (cc->order == -1 || !compaction_deferred(zone, cc->order))
  980. compact_zone(zone, cc);
  981. if (cc->order > 0) {
  982. if (zone_watermark_ok(zone, cc->order,
  983. low_wmark_pages(zone), 0, 0))
  984. compaction_defer_reset(zone, cc->order, false);
  985. /* Currently async compaction is never deferred. */
  986. else if (cc->sync)
  987. defer_compaction(zone, cc->order);
  988. }
  989. VM_BUG_ON(!list_empty(&cc->freepages));
  990. VM_BUG_ON(!list_empty(&cc->migratepages));
  991. }
  992. }
  993. void compact_pgdat(pg_data_t *pgdat, int order)
  994. {
  995. struct compact_control cc = {
  996. .order = order,
  997. .sync = false,
  998. };
  999. if (!order)
  1000. return;
  1001. __compact_pgdat(pgdat, &cc);
  1002. }
  1003. static void compact_node(int nid)
  1004. {
  1005. struct compact_control cc = {
  1006. .order = -1,
  1007. .sync = true,
  1008. .ignore_skip_hint = true,
  1009. };
  1010. __compact_pgdat(NODE_DATA(nid), &cc);
  1011. }
  1012. /* Compact all nodes in the system */
  1013. static void compact_nodes(void)
  1014. {
  1015. int nid;
  1016. /* Flush pending updates to the LRU lists */
  1017. lru_add_drain_all();
  1018. for_each_online_node(nid)
  1019. compact_node(nid);
  1020. }
  1021. /* The written value is actually unused, all memory is compacted */
  1022. int sysctl_compact_memory;
  1023. /* This is the entry point for compacting all nodes via /proc/sys/vm */
  1024. int sysctl_compaction_handler(struct ctl_table *table, int write,
  1025. void __user *buffer, size_t *length, loff_t *ppos)
  1026. {
  1027. if (write)
  1028. compact_nodes();
  1029. return 0;
  1030. }
  1031. int sysctl_extfrag_handler(struct ctl_table *table, int write,
  1032. void __user *buffer, size_t *length, loff_t *ppos)
  1033. {
  1034. proc_dointvec_minmax(table, write, buffer, length, ppos);
  1035. return 0;
  1036. }
  1037. #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
  1038. static ssize_t sysfs_compact_node(struct device *dev,
  1039. struct device_attribute *attr,
  1040. const char *buf, size_t count)
  1041. {
  1042. int nid = dev->id;
  1043. if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
  1044. /* Flush pending updates to the LRU lists */
  1045. lru_add_drain_all();
  1046. compact_node(nid);
  1047. }
  1048. return count;
  1049. }
  1050. static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
  1051. int compaction_register_node(struct node *node)
  1052. {
  1053. return device_create_file(&node->dev, &dev_attr_compact);
  1054. }
  1055. void compaction_unregister_node(struct node *node)
  1056. {
  1057. return device_remove_file(&node->dev, &dev_attr_compact);
  1058. }
  1059. #endif /* CONFIG_SYSFS && CONFIG_NUMA */
  1060. #endif /* CONFIG_COMPACTION */