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