compaction.c 48 KB

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