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. static void compact_capture_page(struct compact_control *cc)
  183. {
  184. unsigned long flags;
  185. int mtype, mtype_low, mtype_high;
  186. if (!cc->page || *cc->page)
  187. return;
  188. /*
  189. * For MIGRATE_MOVABLE allocations we capture a suitable page ASAP
  190. * regardless of the migratetype of the freelist is is captured from.
  191. * This is fine because the order for a high-order MIGRATE_MOVABLE
  192. * allocation is typically at least a pageblock size and overall
  193. * fragmentation is not impaired. Other allocation types must
  194. * capture pages from their own migratelist because otherwise they
  195. * could pollute other pageblocks like MIGRATE_MOVABLE with
  196. * difficult to move pages and making fragmentation worse overall.
  197. */
  198. if (cc->migratetype == MIGRATE_MOVABLE) {
  199. mtype_low = 0;
  200. mtype_high = MIGRATE_PCPTYPES;
  201. } else {
  202. mtype_low = cc->migratetype;
  203. mtype_high = cc->migratetype + 1;
  204. }
  205. /* Speculatively examine the free lists without zone lock */
  206. for (mtype = mtype_low; mtype < mtype_high; mtype++) {
  207. int order;
  208. for (order = cc->order; order < MAX_ORDER; order++) {
  209. struct page *page;
  210. struct free_area *area;
  211. area = &(cc->zone->free_area[order]);
  212. if (list_empty(&area->free_list[mtype]))
  213. continue;
  214. /* Take the lock and attempt capture of the page */
  215. if (!compact_trylock_irqsave(&cc->zone->lock, &flags, cc))
  216. return;
  217. if (!list_empty(&area->free_list[mtype])) {
  218. page = list_entry(area->free_list[mtype].next,
  219. struct page, lru);
  220. if (capture_free_page(page, cc->order, mtype)) {
  221. spin_unlock_irqrestore(&cc->zone->lock,
  222. flags);
  223. *cc->page = page;
  224. return;
  225. }
  226. }
  227. spin_unlock_irqrestore(&cc->zone->lock, flags);
  228. }
  229. }
  230. }
  231. /*
  232. * Isolate free pages onto a private freelist. Caller must hold zone->lock.
  233. * If @strict is true, will abort returning 0 on any invalid PFNs or non-free
  234. * pages inside of the pageblock (even though it may still end up isolating
  235. * some pages).
  236. */
  237. static unsigned long isolate_freepages_block(struct compact_control *cc,
  238. unsigned long blockpfn,
  239. unsigned long end_pfn,
  240. struct list_head *freelist,
  241. bool strict)
  242. {
  243. int nr_scanned = 0, total_isolated = 0;
  244. struct page *cursor, *valid_page = NULL;
  245. unsigned long nr_strict_required = end_pfn - blockpfn;
  246. unsigned long flags;
  247. bool locked = false;
  248. cursor = pfn_to_page(blockpfn);
  249. /* Isolate free pages. */
  250. for (; blockpfn < end_pfn; blockpfn++, cursor++) {
  251. int isolated, i;
  252. struct page *page = cursor;
  253. nr_scanned++;
  254. if (!pfn_valid_within(blockpfn))
  255. continue;
  256. if (!valid_page)
  257. valid_page = page;
  258. if (!PageBuddy(page))
  259. continue;
  260. /*
  261. * The zone lock must be held to isolate freepages.
  262. * Unfortunately this is a very coarse lock and can be
  263. * heavily contended if there are parallel allocations
  264. * or parallel compactions. For async compaction do not
  265. * spin on the lock and we acquire the lock as late as
  266. * possible.
  267. */
  268. locked = compact_checklock_irqsave(&cc->zone->lock, &flags,
  269. locked, cc);
  270. if (!locked)
  271. break;
  272. /* Recheck this is a suitable migration target under lock */
  273. if (!strict && !suitable_migration_target(page))
  274. break;
  275. /* Recheck this is a buddy page under lock */
  276. if (!PageBuddy(page))
  277. continue;
  278. /* Found a free page, break it into order-0 pages */
  279. isolated = split_free_page(page);
  280. if (!isolated && strict)
  281. break;
  282. total_isolated += isolated;
  283. for (i = 0; i < isolated; i++) {
  284. list_add(&page->lru, freelist);
  285. page++;
  286. }
  287. /* If a page was split, advance to the end of it */
  288. if (isolated) {
  289. blockpfn += isolated - 1;
  290. cursor += isolated - 1;
  291. }
  292. }
  293. trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
  294. /*
  295. * If strict isolation is requested by CMA then check that all the
  296. * pages requested were isolated. If there were any failures, 0 is
  297. * returned and CMA will fail.
  298. */
  299. if (strict && nr_strict_required > total_isolated)
  300. total_isolated = 0;
  301. if (locked)
  302. spin_unlock_irqrestore(&cc->zone->lock, flags);
  303. /* Update the pageblock-skip if the whole pageblock was scanned */
  304. if (blockpfn == end_pfn)
  305. update_pageblock_skip(cc, valid_page, total_isolated, false);
  306. return total_isolated;
  307. }
  308. /**
  309. * isolate_freepages_range() - isolate free pages.
  310. * @start_pfn: The first PFN to start isolating.
  311. * @end_pfn: The one-past-last PFN.
  312. *
  313. * Non-free pages, invalid PFNs, or zone boundaries within the
  314. * [start_pfn, end_pfn) range are considered errors, cause function to
  315. * undo its actions and return zero.
  316. *
  317. * Otherwise, function returns one-past-the-last PFN of isolated page
  318. * (which may be greater then end_pfn if end fell in a middle of
  319. * a free page).
  320. */
  321. unsigned long
  322. isolate_freepages_range(struct compact_control *cc,
  323. unsigned long start_pfn, unsigned long end_pfn)
  324. {
  325. unsigned long isolated, pfn, block_end_pfn;
  326. LIST_HEAD(freelist);
  327. for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
  328. if (!pfn_valid(pfn) || cc->zone != page_zone(pfn_to_page(pfn)))
  329. break;
  330. /*
  331. * On subsequent iterations ALIGN() is actually not needed,
  332. * but we keep it that we not to complicate the code.
  333. */
  334. block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
  335. block_end_pfn = min(block_end_pfn, end_pfn);
  336. isolated = isolate_freepages_block(cc, pfn, block_end_pfn,
  337. &freelist, true);
  338. /*
  339. * In strict mode, isolate_freepages_block() returns 0 if
  340. * there are any holes in the block (ie. invalid PFNs or
  341. * non-free pages).
  342. */
  343. if (!isolated)
  344. break;
  345. /*
  346. * If we managed to isolate pages, it is always (1 << n) *
  347. * pageblock_nr_pages for some non-negative n. (Max order
  348. * page may span two pageblocks).
  349. */
  350. }
  351. /* split_free_page does not map the pages */
  352. map_pages(&freelist);
  353. if (pfn < end_pfn) {
  354. /* Loop terminated early, cleanup. */
  355. release_freepages(&freelist);
  356. return 0;
  357. }
  358. /* We don't use freelists for anything. */
  359. return pfn;
  360. }
  361. /* Update the number of anon and file isolated pages in the zone */
  362. static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc)
  363. {
  364. struct page *page;
  365. unsigned int count[2] = { 0, };
  366. list_for_each_entry(page, &cc->migratepages, lru)
  367. count[!!page_is_file_cache(page)]++;
  368. /* If locked we can use the interrupt unsafe versions */
  369. if (locked) {
  370. __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
  371. __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
  372. } else {
  373. mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
  374. mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
  375. }
  376. }
  377. /* Similar to reclaim, but different enough that they don't share logic */
  378. static bool too_many_isolated(struct zone *zone)
  379. {
  380. unsigned long active, inactive, isolated;
  381. inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
  382. zone_page_state(zone, NR_INACTIVE_ANON);
  383. active = zone_page_state(zone, NR_ACTIVE_FILE) +
  384. zone_page_state(zone, NR_ACTIVE_ANON);
  385. isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
  386. zone_page_state(zone, NR_ISOLATED_ANON);
  387. return isolated > (inactive + active) / 2;
  388. }
  389. /**
  390. * isolate_migratepages_range() - isolate all migrate-able pages in range.
  391. * @zone: Zone pages are in.
  392. * @cc: Compaction control structure.
  393. * @low_pfn: The first PFN of the range.
  394. * @end_pfn: The one-past-the-last PFN of the range.
  395. * @unevictable: true if it allows to isolate unevictable pages
  396. *
  397. * Isolate all pages that can be migrated from the range specified by
  398. * [low_pfn, end_pfn). Returns zero if there is a fatal signal
  399. * pending), otherwise PFN of the first page that was not scanned
  400. * (which may be both less, equal to or more then end_pfn).
  401. *
  402. * Assumes that cc->migratepages is empty and cc->nr_migratepages is
  403. * zero.
  404. *
  405. * Apart from cc->migratepages and cc->nr_migratetypes this function
  406. * does not modify any cc's fields, in particular it does not modify
  407. * (or read for that matter) cc->migrate_pfn.
  408. */
  409. unsigned long
  410. isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
  411. unsigned long low_pfn, unsigned long end_pfn, bool unevictable)
  412. {
  413. unsigned long last_pageblock_nr = 0, pageblock_nr;
  414. unsigned long nr_scanned = 0, nr_isolated = 0;
  415. struct list_head *migratelist = &cc->migratepages;
  416. isolate_mode_t mode = 0;
  417. struct lruvec *lruvec;
  418. unsigned long flags;
  419. bool locked = false;
  420. struct page *page = NULL, *valid_page = NULL;
  421. /*
  422. * Ensure that there are not too many pages isolated from the LRU
  423. * list by either parallel reclaimers or compaction. If there are,
  424. * delay for some time until fewer pages are isolated
  425. */
  426. while (unlikely(too_many_isolated(zone))) {
  427. /* async migration should just abort */
  428. if (!cc->sync)
  429. return 0;
  430. congestion_wait(BLK_RW_ASYNC, HZ/10);
  431. if (fatal_signal_pending(current))
  432. return 0;
  433. }
  434. /* Time to isolate some pages for migration */
  435. cond_resched();
  436. for (; low_pfn < end_pfn; low_pfn++) {
  437. /* give a chance to irqs before checking need_resched() */
  438. if (locked && !((low_pfn+1) % SWAP_CLUSTER_MAX)) {
  439. if (should_release_lock(&zone->lru_lock)) {
  440. spin_unlock_irqrestore(&zone->lru_lock, flags);
  441. locked = false;
  442. }
  443. }
  444. /*
  445. * migrate_pfn does not necessarily start aligned to a
  446. * pageblock. Ensure that pfn_valid is called when moving
  447. * into a new MAX_ORDER_NR_PAGES range in case of large
  448. * memory holes within the zone
  449. */
  450. if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
  451. if (!pfn_valid(low_pfn)) {
  452. low_pfn += MAX_ORDER_NR_PAGES - 1;
  453. continue;
  454. }
  455. }
  456. if (!pfn_valid_within(low_pfn))
  457. continue;
  458. nr_scanned++;
  459. /*
  460. * Get the page and ensure the page is within the same zone.
  461. * See the comment in isolate_freepages about overlapping
  462. * nodes. It is deliberate that the new zone lock is not taken
  463. * as memory compaction should not move pages between nodes.
  464. */
  465. page = pfn_to_page(low_pfn);
  466. if (page_zone(page) != zone)
  467. continue;
  468. if (!valid_page)
  469. valid_page = page;
  470. /* If isolation recently failed, do not retry */
  471. pageblock_nr = low_pfn >> pageblock_order;
  472. if (!isolation_suitable(cc, page))
  473. goto next_pageblock;
  474. /* Skip if free */
  475. if (PageBuddy(page))
  476. continue;
  477. /*
  478. * For async migration, also only scan in MOVABLE blocks. Async
  479. * migration is optimistic to see if the minimum amount of work
  480. * satisfies the allocation
  481. */
  482. if (!cc->sync && last_pageblock_nr != pageblock_nr &&
  483. !migrate_async_suitable(get_pageblock_migratetype(page))) {
  484. cc->finished_update_migrate = true;
  485. goto next_pageblock;
  486. }
  487. /*
  488. * Check may be lockless but that's ok as we recheck later.
  489. * It's possible to migrate LRU pages and balloon pages
  490. * Skip any other type of page
  491. */
  492. if (!PageLRU(page)) {
  493. if (unlikely(balloon_page_movable(page))) {
  494. if (locked && balloon_page_isolate(page)) {
  495. /* Successfully isolated */
  496. cc->finished_update_migrate = true;
  497. list_add(&page->lru, migratelist);
  498. cc->nr_migratepages++;
  499. nr_isolated++;
  500. goto check_compact_cluster;
  501. }
  502. }
  503. continue;
  504. }
  505. /*
  506. * PageLRU is set. lru_lock normally excludes isolation
  507. * splitting and collapsing (collapsing has already happened
  508. * if PageLRU is set) but the lock is not necessarily taken
  509. * here and it is wasteful to take it just to check transhuge.
  510. * Check TransHuge without lock and skip the whole pageblock if
  511. * it's either a transhuge or hugetlbfs page, as calling
  512. * compound_order() without preventing THP from splitting the
  513. * page underneath us may return surprising results.
  514. */
  515. if (PageTransHuge(page)) {
  516. if (!locked)
  517. goto next_pageblock;
  518. low_pfn += (1 << compound_order(page)) - 1;
  519. continue;
  520. }
  521. /* Check if it is ok to still hold the lock */
  522. locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
  523. locked, cc);
  524. if (!locked || fatal_signal_pending(current))
  525. break;
  526. /* Recheck PageLRU and PageTransHuge under lock */
  527. if (!PageLRU(page))
  528. continue;
  529. if (PageTransHuge(page)) {
  530. low_pfn += (1 << compound_order(page)) - 1;
  531. continue;
  532. }
  533. if (!cc->sync)
  534. mode |= ISOLATE_ASYNC_MIGRATE;
  535. if (unevictable)
  536. mode |= ISOLATE_UNEVICTABLE;
  537. lruvec = mem_cgroup_page_lruvec(page, zone);
  538. /* Try isolate the page */
  539. if (__isolate_lru_page(page, mode) != 0)
  540. continue;
  541. VM_BUG_ON(PageTransCompound(page));
  542. /* Successfully isolated */
  543. cc->finished_update_migrate = true;
  544. del_page_from_lru_list(page, lruvec, page_lru(page));
  545. list_add(&page->lru, migratelist);
  546. cc->nr_migratepages++;
  547. nr_isolated++;
  548. check_compact_cluster:
  549. /* Avoid isolating too much */
  550. if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
  551. ++low_pfn;
  552. break;
  553. }
  554. continue;
  555. next_pageblock:
  556. low_pfn += pageblock_nr_pages;
  557. low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
  558. last_pageblock_nr = pageblock_nr;
  559. }
  560. acct_isolated(zone, locked, cc);
  561. if (locked)
  562. spin_unlock_irqrestore(&zone->lru_lock, flags);
  563. /* Update the pageblock-skip if the whole pageblock was scanned */
  564. if (low_pfn == end_pfn)
  565. update_pageblock_skip(cc, valid_page, nr_isolated, true);
  566. trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
  567. return low_pfn;
  568. }
  569. #endif /* CONFIG_COMPACTION || CONFIG_CMA */
  570. #ifdef CONFIG_COMPACTION
  571. /*
  572. * Based on information in the current compact_control, find blocks
  573. * suitable for isolating free pages from and then isolate them.
  574. */
  575. static void isolate_freepages(struct zone *zone,
  576. struct compact_control *cc)
  577. {
  578. struct page *page;
  579. unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
  580. int nr_freepages = cc->nr_freepages;
  581. struct list_head *freelist = &cc->freepages;
  582. /*
  583. * Initialise the free scanner. The starting point is where we last
  584. * scanned from (or the end of the zone if starting). The low point
  585. * is the end of the pageblock the migration scanner is using.
  586. */
  587. pfn = cc->free_pfn;
  588. low_pfn = cc->migrate_pfn + pageblock_nr_pages;
  589. /*
  590. * Take care that if the migration scanner is at the end of the zone
  591. * that the free scanner does not accidentally move to the next zone
  592. * in the next isolation cycle.
  593. */
  594. high_pfn = min(low_pfn, pfn);
  595. zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
  596. /*
  597. * Isolate free pages until enough are available to migrate the
  598. * pages on cc->migratepages. We stop searching if the migrate
  599. * and free page scanners meet or enough free pages are isolated.
  600. */
  601. for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
  602. pfn -= pageblock_nr_pages) {
  603. unsigned long isolated;
  604. if (!pfn_valid(pfn))
  605. continue;
  606. /*
  607. * Check for overlapping nodes/zones. It's possible on some
  608. * configurations to have a setup like
  609. * node0 node1 node0
  610. * i.e. it's possible that all pages within a zones range of
  611. * pages do not belong to a single zone.
  612. */
  613. page = pfn_to_page(pfn);
  614. if (page_zone(page) != zone)
  615. continue;
  616. /* Check the block is suitable for migration */
  617. if (!suitable_migration_target(page))
  618. continue;
  619. /* If isolation recently failed, do not retry */
  620. if (!isolation_suitable(cc, page))
  621. continue;
  622. /* Found a block suitable for isolating free pages from */
  623. isolated = 0;
  624. /*
  625. * As pfn may not start aligned, pfn+pageblock_nr_page
  626. * may cross a MAX_ORDER_NR_PAGES boundary and miss
  627. * a pfn_valid check. Ensure isolate_freepages_block()
  628. * only scans within a pageblock
  629. */
  630. end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
  631. end_pfn = min(end_pfn, zone_end_pfn);
  632. isolated = isolate_freepages_block(cc, pfn, end_pfn,
  633. freelist, false);
  634. nr_freepages += isolated;
  635. /*
  636. * Record the highest PFN we isolated pages from. When next
  637. * looking for free pages, the search will restart here as
  638. * page migration may have returned some pages to the allocator
  639. */
  640. if (isolated) {
  641. cc->finished_update_free = true;
  642. high_pfn = max(high_pfn, pfn);
  643. }
  644. }
  645. /* split_free_page does not map the pages */
  646. map_pages(freelist);
  647. cc->free_pfn = high_pfn;
  648. cc->nr_freepages = nr_freepages;
  649. }
  650. /*
  651. * This is a migrate-callback that "allocates" freepages by taking pages
  652. * from the isolated freelists in the block we are migrating to.
  653. */
  654. static struct page *compaction_alloc(struct page *migratepage,
  655. unsigned long data,
  656. int **result)
  657. {
  658. struct compact_control *cc = (struct compact_control *)data;
  659. struct page *freepage;
  660. /* Isolate free pages if necessary */
  661. if (list_empty(&cc->freepages)) {
  662. isolate_freepages(cc->zone, cc);
  663. if (list_empty(&cc->freepages))
  664. return NULL;
  665. }
  666. freepage = list_entry(cc->freepages.next, struct page, lru);
  667. list_del(&freepage->lru);
  668. cc->nr_freepages--;
  669. return freepage;
  670. }
  671. /*
  672. * We cannot control nr_migratepages and nr_freepages fully when migration is
  673. * running as migrate_pages() has no knowledge of compact_control. When
  674. * migration is complete, we count the number of pages on the lists by hand.
  675. */
  676. static void update_nr_listpages(struct compact_control *cc)
  677. {
  678. int nr_migratepages = 0;
  679. int nr_freepages = 0;
  680. struct page *page;
  681. list_for_each_entry(page, &cc->migratepages, lru)
  682. nr_migratepages++;
  683. list_for_each_entry(page, &cc->freepages, lru)
  684. nr_freepages++;
  685. cc->nr_migratepages = nr_migratepages;
  686. cc->nr_freepages = nr_freepages;
  687. }
  688. /* possible outcome of isolate_migratepages */
  689. typedef enum {
  690. ISOLATE_ABORT, /* Abort compaction now */
  691. ISOLATE_NONE, /* No pages isolated, continue scanning */
  692. ISOLATE_SUCCESS, /* Pages isolated, migrate */
  693. } isolate_migrate_t;
  694. /*
  695. * Isolate all pages that can be migrated from the block pointed to by
  696. * the migrate scanner within compact_control.
  697. */
  698. static isolate_migrate_t isolate_migratepages(struct zone *zone,
  699. struct compact_control *cc)
  700. {
  701. unsigned long low_pfn, end_pfn;
  702. /* Do not scan outside zone boundaries */
  703. low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
  704. /* Only scan within a pageblock boundary */
  705. end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
  706. /* Do not cross the free scanner or scan within a memory hole */
  707. if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
  708. cc->migrate_pfn = end_pfn;
  709. return ISOLATE_NONE;
  710. }
  711. /* Perform the isolation */
  712. low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn, false);
  713. if (!low_pfn || cc->contended)
  714. return ISOLATE_ABORT;
  715. cc->migrate_pfn = low_pfn;
  716. return ISOLATE_SUCCESS;
  717. }
  718. static int compact_finished(struct zone *zone,
  719. struct compact_control *cc)
  720. {
  721. unsigned long watermark;
  722. if (fatal_signal_pending(current))
  723. return COMPACT_PARTIAL;
  724. /* Compaction run completes if the migrate and free scanner meet */
  725. if (cc->free_pfn <= cc->migrate_pfn) {
  726. /*
  727. * Mark that the PG_migrate_skip information should be cleared
  728. * by kswapd when it goes to sleep. kswapd does not set the
  729. * flag itself as the decision to be clear should be directly
  730. * based on an allocation request.
  731. */
  732. if (!current_is_kswapd())
  733. zone->compact_blockskip_flush = true;
  734. return COMPACT_COMPLETE;
  735. }
  736. /*
  737. * order == -1 is expected when compacting via
  738. * /proc/sys/vm/compact_memory
  739. */
  740. if (cc->order == -1)
  741. return COMPACT_CONTINUE;
  742. /* Compaction run is not finished if the watermark is not met */
  743. watermark = low_wmark_pages(zone);
  744. watermark += (1 << cc->order);
  745. if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
  746. return COMPACT_CONTINUE;
  747. /* Direct compactor: Is a suitable page free? */
  748. if (cc->page) {
  749. /* Was a suitable page captured? */
  750. if (*cc->page)
  751. return COMPACT_PARTIAL;
  752. } else {
  753. unsigned int order;
  754. for (order = cc->order; order < MAX_ORDER; order++) {
  755. struct free_area *area = &zone->free_area[cc->order];
  756. /* Job done if page is free of the right migratetype */
  757. if (!list_empty(&area->free_list[cc->migratetype]))
  758. return COMPACT_PARTIAL;
  759. /* Job done if allocation would set block type */
  760. if (cc->order >= pageblock_order && area->nr_free)
  761. return COMPACT_PARTIAL;
  762. }
  763. }
  764. return COMPACT_CONTINUE;
  765. }
  766. /*
  767. * compaction_suitable: Is this suitable to run compaction on this zone now?
  768. * Returns
  769. * COMPACT_SKIPPED - If there are too few free pages for compaction
  770. * COMPACT_PARTIAL - If the allocation would succeed without compaction
  771. * COMPACT_CONTINUE - If compaction should run now
  772. */
  773. unsigned long compaction_suitable(struct zone *zone, int order)
  774. {
  775. int fragindex;
  776. unsigned long watermark;
  777. /*
  778. * order == -1 is expected when compacting via
  779. * /proc/sys/vm/compact_memory
  780. */
  781. if (order == -1)
  782. return COMPACT_CONTINUE;
  783. /*
  784. * Watermarks for order-0 must be met for compaction. Note the 2UL.
  785. * This is because during migration, copies of pages need to be
  786. * allocated and for a short time, the footprint is higher
  787. */
  788. watermark = low_wmark_pages(zone) + (2UL << order);
  789. if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
  790. return COMPACT_SKIPPED;
  791. /*
  792. * fragmentation index determines if allocation failures are due to
  793. * low memory or external fragmentation
  794. *
  795. * index of -1000 implies allocations might succeed depending on
  796. * watermarks
  797. * index towards 0 implies failure is due to lack of memory
  798. * index towards 1000 implies failure is due to fragmentation
  799. *
  800. * Only compact if a failure would be due to fragmentation.
  801. */
  802. fragindex = fragmentation_index(zone, order);
  803. if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
  804. return COMPACT_SKIPPED;
  805. if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
  806. 0, 0))
  807. return COMPACT_PARTIAL;
  808. return COMPACT_CONTINUE;
  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 */