compaction.c 59 KB

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