compaction.c 46 KB

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