compaction.c 56 KB

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