compaction.c 34 KB

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