compaction.c 47 KB

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