compaction.c 32 KB

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