compaction.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084
  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. /* Update the number of anon and file isolated pages in the zone */
  533. static void acct_isolated(struct zone *zone, struct compact_control *cc)
  534. {
  535. struct page *page;
  536. unsigned int count[2] = { 0, };
  537. if (list_empty(&cc->migratepages))
  538. return;
  539. list_for_each_entry(page, &cc->migratepages, lru)
  540. count[!!page_is_file_cache(page)]++;
  541. mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_ANON, count[0]);
  542. mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, count[1]);
  543. }
  544. /* Similar to reclaim, but different enough that they don't share logic */
  545. static bool too_many_isolated(struct zone *zone)
  546. {
  547. unsigned long active, inactive, isolated;
  548. inactive = node_page_state(zone->zone_pgdat, NR_INACTIVE_FILE) +
  549. node_page_state(zone->zone_pgdat, NR_INACTIVE_ANON);
  550. active = node_page_state(zone->zone_pgdat, NR_ACTIVE_FILE) +
  551. node_page_state(zone->zone_pgdat, NR_ACTIVE_ANON);
  552. isolated = node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE) +
  553. node_page_state(zone->zone_pgdat, NR_ISOLATED_ANON);
  554. return isolated > (inactive + active) / 2;
  555. }
  556. /**
  557. * isolate_migratepages_block() - isolate all migrate-able pages within
  558. * a single pageblock
  559. * @cc: Compaction control structure.
  560. * @low_pfn: The first PFN to isolate
  561. * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock
  562. * @isolate_mode: Isolation mode to be used.
  563. *
  564. * Isolate all pages that can be migrated from the range specified by
  565. * [low_pfn, end_pfn). The range is expected to be within same pageblock.
  566. * Returns zero if there is a fatal signal pending, otherwise PFN of the
  567. * first page that was not scanned (which may be both less, equal to or more
  568. * than end_pfn).
  569. *
  570. * The pages are isolated on cc->migratepages list (not required to be empty),
  571. * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field
  572. * is neither read nor updated.
  573. */
  574. static unsigned long
  575. isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
  576. unsigned long end_pfn, isolate_mode_t isolate_mode)
  577. {
  578. struct zone *zone = cc->zone;
  579. unsigned long nr_scanned = 0, nr_isolated = 0;
  580. struct lruvec *lruvec;
  581. unsigned long flags = 0;
  582. bool locked = false;
  583. struct page *page = NULL, *valid_page = NULL;
  584. unsigned long start_pfn = low_pfn;
  585. bool skip_on_failure = false;
  586. unsigned long next_skip_pfn = 0;
  587. /*
  588. * Ensure that there are not too many pages isolated from the LRU
  589. * list by either parallel reclaimers or compaction. If there are,
  590. * delay for some time until fewer pages are isolated
  591. */
  592. while (unlikely(too_many_isolated(zone))) {
  593. /* async migration should just abort */
  594. if (cc->mode == MIGRATE_ASYNC)
  595. return 0;
  596. congestion_wait(BLK_RW_ASYNC, HZ/10);
  597. if (fatal_signal_pending(current))
  598. return 0;
  599. }
  600. if (compact_should_abort(cc))
  601. return 0;
  602. if (cc->direct_compaction && (cc->mode == MIGRATE_ASYNC)) {
  603. skip_on_failure = true;
  604. next_skip_pfn = block_end_pfn(low_pfn, cc->order);
  605. }
  606. /* Time to isolate some pages for migration */
  607. for (; low_pfn < end_pfn; low_pfn++) {
  608. if (skip_on_failure && low_pfn >= next_skip_pfn) {
  609. /*
  610. * We have isolated all migration candidates in the
  611. * previous order-aligned block, and did not skip it due
  612. * to failure. We should migrate the pages now and
  613. * hopefully succeed compaction.
  614. */
  615. if (nr_isolated)
  616. break;
  617. /*
  618. * We failed to isolate in the previous order-aligned
  619. * block. Set the new boundary to the end of the
  620. * current block. Note we can't simply increase
  621. * next_skip_pfn by 1 << order, as low_pfn might have
  622. * been incremented by a higher number due to skipping
  623. * a compound or a high-order buddy page in the
  624. * previous loop iteration.
  625. */
  626. next_skip_pfn = block_end_pfn(low_pfn, cc->order);
  627. }
  628. /*
  629. * Periodically drop the lock (if held) regardless of its
  630. * contention, to give chance to IRQs. Abort async compaction
  631. * if contended.
  632. */
  633. if (!(low_pfn % SWAP_CLUSTER_MAX)
  634. && compact_unlock_should_abort(zone_lru_lock(zone), flags,
  635. &locked, cc))
  636. break;
  637. if (!pfn_valid_within(low_pfn))
  638. goto isolate_fail;
  639. nr_scanned++;
  640. page = pfn_to_page(low_pfn);
  641. if (!valid_page)
  642. valid_page = page;
  643. /*
  644. * Skip if free. We read page order here without zone lock
  645. * which is generally unsafe, but the race window is small and
  646. * the worst thing that can happen is that we skip some
  647. * potential isolation targets.
  648. */
  649. if (PageBuddy(page)) {
  650. unsigned long freepage_order = page_order_unsafe(page);
  651. /*
  652. * Without lock, we cannot be sure that what we got is
  653. * a valid page order. Consider only values in the
  654. * valid order range to prevent low_pfn overflow.
  655. */
  656. if (freepage_order > 0 && freepage_order < MAX_ORDER)
  657. low_pfn += (1UL << freepage_order) - 1;
  658. continue;
  659. }
  660. /*
  661. * Regardless of being on LRU, compound pages such as THP and
  662. * hugetlbfs are not to be compacted. We can potentially save
  663. * a lot of iterations if we skip them at once. The check is
  664. * racy, but we can consider only valid values and the only
  665. * danger is skipping too much.
  666. */
  667. if (PageCompound(page)) {
  668. unsigned int comp_order = compound_order(page);
  669. if (likely(comp_order < MAX_ORDER))
  670. low_pfn += (1UL << comp_order) - 1;
  671. goto isolate_fail;
  672. }
  673. /*
  674. * Check may be lockless but that's ok as we recheck later.
  675. * It's possible to migrate LRU and non-lru movable pages.
  676. * Skip any other type of page
  677. */
  678. if (!PageLRU(page)) {
  679. /*
  680. * __PageMovable can return false positive so we need
  681. * to verify it under page_lock.
  682. */
  683. if (unlikely(__PageMovable(page)) &&
  684. !PageIsolated(page)) {
  685. if (locked) {
  686. spin_unlock_irqrestore(zone_lru_lock(zone),
  687. flags);
  688. locked = false;
  689. }
  690. if (isolate_movable_page(page, isolate_mode))
  691. goto isolate_success;
  692. }
  693. goto isolate_fail;
  694. }
  695. /*
  696. * Migration will fail if an anonymous page is pinned in memory,
  697. * so avoid taking lru_lock and isolating it unnecessarily in an
  698. * admittedly racy check.
  699. */
  700. if (!page_mapping(page) &&
  701. page_count(page) > page_mapcount(page))
  702. goto isolate_fail;
  703. /* If we already hold the lock, we can skip some rechecking */
  704. if (!locked) {
  705. locked = compact_trylock_irqsave(zone_lru_lock(zone),
  706. &flags, cc);
  707. if (!locked)
  708. break;
  709. /* Recheck PageLRU and PageCompound under lock */
  710. if (!PageLRU(page))
  711. goto isolate_fail;
  712. /*
  713. * Page become compound since the non-locked check,
  714. * and it's on LRU. It can only be a THP so the order
  715. * is safe to read and it's 0 for tail pages.
  716. */
  717. if (unlikely(PageCompound(page))) {
  718. low_pfn += (1UL << compound_order(page)) - 1;
  719. goto isolate_fail;
  720. }
  721. }
  722. lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat);
  723. /* Try isolate the page */
  724. if (__isolate_lru_page(page, isolate_mode) != 0)
  725. goto isolate_fail;
  726. VM_BUG_ON_PAGE(PageCompound(page), page);
  727. /* Successfully isolated */
  728. del_page_from_lru_list(page, lruvec, page_lru(page));
  729. isolate_success:
  730. list_add(&page->lru, &cc->migratepages);
  731. cc->nr_migratepages++;
  732. nr_isolated++;
  733. /*
  734. * Record where we could have freed pages by migration and not
  735. * yet flushed them to buddy allocator.
  736. * - this is the lowest page that was isolated and likely be
  737. * then freed by migration.
  738. */
  739. if (!cc->last_migrated_pfn)
  740. cc->last_migrated_pfn = low_pfn;
  741. /* Avoid isolating too much */
  742. if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
  743. ++low_pfn;
  744. break;
  745. }
  746. continue;
  747. isolate_fail:
  748. if (!skip_on_failure)
  749. continue;
  750. /*
  751. * We have isolated some pages, but then failed. Release them
  752. * instead of migrating, as we cannot form the cc->order buddy
  753. * page anyway.
  754. */
  755. if (nr_isolated) {
  756. if (locked) {
  757. spin_unlock_irqrestore(zone_lru_lock(zone), flags);
  758. locked = false;
  759. }
  760. acct_isolated(zone, cc);
  761. putback_movable_pages(&cc->migratepages);
  762. cc->nr_migratepages = 0;
  763. cc->last_migrated_pfn = 0;
  764. nr_isolated = 0;
  765. }
  766. if (low_pfn < next_skip_pfn) {
  767. low_pfn = next_skip_pfn - 1;
  768. /*
  769. * The check near the loop beginning would have updated
  770. * next_skip_pfn too, but this is a bit simpler.
  771. */
  772. next_skip_pfn += 1UL << cc->order;
  773. }
  774. }
  775. /*
  776. * The PageBuddy() check could have potentially brought us outside
  777. * the range to be scanned.
  778. */
  779. if (unlikely(low_pfn > end_pfn))
  780. low_pfn = end_pfn;
  781. if (locked)
  782. spin_unlock_irqrestore(zone_lru_lock(zone), flags);
  783. /*
  784. * Update the pageblock-skip information and cached scanner pfn,
  785. * if the whole pageblock was scanned without isolating any page.
  786. */
  787. if (low_pfn == end_pfn)
  788. update_pageblock_skip(cc, valid_page, nr_isolated, true);
  789. trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn,
  790. nr_scanned, nr_isolated);
  791. count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned);
  792. if (nr_isolated)
  793. count_compact_events(COMPACTISOLATED, nr_isolated);
  794. return low_pfn;
  795. }
  796. /**
  797. * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
  798. * @cc: Compaction control structure.
  799. * @start_pfn: The first PFN to start isolating.
  800. * @end_pfn: The one-past-last PFN.
  801. *
  802. * Returns zero if isolation fails fatally due to e.g. pending signal.
  803. * Otherwise, function returns one-past-the-last PFN of isolated page
  804. * (which may be greater than end_pfn if end fell in a middle of a THP page).
  805. */
  806. unsigned long
  807. isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
  808. unsigned long end_pfn)
  809. {
  810. unsigned long pfn, block_start_pfn, block_end_pfn;
  811. /* Scan block by block. First and last block may be incomplete */
  812. pfn = start_pfn;
  813. block_start_pfn = pageblock_start_pfn(pfn);
  814. if (block_start_pfn < cc->zone->zone_start_pfn)
  815. block_start_pfn = cc->zone->zone_start_pfn;
  816. block_end_pfn = pageblock_end_pfn(pfn);
  817. for (; pfn < end_pfn; pfn = block_end_pfn,
  818. block_start_pfn = block_end_pfn,
  819. block_end_pfn += pageblock_nr_pages) {
  820. block_end_pfn = min(block_end_pfn, end_pfn);
  821. if (!pageblock_pfn_to_page(block_start_pfn,
  822. block_end_pfn, cc->zone))
  823. continue;
  824. pfn = isolate_migratepages_block(cc, pfn, block_end_pfn,
  825. ISOLATE_UNEVICTABLE);
  826. if (!pfn)
  827. break;
  828. if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
  829. break;
  830. }
  831. acct_isolated(cc->zone, cc);
  832. return pfn;
  833. }
  834. #endif /* CONFIG_COMPACTION || CONFIG_CMA */
  835. #ifdef CONFIG_COMPACTION
  836. /* Returns true if the page is within a block suitable for migration to */
  837. static bool suitable_migration_target(struct page *page)
  838. {
  839. /* If the page is a large free page, then disallow migration */
  840. if (PageBuddy(page)) {
  841. /*
  842. * We are checking page_order without zone->lock taken. But
  843. * the only small danger is that we skip a potentially suitable
  844. * pageblock, so it's not worth to check order for valid range.
  845. */
  846. if (page_order_unsafe(page) >= pageblock_order)
  847. return false;
  848. }
  849. /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
  850. if (migrate_async_suitable(get_pageblock_migratetype(page)))
  851. return true;
  852. /* Otherwise skip the block */
  853. return false;
  854. }
  855. /*
  856. * Test whether the free scanner has reached the same or lower pageblock than
  857. * the migration scanner, and compaction should thus terminate.
  858. */
  859. static inline bool compact_scanners_met(struct compact_control *cc)
  860. {
  861. return (cc->free_pfn >> pageblock_order)
  862. <= (cc->migrate_pfn >> pageblock_order);
  863. }
  864. /*
  865. * Based on information in the current compact_control, find blocks
  866. * suitable for isolating free pages from and then isolate them.
  867. */
  868. static void isolate_freepages(struct compact_control *cc)
  869. {
  870. struct zone *zone = cc->zone;
  871. struct page *page;
  872. unsigned long block_start_pfn; /* start of current pageblock */
  873. unsigned long isolate_start_pfn; /* exact pfn we start at */
  874. unsigned long block_end_pfn; /* end of current pageblock */
  875. unsigned long low_pfn; /* lowest pfn scanner is able to scan */
  876. struct list_head *freelist = &cc->freepages;
  877. /*
  878. * Initialise the free scanner. The starting point is where we last
  879. * successfully isolated from, zone-cached value, or the end of the
  880. * zone when isolating for the first time. For looping we also need
  881. * this pfn aligned down to the pageblock boundary, because we do
  882. * block_start_pfn -= pageblock_nr_pages in the for loop.
  883. * For ending point, take care when isolating in last pageblock of a
  884. * a zone which ends in the middle of a pageblock.
  885. * The low boundary is the end of the pageblock the migration scanner
  886. * is using.
  887. */
  888. isolate_start_pfn = cc->free_pfn;
  889. block_start_pfn = pageblock_start_pfn(cc->free_pfn);
  890. block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
  891. zone_end_pfn(zone));
  892. low_pfn = pageblock_end_pfn(cc->migrate_pfn);
  893. /*
  894. * Isolate free pages until enough are available to migrate the
  895. * pages on cc->migratepages. We stop searching if the migrate
  896. * and free page scanners meet or enough free pages are isolated.
  897. */
  898. for (; block_start_pfn >= low_pfn;
  899. block_end_pfn = block_start_pfn,
  900. block_start_pfn -= pageblock_nr_pages,
  901. isolate_start_pfn = block_start_pfn) {
  902. /*
  903. * This can iterate a massively long zone without finding any
  904. * suitable migration targets, so periodically check if we need
  905. * to schedule, or even abort async compaction.
  906. */
  907. if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
  908. && compact_should_abort(cc))
  909. break;
  910. page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
  911. zone);
  912. if (!page)
  913. continue;
  914. /* Check the block is suitable for migration */
  915. if (!suitable_migration_target(page))
  916. continue;
  917. /* If isolation recently failed, do not retry */
  918. if (!isolation_suitable(cc, page))
  919. continue;
  920. /* Found a block suitable for isolating free pages from. */
  921. isolate_freepages_block(cc, &isolate_start_pfn, block_end_pfn,
  922. freelist, false);
  923. /*
  924. * If we isolated enough freepages, or aborted due to lock
  925. * contention, terminate.
  926. */
  927. if ((cc->nr_freepages >= cc->nr_migratepages)
  928. || cc->contended) {
  929. if (isolate_start_pfn >= block_end_pfn) {
  930. /*
  931. * Restart at previous pageblock if more
  932. * freepages can be isolated next time.
  933. */
  934. isolate_start_pfn =
  935. block_start_pfn - pageblock_nr_pages;
  936. }
  937. break;
  938. } else if (isolate_start_pfn < block_end_pfn) {
  939. /*
  940. * If isolation failed early, do not continue
  941. * needlessly.
  942. */
  943. break;
  944. }
  945. }
  946. /* __isolate_free_page() does not map the pages */
  947. map_pages(freelist);
  948. /*
  949. * Record where the free scanner will restart next time. Either we
  950. * broke from the loop and set isolate_start_pfn based on the last
  951. * call to isolate_freepages_block(), or we met the migration scanner
  952. * and the loop terminated due to isolate_start_pfn < low_pfn
  953. */
  954. cc->free_pfn = isolate_start_pfn;
  955. }
  956. /*
  957. * This is a migrate-callback that "allocates" freepages by taking pages
  958. * from the isolated freelists in the block we are migrating to.
  959. */
  960. static struct page *compaction_alloc(struct page *migratepage,
  961. unsigned long data,
  962. int **result)
  963. {
  964. struct compact_control *cc = (struct compact_control *)data;
  965. struct page *freepage;
  966. /*
  967. * Isolate free pages if necessary, and if we are not aborting due to
  968. * contention.
  969. */
  970. if (list_empty(&cc->freepages)) {
  971. if (!cc->contended)
  972. isolate_freepages(cc);
  973. if (list_empty(&cc->freepages))
  974. return NULL;
  975. }
  976. freepage = list_entry(cc->freepages.next, struct page, lru);
  977. list_del(&freepage->lru);
  978. cc->nr_freepages--;
  979. return freepage;
  980. }
  981. /*
  982. * This is a migrate-callback that "frees" freepages back to the isolated
  983. * freelist. All pages on the freelist are from the same zone, so there is no
  984. * special handling needed for NUMA.
  985. */
  986. static void compaction_free(struct page *page, unsigned long data)
  987. {
  988. struct compact_control *cc = (struct compact_control *)data;
  989. list_add(&page->lru, &cc->freepages);
  990. cc->nr_freepages++;
  991. }
  992. /* possible outcome of isolate_migratepages */
  993. typedef enum {
  994. ISOLATE_ABORT, /* Abort compaction now */
  995. ISOLATE_NONE, /* No pages isolated, continue scanning */
  996. ISOLATE_SUCCESS, /* Pages isolated, migrate */
  997. } isolate_migrate_t;
  998. /*
  999. * Allow userspace to control policy on scanning the unevictable LRU for
  1000. * compactable pages.
  1001. */
  1002. int sysctl_compact_unevictable_allowed __read_mostly = 1;
  1003. /*
  1004. * Isolate all pages that can be migrated from the first suitable block,
  1005. * starting at the block pointed to by the migrate scanner pfn within
  1006. * compact_control.
  1007. */
  1008. static isolate_migrate_t isolate_migratepages(struct zone *zone,
  1009. struct compact_control *cc)
  1010. {
  1011. unsigned long block_start_pfn;
  1012. unsigned long block_end_pfn;
  1013. unsigned long low_pfn;
  1014. struct page *page;
  1015. const isolate_mode_t isolate_mode =
  1016. (sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) |
  1017. (cc->mode != MIGRATE_SYNC ? ISOLATE_ASYNC_MIGRATE : 0);
  1018. /*
  1019. * Start at where we last stopped, or beginning of the zone as
  1020. * initialized by compact_zone()
  1021. */
  1022. low_pfn = cc->migrate_pfn;
  1023. block_start_pfn = pageblock_start_pfn(low_pfn);
  1024. if (block_start_pfn < zone->zone_start_pfn)
  1025. block_start_pfn = zone->zone_start_pfn;
  1026. /* Only scan within a pageblock boundary */
  1027. block_end_pfn = pageblock_end_pfn(low_pfn);
  1028. /*
  1029. * Iterate over whole pageblocks until we find the first suitable.
  1030. * Do not cross the free scanner.
  1031. */
  1032. for (; block_end_pfn <= cc->free_pfn;
  1033. low_pfn = block_end_pfn,
  1034. block_start_pfn = block_end_pfn,
  1035. block_end_pfn += pageblock_nr_pages) {
  1036. /*
  1037. * This can potentially iterate a massively long zone with
  1038. * many pageblocks unsuitable, so periodically check if we
  1039. * need to schedule, or even abort async compaction.
  1040. */
  1041. if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
  1042. && compact_should_abort(cc))
  1043. break;
  1044. page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
  1045. zone);
  1046. if (!page)
  1047. continue;
  1048. /* If isolation recently failed, do not retry */
  1049. if (!isolation_suitable(cc, page))
  1050. continue;
  1051. /*
  1052. * For async compaction, also only scan in MOVABLE blocks.
  1053. * Async compaction is optimistic to see if the minimum amount
  1054. * of work satisfies the allocation.
  1055. */
  1056. if (cc->mode == MIGRATE_ASYNC &&
  1057. !migrate_async_suitable(get_pageblock_migratetype(page)))
  1058. continue;
  1059. /* Perform the isolation */
  1060. low_pfn = isolate_migratepages_block(cc, low_pfn,
  1061. block_end_pfn, isolate_mode);
  1062. if (!low_pfn || cc->contended) {
  1063. acct_isolated(zone, cc);
  1064. return ISOLATE_ABORT;
  1065. }
  1066. /*
  1067. * Either we isolated something and proceed with migration. Or
  1068. * we failed and compact_zone should decide if we should
  1069. * continue or not.
  1070. */
  1071. break;
  1072. }
  1073. acct_isolated(zone, cc);
  1074. /* Record where migration scanner will be restarted. */
  1075. cc->migrate_pfn = low_pfn;
  1076. return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
  1077. }
  1078. /*
  1079. * order == -1 is expected when compacting via
  1080. * /proc/sys/vm/compact_memory
  1081. */
  1082. static inline bool is_via_compact_memory(int order)
  1083. {
  1084. return order == -1;
  1085. }
  1086. static enum compact_result __compact_finished(struct zone *zone, struct compact_control *cc,
  1087. const int migratetype)
  1088. {
  1089. unsigned int order;
  1090. unsigned long watermark;
  1091. if (cc->contended || fatal_signal_pending(current))
  1092. return COMPACT_CONTENDED;
  1093. /* Compaction run completes if the migrate and free scanner meet */
  1094. if (compact_scanners_met(cc)) {
  1095. /* Let the next compaction start anew. */
  1096. reset_cached_positions(zone);
  1097. /*
  1098. * Mark that the PG_migrate_skip information should be cleared
  1099. * by kswapd when it goes to sleep. kcompactd does not set the
  1100. * flag itself as the decision to be clear should be directly
  1101. * based on an allocation request.
  1102. */
  1103. if (cc->direct_compaction)
  1104. zone->compact_blockskip_flush = true;
  1105. if (cc->whole_zone)
  1106. return COMPACT_COMPLETE;
  1107. else
  1108. return COMPACT_PARTIAL_SKIPPED;
  1109. }
  1110. if (is_via_compact_memory(cc->order))
  1111. return COMPACT_CONTINUE;
  1112. /* Compaction run is not finished if the watermark is not met */
  1113. watermark = low_wmark_pages(zone);
  1114. if (!zone_watermark_ok(zone, cc->order, watermark, cc->classzone_idx,
  1115. cc->alloc_flags))
  1116. return COMPACT_CONTINUE;
  1117. /* Direct compactor: Is a suitable page free? */
  1118. for (order = cc->order; order < MAX_ORDER; order++) {
  1119. struct free_area *area = &zone->free_area[order];
  1120. bool can_steal;
  1121. /* Job done if page is free of the right migratetype */
  1122. if (!list_empty(&area->free_list[migratetype]))
  1123. return COMPACT_PARTIAL;
  1124. #ifdef CONFIG_CMA
  1125. /* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */
  1126. if (migratetype == MIGRATE_MOVABLE &&
  1127. !list_empty(&area->free_list[MIGRATE_CMA]))
  1128. return COMPACT_PARTIAL;
  1129. #endif
  1130. /*
  1131. * Job done if allocation would steal freepages from
  1132. * other migratetype buddy lists.
  1133. */
  1134. if (find_suitable_fallback(area, order, migratetype,
  1135. true, &can_steal) != -1)
  1136. return COMPACT_PARTIAL;
  1137. }
  1138. return COMPACT_NO_SUITABLE_PAGE;
  1139. }
  1140. static enum compact_result compact_finished(struct zone *zone,
  1141. struct compact_control *cc,
  1142. const int migratetype)
  1143. {
  1144. int ret;
  1145. ret = __compact_finished(zone, cc, migratetype);
  1146. trace_mm_compaction_finished(zone, cc->order, ret);
  1147. if (ret == COMPACT_NO_SUITABLE_PAGE)
  1148. ret = COMPACT_CONTINUE;
  1149. return ret;
  1150. }
  1151. /*
  1152. * compaction_suitable: Is this suitable to run compaction on this zone now?
  1153. * Returns
  1154. * COMPACT_SKIPPED - If there are too few free pages for compaction
  1155. * COMPACT_PARTIAL - If the allocation would succeed without compaction
  1156. * COMPACT_CONTINUE - If compaction should run now
  1157. */
  1158. static enum compact_result __compaction_suitable(struct zone *zone, int order,
  1159. unsigned int alloc_flags,
  1160. int classzone_idx,
  1161. unsigned long wmark_target)
  1162. {
  1163. int fragindex;
  1164. unsigned long watermark;
  1165. if (is_via_compact_memory(order))
  1166. return COMPACT_CONTINUE;
  1167. watermark = low_wmark_pages(zone);
  1168. /*
  1169. * If watermarks for high-order allocation are already met, there
  1170. * should be no need for compaction at all.
  1171. */
  1172. if (zone_watermark_ok(zone, order, watermark, classzone_idx,
  1173. alloc_flags))
  1174. return COMPACT_PARTIAL;
  1175. /*
  1176. * Watermarks for order-0 must be met for compaction. Note the 2UL.
  1177. * This is because during migration, copies of pages need to be
  1178. * allocated and for a short time, the footprint is higher
  1179. */
  1180. watermark += (2UL << order);
  1181. if (!__zone_watermark_ok(zone, 0, watermark, classzone_idx,
  1182. alloc_flags, wmark_target))
  1183. return COMPACT_SKIPPED;
  1184. /*
  1185. * fragmentation index determines if allocation failures are due to
  1186. * low memory or external fragmentation
  1187. *
  1188. * index of -1000 would imply allocations might succeed depending on
  1189. * watermarks, but we already failed the high-order watermark check
  1190. * index towards 0 implies failure is due to lack of memory
  1191. * index towards 1000 implies failure is due to fragmentation
  1192. *
  1193. * Only compact if a failure would be due to fragmentation.
  1194. */
  1195. fragindex = fragmentation_index(zone, order);
  1196. if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
  1197. return COMPACT_NOT_SUITABLE_ZONE;
  1198. return COMPACT_CONTINUE;
  1199. }
  1200. enum compact_result compaction_suitable(struct zone *zone, int order,
  1201. unsigned int alloc_flags,
  1202. int classzone_idx)
  1203. {
  1204. enum compact_result ret;
  1205. ret = __compaction_suitable(zone, order, alloc_flags, classzone_idx,
  1206. zone_page_state(zone, NR_FREE_PAGES));
  1207. trace_mm_compaction_suitable(zone, order, ret);
  1208. if (ret == COMPACT_NOT_SUITABLE_ZONE)
  1209. ret = COMPACT_SKIPPED;
  1210. return ret;
  1211. }
  1212. bool compaction_zonelist_suitable(struct alloc_context *ac, int order,
  1213. int alloc_flags)
  1214. {
  1215. struct zone *zone;
  1216. struct zoneref *z;
  1217. /*
  1218. * Make sure at least one zone would pass __compaction_suitable if we continue
  1219. * retrying the reclaim.
  1220. */
  1221. for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
  1222. ac->nodemask) {
  1223. unsigned long available;
  1224. enum compact_result compact_result;
  1225. /*
  1226. * Do not consider all the reclaimable memory because we do not
  1227. * want to trash just for a single high order allocation which
  1228. * is even not guaranteed to appear even if __compaction_suitable
  1229. * is happy about the watermark check.
  1230. */
  1231. available = zone_reclaimable_pages(zone) / order;
  1232. available += zone_page_state_snapshot(zone, NR_FREE_PAGES);
  1233. compact_result = __compaction_suitable(zone, order, alloc_flags,
  1234. ac_classzone_idx(ac), available);
  1235. if (compact_result != COMPACT_SKIPPED &&
  1236. compact_result != COMPACT_NOT_SUITABLE_ZONE)
  1237. return true;
  1238. }
  1239. return false;
  1240. }
  1241. static enum compact_result compact_zone(struct zone *zone, struct compact_control *cc)
  1242. {
  1243. enum compact_result ret;
  1244. unsigned long start_pfn = zone->zone_start_pfn;
  1245. unsigned long end_pfn = zone_end_pfn(zone);
  1246. const int migratetype = gfpflags_to_migratetype(cc->gfp_mask);
  1247. const bool sync = cc->mode != MIGRATE_ASYNC;
  1248. ret = compaction_suitable(zone, cc->order, cc->alloc_flags,
  1249. cc->classzone_idx);
  1250. /* Compaction is likely to fail */
  1251. if (ret == COMPACT_PARTIAL || ret == COMPACT_SKIPPED)
  1252. return ret;
  1253. /* huh, compaction_suitable is returning something unexpected */
  1254. VM_BUG_ON(ret != COMPACT_CONTINUE);
  1255. /*
  1256. * Clear pageblock skip if there were failures recently and compaction
  1257. * is about to be retried after being deferred.
  1258. */
  1259. if (compaction_restarting(zone, cc->order))
  1260. __reset_isolation_suitable(zone);
  1261. /*
  1262. * Setup to move all movable pages to the end of the zone. Used cached
  1263. * information on where the scanners should start but check that it
  1264. * is initialised by ensuring the values are within zone boundaries.
  1265. */
  1266. cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync];
  1267. cc->free_pfn = zone->compact_cached_free_pfn;
  1268. if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) {
  1269. cc->free_pfn = pageblock_start_pfn(end_pfn - 1);
  1270. zone->compact_cached_free_pfn = cc->free_pfn;
  1271. }
  1272. if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) {
  1273. cc->migrate_pfn = start_pfn;
  1274. zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
  1275. zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
  1276. }
  1277. if (cc->migrate_pfn == start_pfn)
  1278. cc->whole_zone = true;
  1279. cc->last_migrated_pfn = 0;
  1280. trace_mm_compaction_begin(start_pfn, cc->migrate_pfn,
  1281. cc->free_pfn, end_pfn, sync);
  1282. migrate_prep_local();
  1283. while ((ret = compact_finished(zone, cc, migratetype)) ==
  1284. COMPACT_CONTINUE) {
  1285. int err;
  1286. switch (isolate_migratepages(zone, cc)) {
  1287. case ISOLATE_ABORT:
  1288. ret = COMPACT_CONTENDED;
  1289. putback_movable_pages(&cc->migratepages);
  1290. cc->nr_migratepages = 0;
  1291. goto out;
  1292. case ISOLATE_NONE:
  1293. /*
  1294. * We haven't isolated and migrated anything, but
  1295. * there might still be unflushed migrations from
  1296. * previous cc->order aligned block.
  1297. */
  1298. goto check_drain;
  1299. case ISOLATE_SUCCESS:
  1300. ;
  1301. }
  1302. err = migrate_pages(&cc->migratepages, compaction_alloc,
  1303. compaction_free, (unsigned long)cc, cc->mode,
  1304. MR_COMPACTION);
  1305. trace_mm_compaction_migratepages(cc->nr_migratepages, err,
  1306. &cc->migratepages);
  1307. /* All pages were either migrated or will be released */
  1308. cc->nr_migratepages = 0;
  1309. if (err) {
  1310. putback_movable_pages(&cc->migratepages);
  1311. /*
  1312. * migrate_pages() may return -ENOMEM when scanners meet
  1313. * and we want compact_finished() to detect it
  1314. */
  1315. if (err == -ENOMEM && !compact_scanners_met(cc)) {
  1316. ret = COMPACT_CONTENDED;
  1317. goto out;
  1318. }
  1319. /*
  1320. * We failed to migrate at least one page in the current
  1321. * order-aligned block, so skip the rest of it.
  1322. */
  1323. if (cc->direct_compaction &&
  1324. (cc->mode == MIGRATE_ASYNC)) {
  1325. cc->migrate_pfn = block_end_pfn(
  1326. cc->migrate_pfn - 1, cc->order);
  1327. /* Draining pcplists is useless in this case */
  1328. cc->last_migrated_pfn = 0;
  1329. }
  1330. }
  1331. check_drain:
  1332. /*
  1333. * Has the migration scanner moved away from the previous
  1334. * cc->order aligned block where we migrated from? If yes,
  1335. * flush the pages that were freed, so that they can merge and
  1336. * compact_finished() can detect immediately if allocation
  1337. * would succeed.
  1338. */
  1339. if (cc->order > 0 && cc->last_migrated_pfn) {
  1340. int cpu;
  1341. unsigned long current_block_start =
  1342. block_start_pfn(cc->migrate_pfn, cc->order);
  1343. if (cc->last_migrated_pfn < current_block_start) {
  1344. cpu = get_cpu();
  1345. lru_add_drain_cpu(cpu);
  1346. drain_local_pages(zone);
  1347. put_cpu();
  1348. /* No more flushing until we migrate again */
  1349. cc->last_migrated_pfn = 0;
  1350. }
  1351. }
  1352. }
  1353. out:
  1354. /*
  1355. * Release free pages and update where the free scanner should restart,
  1356. * so we don't leave any returned pages behind in the next attempt.
  1357. */
  1358. if (cc->nr_freepages > 0) {
  1359. unsigned long free_pfn = release_freepages(&cc->freepages);
  1360. cc->nr_freepages = 0;
  1361. VM_BUG_ON(free_pfn == 0);
  1362. /* The cached pfn is always the first in a pageblock */
  1363. free_pfn = pageblock_start_pfn(free_pfn);
  1364. /*
  1365. * Only go back, not forward. The cached pfn might have been
  1366. * already reset to zone end in compact_finished()
  1367. */
  1368. if (free_pfn > zone->compact_cached_free_pfn)
  1369. zone->compact_cached_free_pfn = free_pfn;
  1370. }
  1371. trace_mm_compaction_end(start_pfn, cc->migrate_pfn,
  1372. cc->free_pfn, end_pfn, sync, ret);
  1373. return ret;
  1374. }
  1375. static enum compact_result compact_zone_order(struct zone *zone, int order,
  1376. gfp_t gfp_mask, enum compact_priority prio,
  1377. unsigned int alloc_flags, int classzone_idx)
  1378. {
  1379. enum compact_result ret;
  1380. struct compact_control cc = {
  1381. .nr_freepages = 0,
  1382. .nr_migratepages = 0,
  1383. .order = order,
  1384. .gfp_mask = gfp_mask,
  1385. .zone = zone,
  1386. .mode = (prio == COMPACT_PRIO_ASYNC) ?
  1387. MIGRATE_ASYNC : MIGRATE_SYNC_LIGHT,
  1388. .alloc_flags = alloc_flags,
  1389. .classzone_idx = classzone_idx,
  1390. .direct_compaction = true,
  1391. };
  1392. INIT_LIST_HEAD(&cc.freepages);
  1393. INIT_LIST_HEAD(&cc.migratepages);
  1394. ret = compact_zone(zone, &cc);
  1395. VM_BUG_ON(!list_empty(&cc.freepages));
  1396. VM_BUG_ON(!list_empty(&cc.migratepages));
  1397. return ret;
  1398. }
  1399. int sysctl_extfrag_threshold = 500;
  1400. /**
  1401. * try_to_compact_pages - Direct compact to satisfy a high-order allocation
  1402. * @gfp_mask: The GFP mask of the current allocation
  1403. * @order: The order of the current allocation
  1404. * @alloc_flags: The allocation flags of the current allocation
  1405. * @ac: The context of current allocation
  1406. * @mode: The migration mode for async, sync light, or sync migration
  1407. *
  1408. * This is the main entry point for direct page compaction.
  1409. */
  1410. enum compact_result try_to_compact_pages(gfp_t gfp_mask, unsigned int order,
  1411. unsigned int alloc_flags, const struct alloc_context *ac,
  1412. enum compact_priority prio)
  1413. {
  1414. int may_enter_fs = gfp_mask & __GFP_FS;
  1415. int may_perform_io = gfp_mask & __GFP_IO;
  1416. struct zoneref *z;
  1417. struct zone *zone;
  1418. enum compact_result rc = COMPACT_SKIPPED;
  1419. /* Check if the GFP flags allow compaction */
  1420. if (!may_enter_fs || !may_perform_io)
  1421. return COMPACT_SKIPPED;
  1422. trace_mm_compaction_try_to_compact_pages(order, gfp_mask, prio);
  1423. /* Compact each zone in the list */
  1424. for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
  1425. ac->nodemask) {
  1426. enum compact_result status;
  1427. if (compaction_deferred(zone, order)) {
  1428. rc = max_t(enum compact_result, COMPACT_DEFERRED, rc);
  1429. continue;
  1430. }
  1431. status = compact_zone_order(zone, order, gfp_mask, prio,
  1432. alloc_flags, ac_classzone_idx(ac));
  1433. rc = max(status, rc);
  1434. /* If a normal allocation would succeed, stop compacting */
  1435. if (zone_watermark_ok(zone, order, low_wmark_pages(zone),
  1436. ac_classzone_idx(ac), alloc_flags)) {
  1437. /*
  1438. * We think the allocation will succeed in this zone,
  1439. * but it is not certain, hence the false. The caller
  1440. * will repeat this with true if allocation indeed
  1441. * succeeds in this zone.
  1442. */
  1443. compaction_defer_reset(zone, order, false);
  1444. break;
  1445. }
  1446. if (prio != COMPACT_PRIO_ASYNC && (status == COMPACT_COMPLETE ||
  1447. status == COMPACT_PARTIAL_SKIPPED))
  1448. /*
  1449. * We think that allocation won't succeed in this zone
  1450. * so we defer compaction there. If it ends up
  1451. * succeeding after all, it will be reset.
  1452. */
  1453. defer_compaction(zone, order);
  1454. /*
  1455. * We might have stopped compacting due to need_resched() in
  1456. * async compaction, or due to a fatal signal detected. In that
  1457. * case do not try further zones
  1458. */
  1459. if ((prio == COMPACT_PRIO_ASYNC && need_resched())
  1460. || fatal_signal_pending(current))
  1461. break;
  1462. }
  1463. return rc;
  1464. }
  1465. /* Compact all zones within a node */
  1466. static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
  1467. {
  1468. int zoneid;
  1469. struct zone *zone;
  1470. for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
  1471. zone = &pgdat->node_zones[zoneid];
  1472. if (!populated_zone(zone))
  1473. continue;
  1474. cc->nr_freepages = 0;
  1475. cc->nr_migratepages = 0;
  1476. cc->zone = zone;
  1477. INIT_LIST_HEAD(&cc->freepages);
  1478. INIT_LIST_HEAD(&cc->migratepages);
  1479. /*
  1480. * When called via /proc/sys/vm/compact_memory
  1481. * this makes sure we compact the whole zone regardless of
  1482. * cached scanner positions.
  1483. */
  1484. if (is_via_compact_memory(cc->order))
  1485. __reset_isolation_suitable(zone);
  1486. if (is_via_compact_memory(cc->order) ||
  1487. !compaction_deferred(zone, cc->order))
  1488. compact_zone(zone, cc);
  1489. VM_BUG_ON(!list_empty(&cc->freepages));
  1490. VM_BUG_ON(!list_empty(&cc->migratepages));
  1491. if (is_via_compact_memory(cc->order))
  1492. continue;
  1493. if (zone_watermark_ok(zone, cc->order,
  1494. low_wmark_pages(zone), 0, 0))
  1495. compaction_defer_reset(zone, cc->order, false);
  1496. }
  1497. }
  1498. void compact_pgdat(pg_data_t *pgdat, int order)
  1499. {
  1500. struct compact_control cc = {
  1501. .order = order,
  1502. .mode = MIGRATE_ASYNC,
  1503. };
  1504. if (!order)
  1505. return;
  1506. __compact_pgdat(pgdat, &cc);
  1507. }
  1508. static void compact_node(int nid)
  1509. {
  1510. struct compact_control cc = {
  1511. .order = -1,
  1512. .mode = MIGRATE_SYNC,
  1513. .ignore_skip_hint = true,
  1514. };
  1515. __compact_pgdat(NODE_DATA(nid), &cc);
  1516. }
  1517. /* Compact all nodes in the system */
  1518. static void compact_nodes(void)
  1519. {
  1520. int nid;
  1521. /* Flush pending updates to the LRU lists */
  1522. lru_add_drain_all();
  1523. for_each_online_node(nid)
  1524. compact_node(nid);
  1525. }
  1526. /* The written value is actually unused, all memory is compacted */
  1527. int sysctl_compact_memory;
  1528. /*
  1529. * This is the entry point for compacting all nodes via
  1530. * /proc/sys/vm/compact_memory
  1531. */
  1532. int sysctl_compaction_handler(struct ctl_table *table, int write,
  1533. void __user *buffer, size_t *length, loff_t *ppos)
  1534. {
  1535. if (write)
  1536. compact_nodes();
  1537. return 0;
  1538. }
  1539. int sysctl_extfrag_handler(struct ctl_table *table, int write,
  1540. void __user *buffer, size_t *length, loff_t *ppos)
  1541. {
  1542. proc_dointvec_minmax(table, write, buffer, length, ppos);
  1543. return 0;
  1544. }
  1545. #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
  1546. static ssize_t sysfs_compact_node(struct device *dev,
  1547. struct device_attribute *attr,
  1548. const char *buf, size_t count)
  1549. {
  1550. int nid = dev->id;
  1551. if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
  1552. /* Flush pending updates to the LRU lists */
  1553. lru_add_drain_all();
  1554. compact_node(nid);
  1555. }
  1556. return count;
  1557. }
  1558. static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
  1559. int compaction_register_node(struct node *node)
  1560. {
  1561. return device_create_file(&node->dev, &dev_attr_compact);
  1562. }
  1563. void compaction_unregister_node(struct node *node)
  1564. {
  1565. return device_remove_file(&node->dev, &dev_attr_compact);
  1566. }
  1567. #endif /* CONFIG_SYSFS && CONFIG_NUMA */
  1568. static inline bool kcompactd_work_requested(pg_data_t *pgdat)
  1569. {
  1570. return pgdat->kcompactd_max_order > 0 || kthread_should_stop();
  1571. }
  1572. static bool kcompactd_node_suitable(pg_data_t *pgdat)
  1573. {
  1574. int zoneid;
  1575. struct zone *zone;
  1576. enum zone_type classzone_idx = pgdat->kcompactd_classzone_idx;
  1577. for (zoneid = 0; zoneid <= classzone_idx; zoneid++) {
  1578. zone = &pgdat->node_zones[zoneid];
  1579. if (!populated_zone(zone))
  1580. continue;
  1581. if (compaction_suitable(zone, pgdat->kcompactd_max_order, 0,
  1582. classzone_idx) == COMPACT_CONTINUE)
  1583. return true;
  1584. }
  1585. return false;
  1586. }
  1587. static void kcompactd_do_work(pg_data_t *pgdat)
  1588. {
  1589. /*
  1590. * With no special task, compact all zones so that a page of requested
  1591. * order is allocatable.
  1592. */
  1593. int zoneid;
  1594. struct zone *zone;
  1595. struct compact_control cc = {
  1596. .order = pgdat->kcompactd_max_order,
  1597. .classzone_idx = pgdat->kcompactd_classzone_idx,
  1598. .mode = MIGRATE_SYNC_LIGHT,
  1599. .ignore_skip_hint = true,
  1600. };
  1601. bool success = false;
  1602. trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order,
  1603. cc.classzone_idx);
  1604. count_vm_event(KCOMPACTD_WAKE);
  1605. for (zoneid = 0; zoneid <= cc.classzone_idx; zoneid++) {
  1606. int status;
  1607. zone = &pgdat->node_zones[zoneid];
  1608. if (!populated_zone(zone))
  1609. continue;
  1610. if (compaction_deferred(zone, cc.order))
  1611. continue;
  1612. if (compaction_suitable(zone, cc.order, 0, zoneid) !=
  1613. COMPACT_CONTINUE)
  1614. continue;
  1615. cc.nr_freepages = 0;
  1616. cc.nr_migratepages = 0;
  1617. cc.zone = zone;
  1618. INIT_LIST_HEAD(&cc.freepages);
  1619. INIT_LIST_HEAD(&cc.migratepages);
  1620. if (kthread_should_stop())
  1621. return;
  1622. status = compact_zone(zone, &cc);
  1623. if (zone_watermark_ok(zone, cc.order, low_wmark_pages(zone),
  1624. cc.classzone_idx, 0)) {
  1625. success = true;
  1626. compaction_defer_reset(zone, cc.order, false);
  1627. } else if (status == COMPACT_PARTIAL_SKIPPED || status == COMPACT_COMPLETE) {
  1628. /*
  1629. * We use sync migration mode here, so we defer like
  1630. * sync direct compaction does.
  1631. */
  1632. defer_compaction(zone, cc.order);
  1633. }
  1634. VM_BUG_ON(!list_empty(&cc.freepages));
  1635. VM_BUG_ON(!list_empty(&cc.migratepages));
  1636. }
  1637. /*
  1638. * Regardless of success, we are done until woken up next. But remember
  1639. * the requested order/classzone_idx in case it was higher/tighter than
  1640. * our current ones
  1641. */
  1642. if (pgdat->kcompactd_max_order <= cc.order)
  1643. pgdat->kcompactd_max_order = 0;
  1644. if (pgdat->kcompactd_classzone_idx >= cc.classzone_idx)
  1645. pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1;
  1646. }
  1647. void wakeup_kcompactd(pg_data_t *pgdat, int order, int classzone_idx)
  1648. {
  1649. if (!order)
  1650. return;
  1651. if (pgdat->kcompactd_max_order < order)
  1652. pgdat->kcompactd_max_order = order;
  1653. if (pgdat->kcompactd_classzone_idx > classzone_idx)
  1654. pgdat->kcompactd_classzone_idx = classzone_idx;
  1655. if (!waitqueue_active(&pgdat->kcompactd_wait))
  1656. return;
  1657. if (!kcompactd_node_suitable(pgdat))
  1658. return;
  1659. trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order,
  1660. classzone_idx);
  1661. wake_up_interruptible(&pgdat->kcompactd_wait);
  1662. }
  1663. /*
  1664. * The background compaction daemon, started as a kernel thread
  1665. * from the init process.
  1666. */
  1667. static int kcompactd(void *p)
  1668. {
  1669. pg_data_t *pgdat = (pg_data_t*)p;
  1670. struct task_struct *tsk = current;
  1671. const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
  1672. if (!cpumask_empty(cpumask))
  1673. set_cpus_allowed_ptr(tsk, cpumask);
  1674. set_freezable();
  1675. pgdat->kcompactd_max_order = 0;
  1676. pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1;
  1677. while (!kthread_should_stop()) {
  1678. trace_mm_compaction_kcompactd_sleep(pgdat->node_id);
  1679. wait_event_freezable(pgdat->kcompactd_wait,
  1680. kcompactd_work_requested(pgdat));
  1681. kcompactd_do_work(pgdat);
  1682. }
  1683. return 0;
  1684. }
  1685. /*
  1686. * This kcompactd start function will be called by init and node-hot-add.
  1687. * On node-hot-add, kcompactd will moved to proper cpus if cpus are hot-added.
  1688. */
  1689. int kcompactd_run(int nid)
  1690. {
  1691. pg_data_t *pgdat = NODE_DATA(nid);
  1692. int ret = 0;
  1693. if (pgdat->kcompactd)
  1694. return 0;
  1695. pgdat->kcompactd = kthread_run(kcompactd, pgdat, "kcompactd%d", nid);
  1696. if (IS_ERR(pgdat->kcompactd)) {
  1697. pr_err("Failed to start kcompactd on node %d\n", nid);
  1698. ret = PTR_ERR(pgdat->kcompactd);
  1699. pgdat->kcompactd = NULL;
  1700. }
  1701. return ret;
  1702. }
  1703. /*
  1704. * Called by memory hotplug when all memory in a node is offlined. Caller must
  1705. * hold mem_hotplug_begin/end().
  1706. */
  1707. void kcompactd_stop(int nid)
  1708. {
  1709. struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd;
  1710. if (kcompactd) {
  1711. kthread_stop(kcompactd);
  1712. NODE_DATA(nid)->kcompactd = NULL;
  1713. }
  1714. }
  1715. /*
  1716. * It's optimal to keep kcompactd on the same CPUs as their memory, but
  1717. * not required for correctness. So if the last cpu in a node goes
  1718. * away, we get changed to run anywhere: as the first one comes back,
  1719. * restore their cpu bindings.
  1720. */
  1721. static int cpu_callback(struct notifier_block *nfb, unsigned long action,
  1722. void *hcpu)
  1723. {
  1724. int nid;
  1725. if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
  1726. for_each_node_state(nid, N_MEMORY) {
  1727. pg_data_t *pgdat = NODE_DATA(nid);
  1728. const struct cpumask *mask;
  1729. mask = cpumask_of_node(pgdat->node_id);
  1730. if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
  1731. /* One of our CPUs online: restore mask */
  1732. set_cpus_allowed_ptr(pgdat->kcompactd, mask);
  1733. }
  1734. }
  1735. return NOTIFY_OK;
  1736. }
  1737. static int __init kcompactd_init(void)
  1738. {
  1739. int nid;
  1740. for_each_node_state(nid, N_MEMORY)
  1741. kcompactd_run(nid);
  1742. hotcpu_notifier(cpu_callback, 0);
  1743. return 0;
  1744. }
  1745. subsys_initcall(kcompactd_init)
  1746. #endif /* CONFIG_COMPACTION */