compaction.c 48 KB

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