compaction.c 34 KB

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