compaction.c 58 KB

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