compaction.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  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. if (!pfn_valid(pfn))
  577. continue;
  578. /*
  579. * Check for overlapping nodes/zones. It's possible on some
  580. * configurations to have a setup like
  581. * node0 node1 node0
  582. * i.e. it's possible that all pages within a zones range of
  583. * pages do not belong to a single zone.
  584. */
  585. page = pfn_to_page(pfn);
  586. if (page_zone(page) != zone)
  587. continue;
  588. /* Check the block is suitable for migration */
  589. if (!suitable_migration_target(page))
  590. continue;
  591. /* If isolation recently failed, do not retry */
  592. if (!isolation_suitable(cc, page))
  593. continue;
  594. /* Found a block suitable for isolating free pages from */
  595. isolated = 0;
  596. /*
  597. * As pfn may not start aligned, pfn+pageblock_nr_page
  598. * may cross a MAX_ORDER_NR_PAGES boundary and miss
  599. * a pfn_valid check. Ensure isolate_freepages_block()
  600. * only scans within a pageblock
  601. */
  602. end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
  603. end_pfn = min(end_pfn, z_end_pfn);
  604. isolated = isolate_freepages_block(cc, pfn, end_pfn,
  605. freelist, false);
  606. nr_freepages += isolated;
  607. /*
  608. * Record the highest PFN we isolated pages from. When next
  609. * looking for free pages, the search will restart here as
  610. * page migration may have returned some pages to the allocator
  611. */
  612. if (isolated) {
  613. cc->finished_update_free = true;
  614. high_pfn = max(high_pfn, pfn);
  615. }
  616. }
  617. /* split_free_page does not map the pages */
  618. map_pages(freelist);
  619. cc->free_pfn = high_pfn;
  620. cc->nr_freepages = nr_freepages;
  621. }
  622. /*
  623. * This is a migrate-callback that "allocates" freepages by taking pages
  624. * from the isolated freelists in the block we are migrating to.
  625. */
  626. static struct page *compaction_alloc(struct page *migratepage,
  627. unsigned long data,
  628. int **result)
  629. {
  630. struct compact_control *cc = (struct compact_control *)data;
  631. struct page *freepage;
  632. /* Isolate free pages if necessary */
  633. if (list_empty(&cc->freepages)) {
  634. isolate_freepages(cc->zone, cc);
  635. if (list_empty(&cc->freepages))
  636. return NULL;
  637. }
  638. freepage = list_entry(cc->freepages.next, struct page, lru);
  639. list_del(&freepage->lru);
  640. cc->nr_freepages--;
  641. return freepage;
  642. }
  643. /*
  644. * We cannot control nr_migratepages and nr_freepages fully when migration is
  645. * running as migrate_pages() has no knowledge of compact_control. When
  646. * migration is complete, we count the number of pages on the lists by hand.
  647. */
  648. static void update_nr_listpages(struct compact_control *cc)
  649. {
  650. int nr_migratepages = 0;
  651. int nr_freepages = 0;
  652. struct page *page;
  653. list_for_each_entry(page, &cc->migratepages, lru)
  654. nr_migratepages++;
  655. list_for_each_entry(page, &cc->freepages, lru)
  656. nr_freepages++;
  657. cc->nr_migratepages = nr_migratepages;
  658. cc->nr_freepages = nr_freepages;
  659. }
  660. /* possible outcome of isolate_migratepages */
  661. typedef enum {
  662. ISOLATE_ABORT, /* Abort compaction now */
  663. ISOLATE_NONE, /* No pages isolated, continue scanning */
  664. ISOLATE_SUCCESS, /* Pages isolated, migrate */
  665. } isolate_migrate_t;
  666. /*
  667. * Isolate all pages that can be migrated from the block pointed to by
  668. * the migrate scanner within compact_control.
  669. */
  670. static isolate_migrate_t isolate_migratepages(struct zone *zone,
  671. struct compact_control *cc)
  672. {
  673. unsigned long low_pfn, end_pfn;
  674. /* Do not scan outside zone boundaries */
  675. low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
  676. /* Only scan within a pageblock boundary */
  677. end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages);
  678. /* Do not cross the free scanner or scan within a memory hole */
  679. if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
  680. cc->migrate_pfn = end_pfn;
  681. return ISOLATE_NONE;
  682. }
  683. /* Perform the isolation */
  684. low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn, false);
  685. if (!low_pfn || cc->contended)
  686. return ISOLATE_ABORT;
  687. cc->migrate_pfn = low_pfn;
  688. return ISOLATE_SUCCESS;
  689. }
  690. static int compact_finished(struct zone *zone,
  691. struct compact_control *cc)
  692. {
  693. unsigned int order;
  694. unsigned long watermark;
  695. if (fatal_signal_pending(current))
  696. return COMPACT_PARTIAL;
  697. /* Compaction run completes if the migrate and free scanner meet */
  698. if (cc->free_pfn <= cc->migrate_pfn) {
  699. /*
  700. * Mark that the PG_migrate_skip information should be cleared
  701. * by kswapd when it goes to sleep. kswapd does not set the
  702. * flag itself as the decision to be clear should be directly
  703. * based on an allocation request.
  704. */
  705. if (!current_is_kswapd())
  706. zone->compact_blockskip_flush = true;
  707. return COMPACT_COMPLETE;
  708. }
  709. /*
  710. * order == -1 is expected when compacting via
  711. * /proc/sys/vm/compact_memory
  712. */
  713. if (cc->order == -1)
  714. return COMPACT_CONTINUE;
  715. /* Compaction run is not finished if the watermark is not met */
  716. watermark = low_wmark_pages(zone);
  717. watermark += (1 << cc->order);
  718. if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
  719. return COMPACT_CONTINUE;
  720. /* Direct compactor: Is a suitable page free? */
  721. for (order = cc->order; order < MAX_ORDER; order++) {
  722. struct free_area *area = &zone->free_area[order];
  723. /* Job done if page is free of the right migratetype */
  724. if (!list_empty(&area->free_list[cc->migratetype]))
  725. return COMPACT_PARTIAL;
  726. /* Job done if allocation would set block type */
  727. if (cc->order >= pageblock_order && area->nr_free)
  728. return COMPACT_PARTIAL;
  729. }
  730. return COMPACT_CONTINUE;
  731. }
  732. /*
  733. * compaction_suitable: Is this suitable to run compaction on this zone now?
  734. * Returns
  735. * COMPACT_SKIPPED - If there are too few free pages for compaction
  736. * COMPACT_PARTIAL - If the allocation would succeed without compaction
  737. * COMPACT_CONTINUE - If compaction should run now
  738. */
  739. unsigned long compaction_suitable(struct zone *zone, int order)
  740. {
  741. int fragindex;
  742. unsigned long watermark;
  743. /*
  744. * order == -1 is expected when compacting via
  745. * /proc/sys/vm/compact_memory
  746. */
  747. if (order == -1)
  748. return COMPACT_CONTINUE;
  749. /*
  750. * Watermarks for order-0 must be met for compaction. Note the 2UL.
  751. * This is because during migration, copies of pages need to be
  752. * allocated and for a short time, the footprint is higher
  753. */
  754. watermark = low_wmark_pages(zone) + (2UL << order);
  755. if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
  756. return COMPACT_SKIPPED;
  757. /*
  758. * fragmentation index determines if allocation failures are due to
  759. * low memory or external fragmentation
  760. *
  761. * index of -1000 implies allocations might succeed depending on
  762. * watermarks
  763. * index towards 0 implies failure is due to lack of memory
  764. * index towards 1000 implies failure is due to fragmentation
  765. *
  766. * Only compact if a failure would be due to fragmentation.
  767. */
  768. fragindex = fragmentation_index(zone, order);
  769. if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
  770. return COMPACT_SKIPPED;
  771. if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
  772. 0, 0))
  773. return COMPACT_PARTIAL;
  774. return COMPACT_CONTINUE;
  775. }
  776. static int compact_zone(struct zone *zone, struct compact_control *cc)
  777. {
  778. int ret;
  779. unsigned long start_pfn = zone->zone_start_pfn;
  780. unsigned long end_pfn = zone_end_pfn(zone);
  781. ret = compaction_suitable(zone, cc->order);
  782. switch (ret) {
  783. case COMPACT_PARTIAL:
  784. case COMPACT_SKIPPED:
  785. /* Compaction is likely to fail */
  786. return ret;
  787. case COMPACT_CONTINUE:
  788. /* Fall through to compaction */
  789. ;
  790. }
  791. /*
  792. * Setup to move all movable pages to the end of the zone. Used cached
  793. * information on where the scanners should start but check that it
  794. * is initialised by ensuring the values are within zone boundaries.
  795. */
  796. cc->migrate_pfn = zone->compact_cached_migrate_pfn;
  797. cc->free_pfn = zone->compact_cached_free_pfn;
  798. if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
  799. cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
  800. zone->compact_cached_free_pfn = cc->free_pfn;
  801. }
  802. if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
  803. cc->migrate_pfn = start_pfn;
  804. zone->compact_cached_migrate_pfn = cc->migrate_pfn;
  805. }
  806. /*
  807. * Clear pageblock skip if there were failures recently and compaction
  808. * is about to be retried after being deferred. kswapd does not do
  809. * this reset as it'll reset the cached information when going to sleep.
  810. */
  811. if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
  812. __reset_isolation_suitable(zone);
  813. migrate_prep_local();
  814. while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
  815. unsigned long nr_migrate, nr_remaining;
  816. int err;
  817. switch (isolate_migratepages(zone, cc)) {
  818. case ISOLATE_ABORT:
  819. ret = COMPACT_PARTIAL;
  820. putback_movable_pages(&cc->migratepages);
  821. cc->nr_migratepages = 0;
  822. goto out;
  823. case ISOLATE_NONE:
  824. continue;
  825. case ISOLATE_SUCCESS:
  826. ;
  827. }
  828. nr_migrate = cc->nr_migratepages;
  829. err = migrate_pages(&cc->migratepages, compaction_alloc,
  830. (unsigned long)cc,
  831. cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC,
  832. MR_COMPACTION);
  833. update_nr_listpages(cc);
  834. nr_remaining = cc->nr_migratepages;
  835. trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
  836. nr_remaining);
  837. /* Release isolated pages not migrated */
  838. if (err) {
  839. putback_movable_pages(&cc->migratepages);
  840. cc->nr_migratepages = 0;
  841. if (err == -ENOMEM) {
  842. ret = COMPACT_PARTIAL;
  843. goto out;
  844. }
  845. }
  846. }
  847. out:
  848. /* Release free pages and check accounting */
  849. cc->nr_freepages -= release_freepages(&cc->freepages);
  850. VM_BUG_ON(cc->nr_freepages != 0);
  851. return ret;
  852. }
  853. static unsigned long compact_zone_order(struct zone *zone,
  854. int order, gfp_t gfp_mask,
  855. bool sync, bool *contended)
  856. {
  857. unsigned long ret;
  858. struct compact_control cc = {
  859. .nr_freepages = 0,
  860. .nr_migratepages = 0,
  861. .order = order,
  862. .migratetype = allocflags_to_migratetype(gfp_mask),
  863. .zone = zone,
  864. .sync = sync,
  865. };
  866. INIT_LIST_HEAD(&cc.freepages);
  867. INIT_LIST_HEAD(&cc.migratepages);
  868. ret = compact_zone(zone, &cc);
  869. VM_BUG_ON(!list_empty(&cc.freepages));
  870. VM_BUG_ON(!list_empty(&cc.migratepages));
  871. *contended = cc.contended;
  872. return ret;
  873. }
  874. int sysctl_extfrag_threshold = 500;
  875. /**
  876. * try_to_compact_pages - Direct compact to satisfy a high-order allocation
  877. * @zonelist: The zonelist used for the current allocation
  878. * @order: The order of the current allocation
  879. * @gfp_mask: The GFP mask of the current allocation
  880. * @nodemask: The allowed nodes to allocate from
  881. * @sync: Whether migration is synchronous or not
  882. * @contended: Return value that is true if compaction was aborted due to lock contention
  883. * @page: Optionally capture a free page of the requested order during compaction
  884. *
  885. * This is the main entry point for direct page compaction.
  886. */
  887. unsigned long try_to_compact_pages(struct zonelist *zonelist,
  888. int order, gfp_t gfp_mask, nodemask_t *nodemask,
  889. bool sync, bool *contended)
  890. {
  891. enum zone_type high_zoneidx = gfp_zone(gfp_mask);
  892. int may_enter_fs = gfp_mask & __GFP_FS;
  893. int may_perform_io = gfp_mask & __GFP_IO;
  894. struct zoneref *z;
  895. struct zone *zone;
  896. int rc = COMPACT_SKIPPED;
  897. int alloc_flags = 0;
  898. /* Check if the GFP flags allow compaction */
  899. if (!order || !may_enter_fs || !may_perform_io)
  900. return rc;
  901. count_compact_event(COMPACTSTALL);
  902. #ifdef CONFIG_CMA
  903. if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
  904. alloc_flags |= ALLOC_CMA;
  905. #endif
  906. /* Compact each zone in the list */
  907. for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
  908. nodemask) {
  909. int status;
  910. status = compact_zone_order(zone, order, gfp_mask, sync,
  911. contended);
  912. rc = max(status, rc);
  913. /* If a normal allocation would succeed, stop compacting */
  914. if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
  915. alloc_flags))
  916. break;
  917. }
  918. return rc;
  919. }
  920. /* Compact all zones within a node */
  921. static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
  922. {
  923. int zoneid;
  924. struct zone *zone;
  925. for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
  926. zone = &pgdat->node_zones[zoneid];
  927. if (!populated_zone(zone))
  928. continue;
  929. cc->nr_freepages = 0;
  930. cc->nr_migratepages = 0;
  931. cc->zone = zone;
  932. INIT_LIST_HEAD(&cc->freepages);
  933. INIT_LIST_HEAD(&cc->migratepages);
  934. if (cc->order == -1 || !compaction_deferred(zone, cc->order))
  935. compact_zone(zone, cc);
  936. if (cc->order > 0) {
  937. int ok = zone_watermark_ok(zone, cc->order,
  938. low_wmark_pages(zone), 0, 0);
  939. if (ok && cc->order >= zone->compact_order_failed)
  940. zone->compact_order_failed = cc->order + 1;
  941. /* Currently async compaction is never deferred. */
  942. else if (!ok && cc->sync)
  943. defer_compaction(zone, cc->order);
  944. }
  945. VM_BUG_ON(!list_empty(&cc->freepages));
  946. VM_BUG_ON(!list_empty(&cc->migratepages));
  947. }
  948. }
  949. void compact_pgdat(pg_data_t *pgdat, int order)
  950. {
  951. struct compact_control cc = {
  952. .order = order,
  953. .sync = false,
  954. };
  955. __compact_pgdat(pgdat, &cc);
  956. }
  957. static void compact_node(int nid)
  958. {
  959. struct compact_control cc = {
  960. .order = -1,
  961. .sync = true,
  962. };
  963. __compact_pgdat(NODE_DATA(nid), &cc);
  964. }
  965. /* Compact all nodes in the system */
  966. static void compact_nodes(void)
  967. {
  968. int nid;
  969. /* Flush pending updates to the LRU lists */
  970. lru_add_drain_all();
  971. for_each_online_node(nid)
  972. compact_node(nid);
  973. }
  974. /* The written value is actually unused, all memory is compacted */
  975. int sysctl_compact_memory;
  976. /* This is the entry point for compacting all nodes via /proc/sys/vm */
  977. int sysctl_compaction_handler(struct ctl_table *table, int write,
  978. void __user *buffer, size_t *length, loff_t *ppos)
  979. {
  980. if (write)
  981. compact_nodes();
  982. return 0;
  983. }
  984. int sysctl_extfrag_handler(struct ctl_table *table, int write,
  985. void __user *buffer, size_t *length, loff_t *ppos)
  986. {
  987. proc_dointvec_minmax(table, write, buffer, length, ppos);
  988. return 0;
  989. }
  990. #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
  991. ssize_t sysfs_compact_node(struct device *dev,
  992. struct device_attribute *attr,
  993. const char *buf, size_t count)
  994. {
  995. int nid = dev->id;
  996. if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
  997. /* Flush pending updates to the LRU lists */
  998. lru_add_drain_all();
  999. compact_node(nid);
  1000. }
  1001. return count;
  1002. }
  1003. static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
  1004. int compaction_register_node(struct node *node)
  1005. {
  1006. return device_create_file(&node->dev, &dev_attr_compact);
  1007. }
  1008. void compaction_unregister_node(struct node *node)
  1009. {
  1010. return device_remove_file(&node->dev, &dev_attr_compact);
  1011. }
  1012. #endif /* CONFIG_SYSFS && CONFIG_NUMA */
  1013. #endif /* CONFIG_COMPACTION */