compaction.c 56 KB

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