compaction.c 41 KB

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