compaction.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  1. /*
  2. * linux/mm/compaction.c
  3. *
  4. * Memory compaction for the reduction of external fragmentation. Note that
  5. * this heavily depends upon page migration to do all the real heavy
  6. * lifting
  7. *
  8. * Copyright IBM Corp. 2007-2010 Mel Gorman <mel@csn.ul.ie>
  9. */
  10. #include <linux/swap.h>
  11. #include <linux/migrate.h>
  12. #include <linux/compaction.h>
  13. #include <linux/mm_inline.h>
  14. #include <linux/backing-dev.h>
  15. #include <linux/sysctl.h>
  16. #include <linux/sysfs.h>
  17. #include <linux/balloon_compaction.h>
  18. #include <linux/page-isolation.h>
  19. #include "internal.h"
  20. #ifdef CONFIG_COMPACTION
  21. static inline void count_compact_event(enum vm_event_item item)
  22. {
  23. count_vm_event(item);
  24. }
  25. static inline void count_compact_events(enum vm_event_item item, long delta)
  26. {
  27. count_vm_events(item, delta);
  28. }
  29. #else
  30. #define count_compact_event(item) do { } while (0)
  31. #define count_compact_events(item, delta) do { } while (0)
  32. #endif
  33. #if defined CONFIG_COMPACTION || defined CONFIG_CMA
  34. #define CREATE_TRACE_POINTS
  35. #include <trace/events/compaction.h>
  36. static unsigned long release_freepages(struct list_head *freelist)
  37. {
  38. struct page *page, *next;
  39. unsigned long count = 0;
  40. list_for_each_entry_safe(page, next, freelist, lru) {
  41. list_del(&page->lru);
  42. __free_page(page);
  43. count++;
  44. }
  45. return count;
  46. }
  47. static void map_pages(struct list_head *list)
  48. {
  49. struct page *page;
  50. list_for_each_entry(page, list, lru) {
  51. arch_alloc_page(page, 0);
  52. kernel_map_pages(page, 1, 1);
  53. }
  54. }
  55. static inline bool migrate_async_suitable(int migratetype)
  56. {
  57. return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
  58. }
  59. /*
  60. * Check that the whole (or subset of) a pageblock given by the interval of
  61. * [start_pfn, end_pfn) is valid and within the same zone, before scanning it
  62. * with the migration of free compaction scanner. The scanners then need to
  63. * use only pfn_valid_within() check for arches that allow holes within
  64. * pageblocks.
  65. *
  66. * Return struct page pointer of start_pfn, or NULL if checks were not passed.
  67. *
  68. * It's possible on some configurations to have a setup like node0 node1 node0
  69. * i.e. it's possible that all pages within a zones range of pages do not
  70. * belong to a single zone. We assume that a border between node0 and node1
  71. * can occur within a single pageblock, but not a node0 node1 node0
  72. * interleaving within a single pageblock. It is therefore sufficient to check
  73. * the first and last page of a pageblock and avoid checking each individual
  74. * page in a pageblock.
  75. */
  76. static struct page *pageblock_pfn_to_page(unsigned long start_pfn,
  77. unsigned long end_pfn, struct zone *zone)
  78. {
  79. struct page *start_page;
  80. struct page *end_page;
  81. /* end_pfn is one past the range we are checking */
  82. end_pfn--;
  83. if (!pfn_valid(start_pfn) || !pfn_valid(end_pfn))
  84. return NULL;
  85. start_page = pfn_to_page(start_pfn);
  86. if (page_zone(start_page) != zone)
  87. return NULL;
  88. end_page = pfn_to_page(end_pfn);
  89. /* This gives a shorter code than deriving page_zone(end_page) */
  90. if (page_zone_id(start_page) != page_zone_id(end_page))
  91. return NULL;
  92. return start_page;
  93. }
  94. #ifdef CONFIG_COMPACTION
  95. /* Returns true if the pageblock should be scanned for pages to isolate. */
  96. static inline bool isolation_suitable(struct compact_control *cc,
  97. struct page *page)
  98. {
  99. if (cc->ignore_skip_hint)
  100. return true;
  101. return !get_pageblock_skip(page);
  102. }
  103. /*
  104. * This function is called to clear all cached information on pageblocks that
  105. * should be skipped for page isolation when the migrate and free page scanner
  106. * meet.
  107. */
  108. static void __reset_isolation_suitable(struct zone *zone)
  109. {
  110. unsigned long start_pfn = zone->zone_start_pfn;
  111. unsigned long end_pfn = zone_end_pfn(zone);
  112. unsigned long pfn;
  113. zone->compact_cached_migrate_pfn[0] = start_pfn;
  114. zone->compact_cached_migrate_pfn[1] = start_pfn;
  115. zone->compact_cached_free_pfn = end_pfn;
  116. zone->compact_blockskip_flush = false;
  117. /* Walk the zone and mark every pageblock as suitable for isolation */
  118. for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
  119. struct page *page;
  120. cond_resched();
  121. if (!pfn_valid(pfn))
  122. continue;
  123. page = pfn_to_page(pfn);
  124. if (zone != page_zone(page))
  125. continue;
  126. clear_pageblock_skip(page);
  127. }
  128. }
  129. void reset_isolation_suitable(pg_data_t *pgdat)
  130. {
  131. int zoneid;
  132. for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
  133. struct zone *zone = &pgdat->node_zones[zoneid];
  134. if (!populated_zone(zone))
  135. continue;
  136. /* Only flush if a full compaction finished recently */
  137. if (zone->compact_blockskip_flush)
  138. __reset_isolation_suitable(zone);
  139. }
  140. }
  141. /*
  142. * If no pages were isolated then mark this pageblock to be skipped in the
  143. * future. The information is later cleared by __reset_isolation_suitable().
  144. */
  145. static void update_pageblock_skip(struct compact_control *cc,
  146. struct page *page, unsigned long nr_isolated,
  147. bool migrate_scanner)
  148. {
  149. struct zone *zone = cc->zone;
  150. unsigned long pfn;
  151. if (cc->ignore_skip_hint)
  152. return;
  153. if (!page)
  154. return;
  155. if (nr_isolated)
  156. return;
  157. set_pageblock_skip(page);
  158. pfn = page_to_pfn(page);
  159. /* Update where async and sync compaction should restart */
  160. if (migrate_scanner) {
  161. if (cc->finished_update_migrate)
  162. return;
  163. if (pfn > zone->compact_cached_migrate_pfn[0])
  164. zone->compact_cached_migrate_pfn[0] = pfn;
  165. if (cc->mode != MIGRATE_ASYNC &&
  166. pfn > zone->compact_cached_migrate_pfn[1])
  167. zone->compact_cached_migrate_pfn[1] = pfn;
  168. } else {
  169. if (cc->finished_update_free)
  170. return;
  171. if (pfn < zone->compact_cached_free_pfn)
  172. zone->compact_cached_free_pfn = pfn;
  173. }
  174. }
  175. #else
  176. static inline bool isolation_suitable(struct compact_control *cc,
  177. struct page *page)
  178. {
  179. return true;
  180. }
  181. static void update_pageblock_skip(struct compact_control *cc,
  182. struct page *page, unsigned long nr_isolated,
  183. bool migrate_scanner)
  184. {
  185. }
  186. #endif /* CONFIG_COMPACTION */
  187. /*
  188. * Compaction requires the taking of some coarse locks that are potentially
  189. * very heavily contended. For async compaction, back out if the lock cannot
  190. * be taken immediately. For sync compaction, spin on the lock if needed.
  191. *
  192. * Returns true if the lock is held
  193. * Returns false if the lock is not held and compaction should abort
  194. */
  195. static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags,
  196. struct compact_control *cc)
  197. {
  198. if (cc->mode == MIGRATE_ASYNC) {
  199. if (!spin_trylock_irqsave(lock, *flags)) {
  200. cc->contended = COMPACT_CONTENDED_LOCK;
  201. return false;
  202. }
  203. } else {
  204. spin_lock_irqsave(lock, *flags);
  205. }
  206. return true;
  207. }
  208. /*
  209. * Compaction requires the taking of some coarse locks that are potentially
  210. * very heavily contended. The lock should be periodically unlocked to avoid
  211. * having disabled IRQs for a long time, even when there is nobody waiting on
  212. * the lock. It might also be that allowing the IRQs will result in
  213. * need_resched() becoming true. If scheduling is needed, async compaction
  214. * aborts. Sync compaction schedules.
  215. * Either compaction type will also abort if a fatal signal is pending.
  216. * In either case if the lock was locked, it is dropped and not regained.
  217. *
  218. * Returns true if compaction should abort due to fatal signal pending, or
  219. * async compaction due to need_resched()
  220. * Returns false when compaction can continue (sync compaction might have
  221. * scheduled)
  222. */
  223. static bool compact_unlock_should_abort(spinlock_t *lock,
  224. unsigned long flags, bool *locked, struct compact_control *cc)
  225. {
  226. if (*locked) {
  227. spin_unlock_irqrestore(lock, flags);
  228. *locked = false;
  229. }
  230. if (fatal_signal_pending(current)) {
  231. cc->contended = COMPACT_CONTENDED_SCHED;
  232. return true;
  233. }
  234. if (need_resched()) {
  235. if (cc->mode == MIGRATE_ASYNC) {
  236. cc->contended = COMPACT_CONTENDED_SCHED;
  237. return true;
  238. }
  239. cond_resched();
  240. }
  241. return false;
  242. }
  243. /*
  244. * Aside from avoiding lock contention, compaction also periodically checks
  245. * need_resched() and either schedules in sync compaction or aborts async
  246. * compaction. This is similar to what compact_unlock_should_abort() does, but
  247. * is used where no lock is concerned.
  248. *
  249. * Returns false when no scheduling was needed, or sync compaction scheduled.
  250. * Returns true when async compaction should abort.
  251. */
  252. static inline bool compact_should_abort(struct compact_control *cc)
  253. {
  254. /* async compaction aborts if contended */
  255. if (need_resched()) {
  256. if (cc->mode == MIGRATE_ASYNC) {
  257. cc->contended = COMPACT_CONTENDED_SCHED;
  258. return true;
  259. }
  260. cond_resched();
  261. }
  262. return false;
  263. }
  264. /* Returns true if the page is within a block suitable for migration to */
  265. static bool suitable_migration_target(struct page *page)
  266. {
  267. /* If the page is a large free page, then disallow migration */
  268. if (PageBuddy(page)) {
  269. /*
  270. * We are checking page_order without zone->lock taken. But
  271. * the only small danger is that we skip a potentially suitable
  272. * pageblock, so it's not worth to check order for valid range.
  273. */
  274. if (page_order_unsafe(page) >= pageblock_order)
  275. return false;
  276. }
  277. /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
  278. if (migrate_async_suitable(get_pageblock_migratetype(page)))
  279. return true;
  280. /* Otherwise skip the block */
  281. return false;
  282. }
  283. /*
  284. * Isolate free pages onto a private freelist. If @strict is true, will abort
  285. * returning 0 on any invalid PFNs or non-free pages inside of the pageblock
  286. * (even though it may still end up isolating some pages).
  287. */
  288. static unsigned long isolate_freepages_block(struct compact_control *cc,
  289. unsigned long *start_pfn,
  290. unsigned long end_pfn,
  291. struct list_head *freelist,
  292. bool strict)
  293. {
  294. int nr_scanned = 0, total_isolated = 0;
  295. struct page *cursor, *valid_page = NULL;
  296. unsigned long flags = 0;
  297. bool locked = false;
  298. unsigned long blockpfn = *start_pfn;
  299. cursor = pfn_to_page(blockpfn);
  300. /* Isolate free pages. */
  301. for (; blockpfn < end_pfn; blockpfn++, cursor++) {
  302. int isolated, i;
  303. struct page *page = cursor;
  304. /*
  305. * Periodically drop the lock (if held) regardless of its
  306. * contention, to give chance to IRQs. Abort if fatal signal
  307. * pending or async compaction detects need_resched()
  308. */
  309. if (!(blockpfn % SWAP_CLUSTER_MAX)
  310. && compact_unlock_should_abort(&cc->zone->lock, flags,
  311. &locked, cc))
  312. break;
  313. nr_scanned++;
  314. if (!pfn_valid_within(blockpfn))
  315. goto isolate_fail;
  316. if (!valid_page)
  317. valid_page = page;
  318. if (!PageBuddy(page))
  319. goto isolate_fail;
  320. /*
  321. * If we already hold the lock, we can skip some rechecking.
  322. * Note that if we hold the lock now, checked_pageblock was
  323. * already set in some previous iteration (or strict is true),
  324. * so it is correct to skip the suitable migration target
  325. * recheck as well.
  326. */
  327. if (!locked) {
  328. /*
  329. * The zone lock must be held to isolate freepages.
  330. * Unfortunately this is a very coarse lock and can be
  331. * heavily contended if there are parallel allocations
  332. * or parallel compactions. For async compaction do not
  333. * spin on the lock and we acquire the lock as late as
  334. * possible.
  335. */
  336. locked = compact_trylock_irqsave(&cc->zone->lock,
  337. &flags, cc);
  338. if (!locked)
  339. break;
  340. /* Recheck this is a buddy page under lock */
  341. if (!PageBuddy(page))
  342. goto isolate_fail;
  343. }
  344. /* Found a free page, break it into order-0 pages */
  345. isolated = split_free_page(page);
  346. total_isolated += isolated;
  347. for (i = 0; i < isolated; i++) {
  348. list_add(&page->lru, freelist);
  349. page++;
  350. }
  351. /* If a page was split, advance to the end of it */
  352. if (isolated) {
  353. blockpfn += isolated - 1;
  354. cursor += isolated - 1;
  355. continue;
  356. }
  357. isolate_fail:
  358. if (strict)
  359. break;
  360. else
  361. continue;
  362. }
  363. /* Record how far we have got within the block */
  364. *start_pfn = blockpfn;
  365. trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
  366. /*
  367. * If strict isolation is requested by CMA then check that all the
  368. * pages requested were isolated. If there were any failures, 0 is
  369. * returned and CMA will fail.
  370. */
  371. if (strict && blockpfn < end_pfn)
  372. total_isolated = 0;
  373. if (locked)
  374. spin_unlock_irqrestore(&cc->zone->lock, flags);
  375. /* Update the pageblock-skip if the whole pageblock was scanned */
  376. if (blockpfn == end_pfn)
  377. update_pageblock_skip(cc, valid_page, total_isolated, false);
  378. count_compact_events(COMPACTFREE_SCANNED, nr_scanned);
  379. if (total_isolated)
  380. count_compact_events(COMPACTISOLATED, total_isolated);
  381. return total_isolated;
  382. }
  383. /**
  384. * isolate_freepages_range() - isolate free pages.
  385. * @start_pfn: The first PFN to start isolating.
  386. * @end_pfn: The one-past-last PFN.
  387. *
  388. * Non-free pages, invalid PFNs, or zone boundaries within the
  389. * [start_pfn, end_pfn) range are considered errors, cause function to
  390. * undo its actions and return zero.
  391. *
  392. * Otherwise, function returns one-past-the-last PFN of isolated page
  393. * (which may be greater then end_pfn if end fell in a middle of
  394. * a free page).
  395. */
  396. unsigned long
  397. isolate_freepages_range(struct compact_control *cc,
  398. unsigned long start_pfn, unsigned long end_pfn)
  399. {
  400. unsigned long isolated, pfn, block_end_pfn;
  401. LIST_HEAD(freelist);
  402. pfn = start_pfn;
  403. block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
  404. for (; pfn < end_pfn; pfn += isolated,
  405. block_end_pfn += pageblock_nr_pages) {
  406. /* Protect pfn from changing by isolate_freepages_block */
  407. unsigned long isolate_start_pfn = pfn;
  408. block_end_pfn = min(block_end_pfn, end_pfn);
  409. /*
  410. * pfn could pass the block_end_pfn if isolated freepage
  411. * is more than pageblock order. In this case, we adjust
  412. * scanning range to right one.
  413. */
  414. if (pfn >= block_end_pfn) {
  415. block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
  416. block_end_pfn = min(block_end_pfn, end_pfn);
  417. }
  418. if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
  419. break;
  420. isolated = isolate_freepages_block(cc, &isolate_start_pfn,
  421. block_end_pfn, &freelist, true);
  422. /*
  423. * In strict mode, isolate_freepages_block() returns 0 if
  424. * there are any holes in the block (ie. invalid PFNs or
  425. * non-free pages).
  426. */
  427. if (!isolated)
  428. break;
  429. /*
  430. * If we managed to isolate pages, it is always (1 << n) *
  431. * pageblock_nr_pages for some non-negative n. (Max order
  432. * page may span two pageblocks).
  433. */
  434. }
  435. /* split_free_page does not map the pages */
  436. map_pages(&freelist);
  437. if (pfn < end_pfn) {
  438. /* Loop terminated early, cleanup. */
  439. release_freepages(&freelist);
  440. return 0;
  441. }
  442. /* We don't use freelists for anything. */
  443. return pfn;
  444. }
  445. /* Update the number of anon and file isolated pages in the zone */
  446. static void acct_isolated(struct zone *zone, struct compact_control *cc)
  447. {
  448. struct page *page;
  449. unsigned int count[2] = { 0, };
  450. if (list_empty(&cc->migratepages))
  451. return;
  452. list_for_each_entry(page, &cc->migratepages, lru)
  453. count[!!page_is_file_cache(page)]++;
  454. mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
  455. mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
  456. }
  457. /* Similar to reclaim, but different enough that they don't share logic */
  458. static bool too_many_isolated(struct zone *zone)
  459. {
  460. unsigned long active, inactive, isolated;
  461. inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
  462. zone_page_state(zone, NR_INACTIVE_ANON);
  463. active = zone_page_state(zone, NR_ACTIVE_FILE) +
  464. zone_page_state(zone, NR_ACTIVE_ANON);
  465. isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
  466. zone_page_state(zone, NR_ISOLATED_ANON);
  467. return isolated > (inactive + active) / 2;
  468. }
  469. /**
  470. * isolate_migratepages_block() - isolate all migrate-able pages within
  471. * a single pageblock
  472. * @cc: Compaction control structure.
  473. * @low_pfn: The first PFN to isolate
  474. * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock
  475. * @isolate_mode: Isolation mode to be used.
  476. *
  477. * Isolate all pages that can be migrated from the range specified by
  478. * [low_pfn, end_pfn). The range is expected to be within same pageblock.
  479. * Returns zero if there is a fatal signal pending, otherwise PFN of the
  480. * first page that was not scanned (which may be both less, equal to or more
  481. * than end_pfn).
  482. *
  483. * The pages are isolated on cc->migratepages list (not required to be empty),
  484. * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field
  485. * is neither read nor updated.
  486. */
  487. static unsigned long
  488. isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
  489. unsigned long end_pfn, isolate_mode_t isolate_mode)
  490. {
  491. struct zone *zone = cc->zone;
  492. unsigned long nr_scanned = 0, nr_isolated = 0;
  493. struct list_head *migratelist = &cc->migratepages;
  494. struct lruvec *lruvec;
  495. unsigned long flags = 0;
  496. bool locked = false;
  497. struct page *page = NULL, *valid_page = NULL;
  498. /*
  499. * Ensure that there are not too many pages isolated from the LRU
  500. * list by either parallel reclaimers or compaction. If there are,
  501. * delay for some time until fewer pages are isolated
  502. */
  503. while (unlikely(too_many_isolated(zone))) {
  504. /* async migration should just abort */
  505. if (cc->mode == MIGRATE_ASYNC)
  506. return 0;
  507. congestion_wait(BLK_RW_ASYNC, HZ/10);
  508. if (fatal_signal_pending(current))
  509. return 0;
  510. }
  511. if (compact_should_abort(cc))
  512. return 0;
  513. /* Time to isolate some pages for migration */
  514. for (; low_pfn < end_pfn; low_pfn++) {
  515. /*
  516. * Periodically drop the lock (if held) regardless of its
  517. * contention, to give chance to IRQs. Abort async compaction
  518. * if contended.
  519. */
  520. if (!(low_pfn % SWAP_CLUSTER_MAX)
  521. && compact_unlock_should_abort(&zone->lru_lock, flags,
  522. &locked, cc))
  523. break;
  524. if (!pfn_valid_within(low_pfn))
  525. continue;
  526. nr_scanned++;
  527. page = pfn_to_page(low_pfn);
  528. if (!valid_page)
  529. valid_page = page;
  530. /*
  531. * Skip if free. We read page order here without zone lock
  532. * which is generally unsafe, but the race window is small and
  533. * the worst thing that can happen is that we skip some
  534. * potential isolation targets.
  535. */
  536. if (PageBuddy(page)) {
  537. unsigned long freepage_order = page_order_unsafe(page);
  538. /*
  539. * Without lock, we cannot be sure that what we got is
  540. * a valid page order. Consider only values in the
  541. * valid order range to prevent low_pfn overflow.
  542. */
  543. if (freepage_order > 0 && freepage_order < MAX_ORDER)
  544. low_pfn += (1UL << freepage_order) - 1;
  545. continue;
  546. }
  547. /*
  548. * Check may be lockless but that's ok as we recheck later.
  549. * It's possible to migrate LRU pages and balloon pages
  550. * Skip any other type of page
  551. */
  552. if (!PageLRU(page)) {
  553. if (unlikely(balloon_page_movable(page))) {
  554. if (balloon_page_isolate(page)) {
  555. /* Successfully isolated */
  556. goto isolate_success;
  557. }
  558. }
  559. continue;
  560. }
  561. /*
  562. * PageLRU is set. lru_lock normally excludes isolation
  563. * splitting and collapsing (collapsing has already happened
  564. * if PageLRU is set) but the lock is not necessarily taken
  565. * here and it is wasteful to take it just to check transhuge.
  566. * Check TransHuge without lock and skip the whole pageblock if
  567. * it's either a transhuge or hugetlbfs page, as calling
  568. * compound_order() without preventing THP from splitting the
  569. * page underneath us may return surprising results.
  570. */
  571. if (PageTransHuge(page)) {
  572. if (!locked)
  573. low_pfn = ALIGN(low_pfn + 1,
  574. pageblock_nr_pages) - 1;
  575. else
  576. low_pfn += (1 << compound_order(page)) - 1;
  577. continue;
  578. }
  579. /*
  580. * Migration will fail if an anonymous page is pinned in memory,
  581. * so avoid taking lru_lock and isolating it unnecessarily in an
  582. * admittedly racy check.
  583. */
  584. if (!page_mapping(page) &&
  585. page_count(page) > page_mapcount(page))
  586. continue;
  587. /* If we already hold the lock, we can skip some rechecking */
  588. if (!locked) {
  589. locked = compact_trylock_irqsave(&zone->lru_lock,
  590. &flags, cc);
  591. if (!locked)
  592. break;
  593. /* Recheck PageLRU and PageTransHuge under lock */
  594. if (!PageLRU(page))
  595. continue;
  596. if (PageTransHuge(page)) {
  597. low_pfn += (1 << compound_order(page)) - 1;
  598. continue;
  599. }
  600. }
  601. lruvec = mem_cgroup_page_lruvec(page, zone);
  602. /* Try isolate the page */
  603. if (__isolate_lru_page(page, isolate_mode) != 0)
  604. continue;
  605. VM_BUG_ON_PAGE(PageTransCompound(page), page);
  606. /* Successfully isolated */
  607. del_page_from_lru_list(page, lruvec, page_lru(page));
  608. isolate_success:
  609. cc->finished_update_migrate = true;
  610. list_add(&page->lru, migratelist);
  611. cc->nr_migratepages++;
  612. nr_isolated++;
  613. /* Avoid isolating too much */
  614. if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
  615. ++low_pfn;
  616. break;
  617. }
  618. }
  619. /*
  620. * The PageBuddy() check could have potentially brought us outside
  621. * the range to be scanned.
  622. */
  623. if (unlikely(low_pfn > end_pfn))
  624. low_pfn = end_pfn;
  625. if (locked)
  626. spin_unlock_irqrestore(&zone->lru_lock, flags);
  627. /*
  628. * Update the pageblock-skip information and cached scanner pfn,
  629. * if the whole pageblock was scanned without isolating any page.
  630. */
  631. if (low_pfn == end_pfn)
  632. update_pageblock_skip(cc, valid_page, nr_isolated, true);
  633. trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
  634. count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned);
  635. if (nr_isolated)
  636. count_compact_events(COMPACTISOLATED, nr_isolated);
  637. return low_pfn;
  638. }
  639. /**
  640. * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
  641. * @cc: Compaction control structure.
  642. * @start_pfn: The first PFN to start isolating.
  643. * @end_pfn: The one-past-last PFN.
  644. *
  645. * Returns zero if isolation fails fatally due to e.g. pending signal.
  646. * Otherwise, function returns one-past-the-last PFN of isolated page
  647. * (which may be greater than end_pfn if end fell in a middle of a THP page).
  648. */
  649. unsigned long
  650. isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
  651. unsigned long end_pfn)
  652. {
  653. unsigned long pfn, block_end_pfn;
  654. /* Scan block by block. First and last block may be incomplete */
  655. pfn = start_pfn;
  656. block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
  657. for (; pfn < end_pfn; pfn = block_end_pfn,
  658. block_end_pfn += pageblock_nr_pages) {
  659. block_end_pfn = min(block_end_pfn, end_pfn);
  660. if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
  661. continue;
  662. pfn = isolate_migratepages_block(cc, pfn, block_end_pfn,
  663. ISOLATE_UNEVICTABLE);
  664. /*
  665. * In case of fatal failure, release everything that might
  666. * have been isolated in the previous iteration, and signal
  667. * the failure back to caller.
  668. */
  669. if (!pfn) {
  670. putback_movable_pages(&cc->migratepages);
  671. cc->nr_migratepages = 0;
  672. break;
  673. }
  674. if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
  675. break;
  676. }
  677. acct_isolated(cc->zone, cc);
  678. return pfn;
  679. }
  680. #endif /* CONFIG_COMPACTION || CONFIG_CMA */
  681. #ifdef CONFIG_COMPACTION
  682. /*
  683. * Based on information in the current compact_control, find blocks
  684. * suitable for isolating free pages from and then isolate them.
  685. */
  686. static void isolate_freepages(struct compact_control *cc)
  687. {
  688. struct zone *zone = cc->zone;
  689. struct page *page;
  690. unsigned long block_start_pfn; /* start of current pageblock */
  691. unsigned long isolate_start_pfn; /* exact pfn we start at */
  692. unsigned long block_end_pfn; /* end of current pageblock */
  693. unsigned long low_pfn; /* lowest pfn scanner is able to scan */
  694. int nr_freepages = cc->nr_freepages;
  695. struct list_head *freelist = &cc->freepages;
  696. /*
  697. * Initialise the free scanner. The starting point is where we last
  698. * successfully isolated from, zone-cached value, or the end of the
  699. * zone when isolating for the first time. For looping we also need
  700. * this pfn aligned down to the pageblock boundary, because we do
  701. * block_start_pfn -= pageblock_nr_pages in the for loop.
  702. * For ending point, take care when isolating in last pageblock of a
  703. * a zone which ends in the middle of a pageblock.
  704. * The low boundary is the end of the pageblock the migration scanner
  705. * is using.
  706. */
  707. isolate_start_pfn = cc->free_pfn;
  708. block_start_pfn = cc->free_pfn & ~(pageblock_nr_pages-1);
  709. block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
  710. zone_end_pfn(zone));
  711. low_pfn = ALIGN(cc->migrate_pfn + 1, pageblock_nr_pages);
  712. /*
  713. * Isolate free pages until enough are available to migrate the
  714. * pages on cc->migratepages. We stop searching if the migrate
  715. * and free page scanners meet or enough free pages are isolated.
  716. */
  717. for (; block_start_pfn >= low_pfn && cc->nr_migratepages > nr_freepages;
  718. block_end_pfn = block_start_pfn,
  719. block_start_pfn -= pageblock_nr_pages,
  720. isolate_start_pfn = block_start_pfn) {
  721. unsigned long isolated;
  722. /*
  723. * This can iterate a massively long zone without finding any
  724. * suitable migration targets, so periodically check if we need
  725. * to schedule, or even abort async compaction.
  726. */
  727. if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
  728. && compact_should_abort(cc))
  729. break;
  730. page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
  731. zone);
  732. if (!page)
  733. continue;
  734. /* Check the block is suitable for migration */
  735. if (!suitable_migration_target(page))
  736. continue;
  737. /* If isolation recently failed, do not retry */
  738. if (!isolation_suitable(cc, page))
  739. continue;
  740. /* Found a block suitable for isolating free pages from. */
  741. isolated = isolate_freepages_block(cc, &isolate_start_pfn,
  742. block_end_pfn, freelist, false);
  743. nr_freepages += isolated;
  744. /*
  745. * Remember where the free scanner should restart next time,
  746. * which is where isolate_freepages_block() left off.
  747. * But if it scanned the whole pageblock, isolate_start_pfn
  748. * now points at block_end_pfn, which is the start of the next
  749. * pageblock.
  750. * In that case we will however want to restart at the start
  751. * of the previous pageblock.
  752. */
  753. cc->free_pfn = (isolate_start_pfn < block_end_pfn) ?
  754. isolate_start_pfn :
  755. block_start_pfn - pageblock_nr_pages;
  756. /*
  757. * Set a flag that we successfully isolated in this pageblock.
  758. * In the next loop iteration, zone->compact_cached_free_pfn
  759. * will not be updated and thus it will effectively contain the
  760. * highest pageblock we isolated pages from.
  761. */
  762. if (isolated)
  763. cc->finished_update_free = true;
  764. /*
  765. * isolate_freepages_block() might have aborted due to async
  766. * compaction being contended
  767. */
  768. if (cc->contended)
  769. break;
  770. }
  771. /* split_free_page does not map the pages */
  772. map_pages(freelist);
  773. /*
  774. * If we crossed the migrate scanner, we want to keep it that way
  775. * so that compact_finished() may detect this
  776. */
  777. if (block_start_pfn < low_pfn)
  778. cc->free_pfn = cc->migrate_pfn;
  779. cc->nr_freepages = nr_freepages;
  780. }
  781. /*
  782. * This is a migrate-callback that "allocates" freepages by taking pages
  783. * from the isolated freelists in the block we are migrating to.
  784. */
  785. static struct page *compaction_alloc(struct page *migratepage,
  786. unsigned long data,
  787. int **result)
  788. {
  789. struct compact_control *cc = (struct compact_control *)data;
  790. struct page *freepage;
  791. /*
  792. * Isolate free pages if necessary, and if we are not aborting due to
  793. * contention.
  794. */
  795. if (list_empty(&cc->freepages)) {
  796. if (!cc->contended)
  797. isolate_freepages(cc);
  798. if (list_empty(&cc->freepages))
  799. return NULL;
  800. }
  801. freepage = list_entry(cc->freepages.next, struct page, lru);
  802. list_del(&freepage->lru);
  803. cc->nr_freepages--;
  804. return freepage;
  805. }
  806. /*
  807. * This is a migrate-callback that "frees" freepages back to the isolated
  808. * freelist. All pages on the freelist are from the same zone, so there is no
  809. * special handling needed for NUMA.
  810. */
  811. static void compaction_free(struct page *page, unsigned long data)
  812. {
  813. struct compact_control *cc = (struct compact_control *)data;
  814. list_add(&page->lru, &cc->freepages);
  815. cc->nr_freepages++;
  816. }
  817. /* possible outcome of isolate_migratepages */
  818. typedef enum {
  819. ISOLATE_ABORT, /* Abort compaction now */
  820. ISOLATE_NONE, /* No pages isolated, continue scanning */
  821. ISOLATE_SUCCESS, /* Pages isolated, migrate */
  822. } isolate_migrate_t;
  823. /*
  824. * Isolate all pages that can be migrated from the first suitable block,
  825. * starting at the block pointed to by the migrate scanner pfn within
  826. * compact_control.
  827. */
  828. static isolate_migrate_t isolate_migratepages(struct zone *zone,
  829. struct compact_control *cc)
  830. {
  831. unsigned long low_pfn, end_pfn;
  832. struct page *page;
  833. const isolate_mode_t isolate_mode =
  834. (cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0);
  835. /*
  836. * Start at where we last stopped, or beginning of the zone as
  837. * initialized by compact_zone()
  838. */
  839. low_pfn = cc->migrate_pfn;
  840. /* Only scan within a pageblock boundary */
  841. end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages);
  842. /*
  843. * Iterate over whole pageblocks until we find the first suitable.
  844. * Do not cross the free scanner.
  845. */
  846. for (; end_pfn <= cc->free_pfn;
  847. low_pfn = end_pfn, end_pfn += pageblock_nr_pages) {
  848. /*
  849. * This can potentially iterate a massively long zone with
  850. * many pageblocks unsuitable, so periodically check if we
  851. * need to schedule, or even abort async compaction.
  852. */
  853. if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
  854. && compact_should_abort(cc))
  855. break;
  856. page = pageblock_pfn_to_page(low_pfn, end_pfn, zone);
  857. if (!page)
  858. continue;
  859. /* If isolation recently failed, do not retry */
  860. if (!isolation_suitable(cc, page))
  861. continue;
  862. /*
  863. * For async compaction, also only scan in MOVABLE blocks.
  864. * Async compaction is optimistic to see if the minimum amount
  865. * of work satisfies the allocation.
  866. */
  867. if (cc->mode == MIGRATE_ASYNC &&
  868. !migrate_async_suitable(get_pageblock_migratetype(page)))
  869. continue;
  870. /* Perform the isolation */
  871. low_pfn = isolate_migratepages_block(cc, low_pfn, end_pfn,
  872. isolate_mode);
  873. if (!low_pfn || cc->contended) {
  874. acct_isolated(zone, cc);
  875. return ISOLATE_ABORT;
  876. }
  877. /*
  878. * Either we isolated something and proceed with migration. Or
  879. * we failed and compact_zone should decide if we should
  880. * continue or not.
  881. */
  882. break;
  883. }
  884. acct_isolated(zone, cc);
  885. /*
  886. * Record where migration scanner will be restarted. If we end up in
  887. * the same pageblock as the free scanner, make the scanners fully
  888. * meet so that compact_finished() terminates compaction.
  889. */
  890. cc->migrate_pfn = (end_pfn <= cc->free_pfn) ? low_pfn : cc->free_pfn;
  891. return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
  892. }
  893. static int compact_finished(struct zone *zone, struct compact_control *cc,
  894. const int migratetype)
  895. {
  896. unsigned int order;
  897. unsigned long watermark;
  898. if (cc->contended || fatal_signal_pending(current))
  899. return COMPACT_PARTIAL;
  900. /* Compaction run completes if the migrate and free scanner meet */
  901. if (cc->free_pfn <= cc->migrate_pfn) {
  902. /* Let the next compaction start anew. */
  903. zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
  904. zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
  905. zone->compact_cached_free_pfn = zone_end_pfn(zone);
  906. /*
  907. * Mark that the PG_migrate_skip information should be cleared
  908. * by kswapd when it goes to sleep. kswapd does not set the
  909. * flag itself as the decision to be clear should be directly
  910. * based on an allocation request.
  911. */
  912. if (!current_is_kswapd())
  913. zone->compact_blockskip_flush = true;
  914. return COMPACT_COMPLETE;
  915. }
  916. /*
  917. * order == -1 is expected when compacting via
  918. * /proc/sys/vm/compact_memory
  919. */
  920. if (cc->order == -1)
  921. return COMPACT_CONTINUE;
  922. /* Compaction run is not finished if the watermark is not met */
  923. watermark = low_wmark_pages(zone);
  924. watermark += (1 << cc->order);
  925. if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
  926. return COMPACT_CONTINUE;
  927. /* Direct compactor: Is a suitable page free? */
  928. for (order = cc->order; order < MAX_ORDER; order++) {
  929. struct free_area *area = &zone->free_area[order];
  930. /* Job done if page is free of the right migratetype */
  931. if (!list_empty(&area->free_list[migratetype]))
  932. return COMPACT_PARTIAL;
  933. /* Job done if allocation would set block type */
  934. if (order >= pageblock_order && area->nr_free)
  935. return COMPACT_PARTIAL;
  936. }
  937. return COMPACT_CONTINUE;
  938. }
  939. /*
  940. * compaction_suitable: Is this suitable to run compaction on this zone now?
  941. * Returns
  942. * COMPACT_SKIPPED - If there are too few free pages for compaction
  943. * COMPACT_PARTIAL - If the allocation would succeed without compaction
  944. * COMPACT_CONTINUE - If compaction should run now
  945. */
  946. unsigned long compaction_suitable(struct zone *zone, int order)
  947. {
  948. int fragindex;
  949. unsigned long watermark;
  950. /*
  951. * order == -1 is expected when compacting via
  952. * /proc/sys/vm/compact_memory
  953. */
  954. if (order == -1)
  955. return COMPACT_CONTINUE;
  956. /*
  957. * Watermarks for order-0 must be met for compaction. Note the 2UL.
  958. * This is because during migration, copies of pages need to be
  959. * allocated and for a short time, the footprint is higher
  960. */
  961. watermark = low_wmark_pages(zone) + (2UL << order);
  962. if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
  963. return COMPACT_SKIPPED;
  964. /*
  965. * fragmentation index determines if allocation failures are due to
  966. * low memory or external fragmentation
  967. *
  968. * index of -1000 implies allocations might succeed depending on
  969. * watermarks
  970. * index towards 0 implies failure is due to lack of memory
  971. * index towards 1000 implies failure is due to fragmentation
  972. *
  973. * Only compact if a failure would be due to fragmentation.
  974. */
  975. fragindex = fragmentation_index(zone, order);
  976. if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
  977. return COMPACT_SKIPPED;
  978. if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
  979. 0, 0))
  980. return COMPACT_PARTIAL;
  981. return COMPACT_CONTINUE;
  982. }
  983. static int compact_zone(struct zone *zone, struct compact_control *cc)
  984. {
  985. int ret;
  986. unsigned long start_pfn = zone->zone_start_pfn;
  987. unsigned long end_pfn = zone_end_pfn(zone);
  988. const int migratetype = gfpflags_to_migratetype(cc->gfp_mask);
  989. const bool sync = cc->mode != MIGRATE_ASYNC;
  990. ret = compaction_suitable(zone, cc->order);
  991. switch (ret) {
  992. case COMPACT_PARTIAL:
  993. case COMPACT_SKIPPED:
  994. /* Compaction is likely to fail */
  995. return ret;
  996. case COMPACT_CONTINUE:
  997. /* Fall through to compaction */
  998. ;
  999. }
  1000. /*
  1001. * Clear pageblock skip if there were failures recently and compaction
  1002. * is about to be retried after being deferred. kswapd does not do
  1003. * this reset as it'll reset the cached information when going to sleep.
  1004. */
  1005. if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
  1006. __reset_isolation_suitable(zone);
  1007. /*
  1008. * Setup to move all movable pages to the end of the zone. Used cached
  1009. * information on where the scanners should start but check that it
  1010. * is initialised by ensuring the values are within zone boundaries.
  1011. */
  1012. cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync];
  1013. cc->free_pfn = zone->compact_cached_free_pfn;
  1014. if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
  1015. cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
  1016. zone->compact_cached_free_pfn = cc->free_pfn;
  1017. }
  1018. if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
  1019. cc->migrate_pfn = start_pfn;
  1020. zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
  1021. zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
  1022. }
  1023. trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, cc->free_pfn, end_pfn);
  1024. migrate_prep_local();
  1025. while ((ret = compact_finished(zone, cc, migratetype)) ==
  1026. COMPACT_CONTINUE) {
  1027. int err;
  1028. switch (isolate_migratepages(zone, cc)) {
  1029. case ISOLATE_ABORT:
  1030. ret = COMPACT_PARTIAL;
  1031. putback_movable_pages(&cc->migratepages);
  1032. cc->nr_migratepages = 0;
  1033. goto out;
  1034. case ISOLATE_NONE:
  1035. continue;
  1036. case ISOLATE_SUCCESS:
  1037. ;
  1038. }
  1039. err = migrate_pages(&cc->migratepages, compaction_alloc,
  1040. compaction_free, (unsigned long)cc, cc->mode,
  1041. MR_COMPACTION);
  1042. trace_mm_compaction_migratepages(cc->nr_migratepages, err,
  1043. &cc->migratepages);
  1044. /* All pages were either migrated or will be released */
  1045. cc->nr_migratepages = 0;
  1046. if (err) {
  1047. putback_movable_pages(&cc->migratepages);
  1048. /*
  1049. * migrate_pages() may return -ENOMEM when scanners meet
  1050. * and we want compact_finished() to detect it
  1051. */
  1052. if (err == -ENOMEM && cc->free_pfn > cc->migrate_pfn) {
  1053. ret = COMPACT_PARTIAL;
  1054. goto out;
  1055. }
  1056. }
  1057. }
  1058. out:
  1059. /* Release free pages and check accounting */
  1060. cc->nr_freepages -= release_freepages(&cc->freepages);
  1061. VM_BUG_ON(cc->nr_freepages != 0);
  1062. trace_mm_compaction_end(ret);
  1063. return ret;
  1064. }
  1065. static unsigned long compact_zone_order(struct zone *zone, int order,
  1066. gfp_t gfp_mask, enum migrate_mode mode, int *contended)
  1067. {
  1068. unsigned long ret;
  1069. struct compact_control cc = {
  1070. .nr_freepages = 0,
  1071. .nr_migratepages = 0,
  1072. .order = order,
  1073. .gfp_mask = gfp_mask,
  1074. .zone = zone,
  1075. .mode = mode,
  1076. };
  1077. INIT_LIST_HEAD(&cc.freepages);
  1078. INIT_LIST_HEAD(&cc.migratepages);
  1079. ret = compact_zone(zone, &cc);
  1080. VM_BUG_ON(!list_empty(&cc.freepages));
  1081. VM_BUG_ON(!list_empty(&cc.migratepages));
  1082. *contended = cc.contended;
  1083. return ret;
  1084. }
  1085. int sysctl_extfrag_threshold = 500;
  1086. /**
  1087. * try_to_compact_pages - Direct compact to satisfy a high-order allocation
  1088. * @zonelist: The zonelist used for the current allocation
  1089. * @order: The order of the current allocation
  1090. * @gfp_mask: The GFP mask of the current allocation
  1091. * @nodemask: The allowed nodes to allocate from
  1092. * @mode: The migration mode for async, sync light, or sync migration
  1093. * @contended: Return value that determines if compaction was aborted due to
  1094. * need_resched() or lock contention
  1095. * @candidate_zone: Return the zone where we think allocation should succeed
  1096. *
  1097. * This is the main entry point for direct page compaction.
  1098. */
  1099. unsigned long try_to_compact_pages(struct zonelist *zonelist,
  1100. int order, gfp_t gfp_mask, nodemask_t *nodemask,
  1101. enum migrate_mode mode, int *contended,
  1102. struct zone **candidate_zone)
  1103. {
  1104. enum zone_type high_zoneidx = gfp_zone(gfp_mask);
  1105. int may_enter_fs = gfp_mask & __GFP_FS;
  1106. int may_perform_io = gfp_mask & __GFP_IO;
  1107. struct zoneref *z;
  1108. struct zone *zone;
  1109. int rc = COMPACT_DEFERRED;
  1110. int alloc_flags = 0;
  1111. int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */
  1112. *contended = COMPACT_CONTENDED_NONE;
  1113. /* Check if the GFP flags allow compaction */
  1114. if (!order || !may_enter_fs || !may_perform_io)
  1115. return COMPACT_SKIPPED;
  1116. #ifdef CONFIG_CMA
  1117. if (gfpflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
  1118. alloc_flags |= ALLOC_CMA;
  1119. #endif
  1120. /* Compact each zone in the list */
  1121. for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
  1122. nodemask) {
  1123. int status;
  1124. int zone_contended;
  1125. if (compaction_deferred(zone, order))
  1126. continue;
  1127. status = compact_zone_order(zone, order, gfp_mask, mode,
  1128. &zone_contended);
  1129. rc = max(status, rc);
  1130. /*
  1131. * It takes at least one zone that wasn't lock contended
  1132. * to clear all_zones_contended.
  1133. */
  1134. all_zones_contended &= zone_contended;
  1135. /* If a normal allocation would succeed, stop compacting */
  1136. if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
  1137. alloc_flags)) {
  1138. *candidate_zone = zone;
  1139. /*
  1140. * We think the allocation will succeed in this zone,
  1141. * but it is not certain, hence the false. The caller
  1142. * will repeat this with true if allocation indeed
  1143. * succeeds in this zone.
  1144. */
  1145. compaction_defer_reset(zone, order, false);
  1146. /*
  1147. * It is possible that async compaction aborted due to
  1148. * need_resched() and the watermarks were ok thanks to
  1149. * somebody else freeing memory. The allocation can
  1150. * however still fail so we better signal the
  1151. * need_resched() contention anyway (this will not
  1152. * prevent the allocation attempt).
  1153. */
  1154. if (zone_contended == COMPACT_CONTENDED_SCHED)
  1155. *contended = COMPACT_CONTENDED_SCHED;
  1156. goto break_loop;
  1157. }
  1158. if (mode != MIGRATE_ASYNC) {
  1159. /*
  1160. * We think that allocation won't succeed in this zone
  1161. * so we defer compaction there. If it ends up
  1162. * succeeding after all, it will be reset.
  1163. */
  1164. defer_compaction(zone, order);
  1165. }
  1166. /*
  1167. * We might have stopped compacting due to need_resched() in
  1168. * async compaction, or due to a fatal signal detected. In that
  1169. * case do not try further zones and signal need_resched()
  1170. * contention.
  1171. */
  1172. if ((zone_contended == COMPACT_CONTENDED_SCHED)
  1173. || fatal_signal_pending(current)) {
  1174. *contended = COMPACT_CONTENDED_SCHED;
  1175. goto break_loop;
  1176. }
  1177. continue;
  1178. break_loop:
  1179. /*
  1180. * We might not have tried all the zones, so be conservative
  1181. * and assume they are not all lock contended.
  1182. */
  1183. all_zones_contended = 0;
  1184. break;
  1185. }
  1186. /*
  1187. * If at least one zone wasn't deferred or skipped, we report if all
  1188. * zones that were tried were lock contended.
  1189. */
  1190. if (rc > COMPACT_SKIPPED && all_zones_contended)
  1191. *contended = COMPACT_CONTENDED_LOCK;
  1192. return rc;
  1193. }
  1194. /* Compact all zones within a node */
  1195. static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
  1196. {
  1197. int zoneid;
  1198. struct zone *zone;
  1199. for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
  1200. zone = &pgdat->node_zones[zoneid];
  1201. if (!populated_zone(zone))
  1202. continue;
  1203. cc->nr_freepages = 0;
  1204. cc->nr_migratepages = 0;
  1205. cc->zone = zone;
  1206. INIT_LIST_HEAD(&cc->freepages);
  1207. INIT_LIST_HEAD(&cc->migratepages);
  1208. if (cc->order == -1 || !compaction_deferred(zone, cc->order))
  1209. compact_zone(zone, cc);
  1210. if (cc->order > 0) {
  1211. if (zone_watermark_ok(zone, cc->order,
  1212. low_wmark_pages(zone), 0, 0))
  1213. compaction_defer_reset(zone, cc->order, false);
  1214. }
  1215. VM_BUG_ON(!list_empty(&cc->freepages));
  1216. VM_BUG_ON(!list_empty(&cc->migratepages));
  1217. }
  1218. }
  1219. void compact_pgdat(pg_data_t *pgdat, int order)
  1220. {
  1221. struct compact_control cc = {
  1222. .order = order,
  1223. .mode = MIGRATE_ASYNC,
  1224. };
  1225. if (!order)
  1226. return;
  1227. __compact_pgdat(pgdat, &cc);
  1228. }
  1229. static void compact_node(int nid)
  1230. {
  1231. struct compact_control cc = {
  1232. .order = -1,
  1233. .mode = MIGRATE_SYNC,
  1234. .ignore_skip_hint = true,
  1235. };
  1236. __compact_pgdat(NODE_DATA(nid), &cc);
  1237. }
  1238. /* Compact all nodes in the system */
  1239. static void compact_nodes(void)
  1240. {
  1241. int nid;
  1242. /* Flush pending updates to the LRU lists */
  1243. lru_add_drain_all();
  1244. for_each_online_node(nid)
  1245. compact_node(nid);
  1246. }
  1247. /* The written value is actually unused, all memory is compacted */
  1248. int sysctl_compact_memory;
  1249. /* This is the entry point for compacting all nodes via /proc/sys/vm */
  1250. int sysctl_compaction_handler(struct ctl_table *table, int write,
  1251. void __user *buffer, size_t *length, loff_t *ppos)
  1252. {
  1253. if (write)
  1254. compact_nodes();
  1255. return 0;
  1256. }
  1257. int sysctl_extfrag_handler(struct ctl_table *table, int write,
  1258. void __user *buffer, size_t *length, loff_t *ppos)
  1259. {
  1260. proc_dointvec_minmax(table, write, buffer, length, ppos);
  1261. return 0;
  1262. }
  1263. #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
  1264. static ssize_t sysfs_compact_node(struct device *dev,
  1265. struct device_attribute *attr,
  1266. const char *buf, size_t count)
  1267. {
  1268. int nid = dev->id;
  1269. if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
  1270. /* Flush pending updates to the LRU lists */
  1271. lru_add_drain_all();
  1272. compact_node(nid);
  1273. }
  1274. return count;
  1275. }
  1276. static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
  1277. int compaction_register_node(struct node *node)
  1278. {
  1279. return device_create_file(&node->dev, &dev_attr_compact);
  1280. }
  1281. void compaction_unregister_node(struct node *node)
  1282. {
  1283. return device_remove_file(&node->dev, &dev_attr_compact);
  1284. }
  1285. #endif /* CONFIG_SYSFS && CONFIG_NUMA */
  1286. #endif /* CONFIG_COMPACTION */