compaction.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508
  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. if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
  666. break;
  667. }
  668. acct_isolated(cc->zone, cc);
  669. return pfn;
  670. }
  671. #endif /* CONFIG_COMPACTION || CONFIG_CMA */
  672. #ifdef CONFIG_COMPACTION
  673. /*
  674. * Based on information in the current compact_control, find blocks
  675. * suitable for isolating free pages from and then isolate them.
  676. */
  677. static void isolate_freepages(struct compact_control *cc)
  678. {
  679. struct zone *zone = cc->zone;
  680. struct page *page;
  681. unsigned long block_start_pfn; /* start of current pageblock */
  682. unsigned long isolate_start_pfn; /* exact pfn we start at */
  683. unsigned long block_end_pfn; /* end of current pageblock */
  684. unsigned long low_pfn; /* lowest pfn scanner is able to scan */
  685. int nr_freepages = cc->nr_freepages;
  686. struct list_head *freelist = &cc->freepages;
  687. /*
  688. * Initialise the free scanner. The starting point is where we last
  689. * successfully isolated from, zone-cached value, or the end of the
  690. * zone when isolating for the first time. For looping we also need
  691. * this pfn aligned down to the pageblock boundary, because we do
  692. * block_start_pfn -= pageblock_nr_pages in the for loop.
  693. * For ending point, take care when isolating in last pageblock of a
  694. * a zone which ends in the middle of a pageblock.
  695. * The low boundary is the end of the pageblock the migration scanner
  696. * is using.
  697. */
  698. isolate_start_pfn = cc->free_pfn;
  699. block_start_pfn = cc->free_pfn & ~(pageblock_nr_pages-1);
  700. block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
  701. zone_end_pfn(zone));
  702. low_pfn = ALIGN(cc->migrate_pfn + 1, pageblock_nr_pages);
  703. /*
  704. * Isolate free pages until enough are available to migrate the
  705. * pages on cc->migratepages. We stop searching if the migrate
  706. * and free page scanners meet or enough free pages are isolated.
  707. */
  708. for (; block_start_pfn >= low_pfn && cc->nr_migratepages > nr_freepages;
  709. block_end_pfn = block_start_pfn,
  710. block_start_pfn -= pageblock_nr_pages,
  711. isolate_start_pfn = block_start_pfn) {
  712. unsigned long isolated;
  713. /*
  714. * This can iterate a massively long zone without finding any
  715. * suitable migration targets, so periodically check if we need
  716. * to schedule, or even abort async compaction.
  717. */
  718. if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
  719. && compact_should_abort(cc))
  720. break;
  721. page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
  722. zone);
  723. if (!page)
  724. continue;
  725. /* Check the block is suitable for migration */
  726. if (!suitable_migration_target(page))
  727. continue;
  728. /* If isolation recently failed, do not retry */
  729. if (!isolation_suitable(cc, page))
  730. continue;
  731. /* Found a block suitable for isolating free pages from. */
  732. isolated = isolate_freepages_block(cc, &isolate_start_pfn,
  733. block_end_pfn, freelist, false);
  734. nr_freepages += isolated;
  735. /*
  736. * Remember where the free scanner should restart next time,
  737. * which is where isolate_freepages_block() left off.
  738. * But if it scanned the whole pageblock, isolate_start_pfn
  739. * now points at block_end_pfn, which is the start of the next
  740. * pageblock.
  741. * In that case we will however want to restart at the start
  742. * of the previous pageblock.
  743. */
  744. cc->free_pfn = (isolate_start_pfn < block_end_pfn) ?
  745. isolate_start_pfn :
  746. block_start_pfn - pageblock_nr_pages;
  747. /*
  748. * Set a flag that we successfully isolated in this pageblock.
  749. * In the next loop iteration, zone->compact_cached_free_pfn
  750. * will not be updated and thus it will effectively contain the
  751. * highest pageblock we isolated pages from.
  752. */
  753. if (isolated)
  754. cc->finished_update_free = true;
  755. /*
  756. * isolate_freepages_block() might have aborted due to async
  757. * compaction being contended
  758. */
  759. if (cc->contended)
  760. break;
  761. }
  762. /* split_free_page does not map the pages */
  763. map_pages(freelist);
  764. /*
  765. * If we crossed the migrate scanner, we want to keep it that way
  766. * so that compact_finished() may detect this
  767. */
  768. if (block_start_pfn < low_pfn)
  769. cc->free_pfn = cc->migrate_pfn;
  770. cc->nr_freepages = nr_freepages;
  771. }
  772. /*
  773. * This is a migrate-callback that "allocates" freepages by taking pages
  774. * from the isolated freelists in the block we are migrating to.
  775. */
  776. static struct page *compaction_alloc(struct page *migratepage,
  777. unsigned long data,
  778. int **result)
  779. {
  780. struct compact_control *cc = (struct compact_control *)data;
  781. struct page *freepage;
  782. /*
  783. * Isolate free pages if necessary, and if we are not aborting due to
  784. * contention.
  785. */
  786. if (list_empty(&cc->freepages)) {
  787. if (!cc->contended)
  788. isolate_freepages(cc);
  789. if (list_empty(&cc->freepages))
  790. return NULL;
  791. }
  792. freepage = list_entry(cc->freepages.next, struct page, lru);
  793. list_del(&freepage->lru);
  794. cc->nr_freepages--;
  795. return freepage;
  796. }
  797. /*
  798. * This is a migrate-callback that "frees" freepages back to the isolated
  799. * freelist. All pages on the freelist are from the same zone, so there is no
  800. * special handling needed for NUMA.
  801. */
  802. static void compaction_free(struct page *page, unsigned long data)
  803. {
  804. struct compact_control *cc = (struct compact_control *)data;
  805. list_add(&page->lru, &cc->freepages);
  806. cc->nr_freepages++;
  807. }
  808. /* possible outcome of isolate_migratepages */
  809. typedef enum {
  810. ISOLATE_ABORT, /* Abort compaction now */
  811. ISOLATE_NONE, /* No pages isolated, continue scanning */
  812. ISOLATE_SUCCESS, /* Pages isolated, migrate */
  813. } isolate_migrate_t;
  814. /*
  815. * Isolate all pages that can be migrated from the first suitable block,
  816. * starting at the block pointed to by the migrate scanner pfn within
  817. * compact_control.
  818. */
  819. static isolate_migrate_t isolate_migratepages(struct zone *zone,
  820. struct compact_control *cc)
  821. {
  822. unsigned long low_pfn, end_pfn;
  823. struct page *page;
  824. const isolate_mode_t isolate_mode =
  825. (cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0);
  826. /*
  827. * Start at where we last stopped, or beginning of the zone as
  828. * initialized by compact_zone()
  829. */
  830. low_pfn = cc->migrate_pfn;
  831. /* Only scan within a pageblock boundary */
  832. end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages);
  833. /*
  834. * Iterate over whole pageblocks until we find the first suitable.
  835. * Do not cross the free scanner.
  836. */
  837. for (; end_pfn <= cc->free_pfn;
  838. low_pfn = end_pfn, end_pfn += pageblock_nr_pages) {
  839. /*
  840. * This can potentially iterate a massively long zone with
  841. * many pageblocks unsuitable, so periodically check if we
  842. * need to schedule, or even abort async compaction.
  843. */
  844. if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
  845. && compact_should_abort(cc))
  846. break;
  847. page = pageblock_pfn_to_page(low_pfn, end_pfn, zone);
  848. if (!page)
  849. continue;
  850. /* If isolation recently failed, do not retry */
  851. if (!isolation_suitable(cc, page))
  852. continue;
  853. /*
  854. * For async compaction, also only scan in MOVABLE blocks.
  855. * Async compaction is optimistic to see if the minimum amount
  856. * of work satisfies the allocation.
  857. */
  858. if (cc->mode == MIGRATE_ASYNC &&
  859. !migrate_async_suitable(get_pageblock_migratetype(page)))
  860. continue;
  861. /* Perform the isolation */
  862. low_pfn = isolate_migratepages_block(cc, low_pfn, end_pfn,
  863. isolate_mode);
  864. if (!low_pfn || cc->contended)
  865. return ISOLATE_ABORT;
  866. /*
  867. * Either we isolated something and proceed with migration. Or
  868. * we failed and compact_zone should decide if we should
  869. * continue or not.
  870. */
  871. break;
  872. }
  873. acct_isolated(zone, cc);
  874. /* Record where migration scanner will be restarted */
  875. cc->migrate_pfn = low_pfn;
  876. return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
  877. }
  878. static int compact_finished(struct zone *zone, struct compact_control *cc,
  879. const int migratetype)
  880. {
  881. unsigned int order;
  882. unsigned long watermark;
  883. if (cc->contended || fatal_signal_pending(current))
  884. return COMPACT_PARTIAL;
  885. /* Compaction run completes if the migrate and free scanner meet */
  886. if (cc->free_pfn <= cc->migrate_pfn) {
  887. /* Let the next compaction start anew. */
  888. zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
  889. zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
  890. zone->compact_cached_free_pfn = zone_end_pfn(zone);
  891. /*
  892. * Mark that the PG_migrate_skip information should be cleared
  893. * by kswapd when it goes to sleep. kswapd does not set the
  894. * flag itself as the decision to be clear should be directly
  895. * based on an allocation request.
  896. */
  897. if (!current_is_kswapd())
  898. zone->compact_blockskip_flush = true;
  899. return COMPACT_COMPLETE;
  900. }
  901. /*
  902. * order == -1 is expected when compacting via
  903. * /proc/sys/vm/compact_memory
  904. */
  905. if (cc->order == -1)
  906. return COMPACT_CONTINUE;
  907. /* Compaction run is not finished if the watermark is not met */
  908. watermark = low_wmark_pages(zone);
  909. watermark += (1 << cc->order);
  910. if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
  911. return COMPACT_CONTINUE;
  912. /* Direct compactor: Is a suitable page free? */
  913. for (order = cc->order; order < MAX_ORDER; order++) {
  914. struct free_area *area = &zone->free_area[order];
  915. /* Job done if page is free of the right migratetype */
  916. if (!list_empty(&area->free_list[migratetype]))
  917. return COMPACT_PARTIAL;
  918. /* Job done if allocation would set block type */
  919. if (cc->order >= pageblock_order && area->nr_free)
  920. return COMPACT_PARTIAL;
  921. }
  922. return COMPACT_CONTINUE;
  923. }
  924. /*
  925. * compaction_suitable: Is this suitable to run compaction on this zone now?
  926. * Returns
  927. * COMPACT_SKIPPED - If there are too few free pages for compaction
  928. * COMPACT_PARTIAL - If the allocation would succeed without compaction
  929. * COMPACT_CONTINUE - If compaction should run now
  930. */
  931. unsigned long compaction_suitable(struct zone *zone, int order)
  932. {
  933. int fragindex;
  934. unsigned long watermark;
  935. /*
  936. * order == -1 is expected when compacting via
  937. * /proc/sys/vm/compact_memory
  938. */
  939. if (order == -1)
  940. return COMPACT_CONTINUE;
  941. /*
  942. * Watermarks for order-0 must be met for compaction. Note the 2UL.
  943. * This is because during migration, copies of pages need to be
  944. * allocated and for a short time, the footprint is higher
  945. */
  946. watermark = low_wmark_pages(zone) + (2UL << order);
  947. if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
  948. return COMPACT_SKIPPED;
  949. /*
  950. * fragmentation index determines if allocation failures are due to
  951. * low memory or external fragmentation
  952. *
  953. * index of -1000 implies allocations might succeed depending on
  954. * watermarks
  955. * index towards 0 implies failure is due to lack of memory
  956. * index towards 1000 implies failure is due to fragmentation
  957. *
  958. * Only compact if a failure would be due to fragmentation.
  959. */
  960. fragindex = fragmentation_index(zone, order);
  961. if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
  962. return COMPACT_SKIPPED;
  963. if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
  964. 0, 0))
  965. return COMPACT_PARTIAL;
  966. return COMPACT_CONTINUE;
  967. }
  968. static int compact_zone(struct zone *zone, struct compact_control *cc)
  969. {
  970. int ret;
  971. unsigned long start_pfn = zone->zone_start_pfn;
  972. unsigned long end_pfn = zone_end_pfn(zone);
  973. const int migratetype = gfpflags_to_migratetype(cc->gfp_mask);
  974. const bool sync = cc->mode != MIGRATE_ASYNC;
  975. ret = compaction_suitable(zone, cc->order);
  976. switch (ret) {
  977. case COMPACT_PARTIAL:
  978. case COMPACT_SKIPPED:
  979. /* Compaction is likely to fail */
  980. return ret;
  981. case COMPACT_CONTINUE:
  982. /* Fall through to compaction */
  983. ;
  984. }
  985. /*
  986. * Clear pageblock skip if there were failures recently and compaction
  987. * is about to be retried after being deferred. kswapd does not do
  988. * this reset as it'll reset the cached information when going to sleep.
  989. */
  990. if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
  991. __reset_isolation_suitable(zone);
  992. /*
  993. * Setup to move all movable pages to the end of the zone. Used cached
  994. * information on where the scanners should start but check that it
  995. * is initialised by ensuring the values are within zone boundaries.
  996. */
  997. cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync];
  998. cc->free_pfn = zone->compact_cached_free_pfn;
  999. if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
  1000. cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
  1001. zone->compact_cached_free_pfn = cc->free_pfn;
  1002. }
  1003. if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
  1004. cc->migrate_pfn = start_pfn;
  1005. zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
  1006. zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
  1007. }
  1008. trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, cc->free_pfn, end_pfn);
  1009. migrate_prep_local();
  1010. while ((ret = compact_finished(zone, cc, migratetype)) ==
  1011. COMPACT_CONTINUE) {
  1012. int err;
  1013. switch (isolate_migratepages(zone, cc)) {
  1014. case ISOLATE_ABORT:
  1015. ret = COMPACT_PARTIAL;
  1016. putback_movable_pages(&cc->migratepages);
  1017. cc->nr_migratepages = 0;
  1018. goto out;
  1019. case ISOLATE_NONE:
  1020. continue;
  1021. case ISOLATE_SUCCESS:
  1022. ;
  1023. }
  1024. err = migrate_pages(&cc->migratepages, compaction_alloc,
  1025. compaction_free, (unsigned long)cc, cc->mode,
  1026. MR_COMPACTION);
  1027. trace_mm_compaction_migratepages(cc->nr_migratepages, err,
  1028. &cc->migratepages);
  1029. /* All pages were either migrated or will be released */
  1030. cc->nr_migratepages = 0;
  1031. if (err) {
  1032. putback_movable_pages(&cc->migratepages);
  1033. /*
  1034. * migrate_pages() may return -ENOMEM when scanners meet
  1035. * and we want compact_finished() to detect it
  1036. */
  1037. if (err == -ENOMEM && cc->free_pfn > cc->migrate_pfn) {
  1038. ret = COMPACT_PARTIAL;
  1039. goto out;
  1040. }
  1041. }
  1042. }
  1043. out:
  1044. /* Release free pages and check accounting */
  1045. cc->nr_freepages -= release_freepages(&cc->freepages);
  1046. VM_BUG_ON(cc->nr_freepages != 0);
  1047. trace_mm_compaction_end(ret);
  1048. return ret;
  1049. }
  1050. static unsigned long compact_zone_order(struct zone *zone, int order,
  1051. gfp_t gfp_mask, enum migrate_mode mode, int *contended)
  1052. {
  1053. unsigned long ret;
  1054. struct compact_control cc = {
  1055. .nr_freepages = 0,
  1056. .nr_migratepages = 0,
  1057. .order = order,
  1058. .gfp_mask = gfp_mask,
  1059. .zone = zone,
  1060. .mode = mode,
  1061. };
  1062. INIT_LIST_HEAD(&cc.freepages);
  1063. INIT_LIST_HEAD(&cc.migratepages);
  1064. ret = compact_zone(zone, &cc);
  1065. VM_BUG_ON(!list_empty(&cc.freepages));
  1066. VM_BUG_ON(!list_empty(&cc.migratepages));
  1067. *contended = cc.contended;
  1068. return ret;
  1069. }
  1070. int sysctl_extfrag_threshold = 500;
  1071. /**
  1072. * try_to_compact_pages - Direct compact to satisfy a high-order allocation
  1073. * @zonelist: The zonelist used for the current allocation
  1074. * @order: The order of the current allocation
  1075. * @gfp_mask: The GFP mask of the current allocation
  1076. * @nodemask: The allowed nodes to allocate from
  1077. * @mode: The migration mode for async, sync light, or sync migration
  1078. * @contended: Return value that determines if compaction was aborted due to
  1079. * need_resched() or lock contention
  1080. * @candidate_zone: Return the zone where we think allocation should succeed
  1081. *
  1082. * This is the main entry point for direct page compaction.
  1083. */
  1084. unsigned long try_to_compact_pages(struct zonelist *zonelist,
  1085. int order, gfp_t gfp_mask, nodemask_t *nodemask,
  1086. enum migrate_mode mode, int *contended,
  1087. struct zone **candidate_zone)
  1088. {
  1089. enum zone_type high_zoneidx = gfp_zone(gfp_mask);
  1090. int may_enter_fs = gfp_mask & __GFP_FS;
  1091. int may_perform_io = gfp_mask & __GFP_IO;
  1092. struct zoneref *z;
  1093. struct zone *zone;
  1094. int rc = COMPACT_DEFERRED;
  1095. int alloc_flags = 0;
  1096. int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */
  1097. *contended = COMPACT_CONTENDED_NONE;
  1098. /* Check if the GFP flags allow compaction */
  1099. if (!order || !may_enter_fs || !may_perform_io)
  1100. return COMPACT_SKIPPED;
  1101. #ifdef CONFIG_CMA
  1102. if (gfpflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
  1103. alloc_flags |= ALLOC_CMA;
  1104. #endif
  1105. /* Compact each zone in the list */
  1106. for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
  1107. nodemask) {
  1108. int status;
  1109. int zone_contended;
  1110. if (compaction_deferred(zone, order))
  1111. continue;
  1112. status = compact_zone_order(zone, order, gfp_mask, mode,
  1113. &zone_contended);
  1114. rc = max(status, rc);
  1115. /*
  1116. * It takes at least one zone that wasn't lock contended
  1117. * to clear all_zones_contended.
  1118. */
  1119. all_zones_contended &= zone_contended;
  1120. /* If a normal allocation would succeed, stop compacting */
  1121. if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
  1122. alloc_flags)) {
  1123. *candidate_zone = zone;
  1124. /*
  1125. * We think the allocation will succeed in this zone,
  1126. * but it is not certain, hence the false. The caller
  1127. * will repeat this with true if allocation indeed
  1128. * succeeds in this zone.
  1129. */
  1130. compaction_defer_reset(zone, order, false);
  1131. /*
  1132. * It is possible that async compaction aborted due to
  1133. * need_resched() and the watermarks were ok thanks to
  1134. * somebody else freeing memory. The allocation can
  1135. * however still fail so we better signal the
  1136. * need_resched() contention anyway (this will not
  1137. * prevent the allocation attempt).
  1138. */
  1139. if (zone_contended == COMPACT_CONTENDED_SCHED)
  1140. *contended = COMPACT_CONTENDED_SCHED;
  1141. goto break_loop;
  1142. }
  1143. if (mode != MIGRATE_ASYNC) {
  1144. /*
  1145. * We think that allocation won't succeed in this zone
  1146. * so we defer compaction there. If it ends up
  1147. * succeeding after all, it will be reset.
  1148. */
  1149. defer_compaction(zone, order);
  1150. }
  1151. /*
  1152. * We might have stopped compacting due to need_resched() in
  1153. * async compaction, or due to a fatal signal detected. In that
  1154. * case do not try further zones and signal need_resched()
  1155. * contention.
  1156. */
  1157. if ((zone_contended == COMPACT_CONTENDED_SCHED)
  1158. || fatal_signal_pending(current)) {
  1159. *contended = COMPACT_CONTENDED_SCHED;
  1160. goto break_loop;
  1161. }
  1162. continue;
  1163. break_loop:
  1164. /*
  1165. * We might not have tried all the zones, so be conservative
  1166. * and assume they are not all lock contended.
  1167. */
  1168. all_zones_contended = 0;
  1169. break;
  1170. }
  1171. /*
  1172. * If at least one zone wasn't deferred or skipped, we report if all
  1173. * zones that were tried were lock contended.
  1174. */
  1175. if (rc > COMPACT_SKIPPED && all_zones_contended)
  1176. *contended = COMPACT_CONTENDED_LOCK;
  1177. return rc;
  1178. }
  1179. /* Compact all zones within a node */
  1180. static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
  1181. {
  1182. int zoneid;
  1183. struct zone *zone;
  1184. for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
  1185. zone = &pgdat->node_zones[zoneid];
  1186. if (!populated_zone(zone))
  1187. continue;
  1188. cc->nr_freepages = 0;
  1189. cc->nr_migratepages = 0;
  1190. cc->zone = zone;
  1191. INIT_LIST_HEAD(&cc->freepages);
  1192. INIT_LIST_HEAD(&cc->migratepages);
  1193. if (cc->order == -1 || !compaction_deferred(zone, cc->order))
  1194. compact_zone(zone, cc);
  1195. if (cc->order > 0) {
  1196. if (zone_watermark_ok(zone, cc->order,
  1197. low_wmark_pages(zone), 0, 0))
  1198. compaction_defer_reset(zone, cc->order, false);
  1199. }
  1200. VM_BUG_ON(!list_empty(&cc->freepages));
  1201. VM_BUG_ON(!list_empty(&cc->migratepages));
  1202. }
  1203. }
  1204. void compact_pgdat(pg_data_t *pgdat, int order)
  1205. {
  1206. struct compact_control cc = {
  1207. .order = order,
  1208. .mode = MIGRATE_ASYNC,
  1209. };
  1210. if (!order)
  1211. return;
  1212. __compact_pgdat(pgdat, &cc);
  1213. }
  1214. static void compact_node(int nid)
  1215. {
  1216. struct compact_control cc = {
  1217. .order = -1,
  1218. .mode = MIGRATE_SYNC,
  1219. .ignore_skip_hint = true,
  1220. };
  1221. __compact_pgdat(NODE_DATA(nid), &cc);
  1222. }
  1223. /* Compact all nodes in the system */
  1224. static void compact_nodes(void)
  1225. {
  1226. int nid;
  1227. /* Flush pending updates to the LRU lists */
  1228. lru_add_drain_all();
  1229. for_each_online_node(nid)
  1230. compact_node(nid);
  1231. }
  1232. /* The written value is actually unused, all memory is compacted */
  1233. int sysctl_compact_memory;
  1234. /* This is the entry point for compacting all nodes via /proc/sys/vm */
  1235. int sysctl_compaction_handler(struct ctl_table *table, int write,
  1236. void __user *buffer, size_t *length, loff_t *ppos)
  1237. {
  1238. if (write)
  1239. compact_nodes();
  1240. return 0;
  1241. }
  1242. int sysctl_extfrag_handler(struct ctl_table *table, int write,
  1243. void __user *buffer, size_t *length, loff_t *ppos)
  1244. {
  1245. proc_dointvec_minmax(table, write, buffer, length, ppos);
  1246. return 0;
  1247. }
  1248. #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
  1249. static ssize_t sysfs_compact_node(struct device *dev,
  1250. struct device_attribute *attr,
  1251. const char *buf, size_t count)
  1252. {
  1253. int nid = dev->id;
  1254. if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
  1255. /* Flush pending updates to the LRU lists */
  1256. lru_add_drain_all();
  1257. compact_node(nid);
  1258. }
  1259. return count;
  1260. }
  1261. static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
  1262. int compaction_register_node(struct node *node)
  1263. {
  1264. return device_create_file(&node->dev, &dev_attr_compact);
  1265. }
  1266. void compaction_unregister_node(struct node *node)
  1267. {
  1268. return device_remove_file(&node->dev, &dev_attr_compact);
  1269. }
  1270. #endif /* CONFIG_SYSFS && CONFIG_NUMA */
  1271. #endif /* CONFIG_COMPACTION */