compaction.c 44 KB

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