compaction.c 48 KB

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