compaction.c 48 KB

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