compaction.c 58 KB

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