compaction.c 47 KB

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