compaction.c 32 KB

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