compaction.c 47 KB

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