compaction.c 58 KB

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