compaction.c 47 KB

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