compaction.c 42 KB

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