compaction.c 49 KB

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