compaction.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  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 "internal.h"
  18. #if defined CONFIG_COMPACTION || defined CONFIG_CMA
  19. #define CREATE_TRACE_POINTS
  20. #include <trace/events/compaction.h>
  21. static unsigned long release_freepages(struct list_head *freelist)
  22. {
  23. struct page *page, *next;
  24. unsigned long count = 0;
  25. list_for_each_entry_safe(page, next, freelist, lru) {
  26. list_del(&page->lru);
  27. __free_page(page);
  28. count++;
  29. }
  30. return count;
  31. }
  32. static void map_pages(struct list_head *list)
  33. {
  34. struct page *page;
  35. list_for_each_entry(page, list, lru) {
  36. arch_alloc_page(page, 0);
  37. kernel_map_pages(page, 1, 1);
  38. }
  39. }
  40. static inline bool migrate_async_suitable(int migratetype)
  41. {
  42. return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
  43. }
  44. #ifdef CONFIG_COMPACTION
  45. /* Returns true if the pageblock should be scanned for pages to isolate. */
  46. static inline bool isolation_suitable(struct compact_control *cc,
  47. struct page *page)
  48. {
  49. if (cc->ignore_skip_hint)
  50. return true;
  51. return !get_pageblock_skip(page);
  52. }
  53. /*
  54. * This function is called to clear all cached information on pageblocks that
  55. * should be skipped for page isolation when the migrate and free page scanner
  56. * meet.
  57. */
  58. static void __reset_isolation_suitable(struct zone *zone)
  59. {
  60. unsigned long start_pfn = zone->zone_start_pfn;
  61. unsigned long end_pfn = zone->zone_start_pfn + zone->spanned_pages;
  62. unsigned long pfn;
  63. zone->compact_cached_migrate_pfn = start_pfn;
  64. zone->compact_cached_free_pfn = end_pfn;
  65. zone->compact_blockskip_flush = false;
  66. /* Walk the zone and mark every pageblock as suitable for isolation */
  67. for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
  68. struct page *page;
  69. cond_resched();
  70. if (!pfn_valid(pfn))
  71. continue;
  72. page = pfn_to_page(pfn);
  73. if (zone != page_zone(page))
  74. continue;
  75. clear_pageblock_skip(page);
  76. }
  77. }
  78. void reset_isolation_suitable(pg_data_t *pgdat)
  79. {
  80. int zoneid;
  81. for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
  82. struct zone *zone = &pgdat->node_zones[zoneid];
  83. if (!populated_zone(zone))
  84. continue;
  85. /* Only flush if a full compaction finished recently */
  86. if (zone->compact_blockskip_flush)
  87. __reset_isolation_suitable(zone);
  88. }
  89. }
  90. /*
  91. * If no pages were isolated then mark this pageblock to be skipped in the
  92. * future. The information is later cleared by __reset_isolation_suitable().
  93. */
  94. static void update_pageblock_skip(struct compact_control *cc,
  95. struct page *page, unsigned long nr_isolated,
  96. bool migrate_scanner)
  97. {
  98. struct zone *zone = cc->zone;
  99. if (!page)
  100. return;
  101. if (!nr_isolated) {
  102. unsigned long pfn = page_to_pfn(page);
  103. set_pageblock_skip(page);
  104. /* Update where compaction should restart */
  105. if (migrate_scanner) {
  106. if (!cc->finished_update_migrate &&
  107. pfn > zone->compact_cached_migrate_pfn)
  108. zone->compact_cached_migrate_pfn = pfn;
  109. } else {
  110. if (!cc->finished_update_free &&
  111. pfn < zone->compact_cached_free_pfn)
  112. zone->compact_cached_free_pfn = pfn;
  113. }
  114. }
  115. }
  116. #else
  117. static inline bool isolation_suitable(struct compact_control *cc,
  118. struct page *page)
  119. {
  120. return true;
  121. }
  122. static void update_pageblock_skip(struct compact_control *cc,
  123. struct page *page, unsigned long nr_isolated,
  124. bool migrate_scanner)
  125. {
  126. }
  127. #endif /* CONFIG_COMPACTION */
  128. static inline bool should_release_lock(spinlock_t *lock)
  129. {
  130. return need_resched() || spin_is_contended(lock);
  131. }
  132. /*
  133. * Compaction requires the taking of some coarse locks that are potentially
  134. * very heavily contended. Check if the process needs to be scheduled or
  135. * if the lock is contended. For async compaction, back out in the event
  136. * if contention is severe. For sync compaction, schedule.
  137. *
  138. * Returns true if the lock is held.
  139. * Returns false if the lock is released and compaction should abort
  140. */
  141. static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags,
  142. bool locked, struct compact_control *cc)
  143. {
  144. if (should_release_lock(lock)) {
  145. if (locked) {
  146. spin_unlock_irqrestore(lock, *flags);
  147. locked = false;
  148. }
  149. /* async aborts if taking too long or contended */
  150. if (!cc->sync) {
  151. cc->contended = true;
  152. return false;
  153. }
  154. cond_resched();
  155. }
  156. if (!locked)
  157. spin_lock_irqsave(lock, *flags);
  158. return true;
  159. }
  160. static inline bool compact_trylock_irqsave(spinlock_t *lock,
  161. unsigned long *flags, struct compact_control *cc)
  162. {
  163. return compact_checklock_irqsave(lock, flags, false, cc);
  164. }
  165. /* Returns true if the page is within a block suitable for migration to */
  166. static bool suitable_migration_target(struct page *page)
  167. {
  168. int migratetype = get_pageblock_migratetype(page);
  169. /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
  170. if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
  171. return false;
  172. /* If the page is a large free page, then allow migration */
  173. if (PageBuddy(page) && page_order(page) >= pageblock_order)
  174. return true;
  175. /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
  176. if (migrate_async_suitable(migratetype))
  177. return true;
  178. /* Otherwise skip the block */
  179. return false;
  180. }
  181. static void compact_capture_page(struct compact_control *cc)
  182. {
  183. unsigned long flags;
  184. int mtype, mtype_low, mtype_high;
  185. if (!cc->page || *cc->page)
  186. return;
  187. /*
  188. * For MIGRATE_MOVABLE allocations we capture a suitable page ASAP
  189. * regardless of the migratetype of the freelist is is captured from.
  190. * This is fine because the order for a high-order MIGRATE_MOVABLE
  191. * allocation is typically at least a pageblock size and overall
  192. * fragmentation is not impaired. Other allocation types must
  193. * capture pages from their own migratelist because otherwise they
  194. * could pollute other pageblocks like MIGRATE_MOVABLE with
  195. * difficult to move pages and making fragmentation worse overall.
  196. */
  197. if (cc->migratetype == MIGRATE_MOVABLE) {
  198. mtype_low = 0;
  199. mtype_high = MIGRATE_PCPTYPES;
  200. } else {
  201. mtype_low = cc->migratetype;
  202. mtype_high = cc->migratetype + 1;
  203. }
  204. /* Speculatively examine the free lists without zone lock */
  205. for (mtype = mtype_low; mtype < mtype_high; mtype++) {
  206. int order;
  207. for (order = cc->order; order < MAX_ORDER; order++) {
  208. struct page *page;
  209. struct free_area *area;
  210. area = &(cc->zone->free_area[order]);
  211. if (list_empty(&area->free_list[mtype]))
  212. continue;
  213. /* Take the lock and attempt capture of the page */
  214. if (!compact_trylock_irqsave(&cc->zone->lock, &flags, cc))
  215. return;
  216. if (!list_empty(&area->free_list[mtype])) {
  217. page = list_entry(area->free_list[mtype].next,
  218. struct page, lru);
  219. if (capture_free_page(page, cc->order, mtype)) {
  220. spin_unlock_irqrestore(&cc->zone->lock,
  221. flags);
  222. *cc->page = page;
  223. return;
  224. }
  225. }
  226. spin_unlock_irqrestore(&cc->zone->lock, flags);
  227. }
  228. }
  229. }
  230. /*
  231. * Isolate free pages onto a private freelist. Caller must hold zone->lock.
  232. * If @strict is true, will abort returning 0 on any invalid PFNs or non-free
  233. * pages inside of the pageblock (even though it may still end up isolating
  234. * some pages).
  235. */
  236. static unsigned long isolate_freepages_block(struct compact_control *cc,
  237. unsigned long blockpfn,
  238. unsigned long end_pfn,
  239. struct list_head *freelist,
  240. bool strict)
  241. {
  242. int nr_scanned = 0, total_isolated = 0;
  243. struct page *cursor, *valid_page = NULL;
  244. unsigned long nr_strict_required = end_pfn - blockpfn;
  245. unsigned long flags;
  246. bool locked = false;
  247. cursor = pfn_to_page(blockpfn);
  248. /* Isolate free pages. */
  249. for (; blockpfn < end_pfn; blockpfn++, cursor++) {
  250. int isolated, i;
  251. struct page *page = cursor;
  252. nr_scanned++;
  253. if (!pfn_valid_within(blockpfn))
  254. continue;
  255. if (!valid_page)
  256. valid_page = page;
  257. if (!PageBuddy(page))
  258. continue;
  259. /*
  260. * The zone lock must be held to isolate freepages.
  261. * Unfortunately this is a very coarse lock and can be
  262. * heavily contended if there are parallel allocations
  263. * or parallel compactions. For async compaction do not
  264. * spin on the lock and we acquire the lock as late as
  265. * possible.
  266. */
  267. locked = compact_checklock_irqsave(&cc->zone->lock, &flags,
  268. locked, cc);
  269. if (!locked)
  270. break;
  271. /* Recheck this is a suitable migration target under lock */
  272. if (!strict && !suitable_migration_target(page))
  273. break;
  274. /* Recheck this is a buddy page under lock */
  275. if (!PageBuddy(page))
  276. continue;
  277. /* Found a free page, break it into order-0 pages */
  278. isolated = split_free_page(page);
  279. if (!isolated && strict)
  280. break;
  281. total_isolated += isolated;
  282. for (i = 0; i < isolated; i++) {
  283. list_add(&page->lru, freelist);
  284. page++;
  285. }
  286. /* If a page was split, advance to the end of it */
  287. if (isolated) {
  288. blockpfn += isolated - 1;
  289. cursor += isolated - 1;
  290. }
  291. }
  292. trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
  293. /*
  294. * If strict isolation is requested by CMA then check that all the
  295. * pages requested were isolated. If there were any failures, 0 is
  296. * returned and CMA will fail.
  297. */
  298. if (strict && nr_strict_required > total_isolated)
  299. total_isolated = 0;
  300. if (locked)
  301. spin_unlock_irqrestore(&cc->zone->lock, flags);
  302. /* Update the pageblock-skip if the whole pageblock was scanned */
  303. if (blockpfn == end_pfn)
  304. update_pageblock_skip(cc, valid_page, total_isolated, false);
  305. return total_isolated;
  306. }
  307. /**
  308. * isolate_freepages_range() - isolate free pages.
  309. * @start_pfn: The first PFN to start isolating.
  310. * @end_pfn: The one-past-last PFN.
  311. *
  312. * Non-free pages, invalid PFNs, or zone boundaries within the
  313. * [start_pfn, end_pfn) range are considered errors, cause function to
  314. * undo its actions and return zero.
  315. *
  316. * Otherwise, function returns one-past-the-last PFN of isolated page
  317. * (which may be greater then end_pfn if end fell in a middle of
  318. * a free page).
  319. */
  320. unsigned long
  321. isolate_freepages_range(struct compact_control *cc,
  322. unsigned long start_pfn, unsigned long end_pfn)
  323. {
  324. unsigned long isolated, pfn, block_end_pfn;
  325. LIST_HEAD(freelist);
  326. for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
  327. if (!pfn_valid(pfn) || cc->zone != page_zone(pfn_to_page(pfn)))
  328. break;
  329. /*
  330. * On subsequent iterations ALIGN() is actually not needed,
  331. * but we keep it that we not to complicate the code.
  332. */
  333. block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
  334. block_end_pfn = min(block_end_pfn, end_pfn);
  335. isolated = isolate_freepages_block(cc, pfn, block_end_pfn,
  336. &freelist, true);
  337. /*
  338. * In strict mode, isolate_freepages_block() returns 0 if
  339. * there are any holes in the block (ie. invalid PFNs or
  340. * non-free pages).
  341. */
  342. if (!isolated)
  343. break;
  344. /*
  345. * If we managed to isolate pages, it is always (1 << n) *
  346. * pageblock_nr_pages for some non-negative n. (Max order
  347. * page may span two pageblocks).
  348. */
  349. }
  350. /* split_free_page does not map the pages */
  351. map_pages(&freelist);
  352. if (pfn < end_pfn) {
  353. /* Loop terminated early, cleanup. */
  354. release_freepages(&freelist);
  355. return 0;
  356. }
  357. /* We don't use freelists for anything. */
  358. return pfn;
  359. }
  360. /* Update the number of anon and file isolated pages in the zone */
  361. static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc)
  362. {
  363. struct page *page;
  364. unsigned int count[2] = { 0, };
  365. list_for_each_entry(page, &cc->migratepages, lru)
  366. count[!!page_is_file_cache(page)]++;
  367. /* If locked we can use the interrupt unsafe versions */
  368. if (locked) {
  369. __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
  370. __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
  371. } else {
  372. mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
  373. mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
  374. }
  375. }
  376. /* Similar to reclaim, but different enough that they don't share logic */
  377. static bool too_many_isolated(struct zone *zone)
  378. {
  379. unsigned long active, inactive, isolated;
  380. inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
  381. zone_page_state(zone, NR_INACTIVE_ANON);
  382. active = zone_page_state(zone, NR_ACTIVE_FILE) +
  383. zone_page_state(zone, NR_ACTIVE_ANON);
  384. isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
  385. zone_page_state(zone, NR_ISOLATED_ANON);
  386. return isolated > (inactive + active) / 2;
  387. }
  388. /**
  389. * isolate_migratepages_range() - isolate all migrate-able pages in range.
  390. * @zone: Zone pages are in.
  391. * @cc: Compaction control structure.
  392. * @low_pfn: The first PFN of the range.
  393. * @end_pfn: The one-past-the-last PFN of the range.
  394. * @unevictable: true if it allows to isolate unevictable pages
  395. *
  396. * Isolate all pages that can be migrated from the range specified by
  397. * [low_pfn, end_pfn). Returns zero if there is a fatal signal
  398. * pending), otherwise PFN of the first page that was not scanned
  399. * (which may be both less, equal to or more then end_pfn).
  400. *
  401. * Assumes that cc->migratepages is empty and cc->nr_migratepages is
  402. * zero.
  403. *
  404. * Apart from cc->migratepages and cc->nr_migratetypes this function
  405. * does not modify any cc's fields, in particular it does not modify
  406. * (or read for that matter) cc->migrate_pfn.
  407. */
  408. unsigned long
  409. isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
  410. unsigned long low_pfn, unsigned long end_pfn, bool unevictable)
  411. {
  412. unsigned long last_pageblock_nr = 0, pageblock_nr;
  413. unsigned long nr_scanned = 0, nr_isolated = 0;
  414. struct list_head *migratelist = &cc->migratepages;
  415. isolate_mode_t mode = 0;
  416. struct lruvec *lruvec;
  417. unsigned long flags;
  418. bool locked = false;
  419. struct page *page = NULL, *valid_page = NULL;
  420. /*
  421. * Ensure that there are not too many pages isolated from the LRU
  422. * list by either parallel reclaimers or compaction. If there are,
  423. * delay for some time until fewer pages are isolated
  424. */
  425. while (unlikely(too_many_isolated(zone))) {
  426. /* async migration should just abort */
  427. if (!cc->sync)
  428. return 0;
  429. congestion_wait(BLK_RW_ASYNC, HZ/10);
  430. if (fatal_signal_pending(current))
  431. return 0;
  432. }
  433. /* Time to isolate some pages for migration */
  434. cond_resched();
  435. for (; low_pfn < end_pfn; low_pfn++) {
  436. /* give a chance to irqs before checking need_resched() */
  437. if (locked && !((low_pfn+1) % SWAP_CLUSTER_MAX)) {
  438. if (should_release_lock(&zone->lru_lock)) {
  439. spin_unlock_irqrestore(&zone->lru_lock, flags);
  440. locked = false;
  441. }
  442. }
  443. /*
  444. * migrate_pfn does not necessarily start aligned to a
  445. * pageblock. Ensure that pfn_valid is called when moving
  446. * into a new MAX_ORDER_NR_PAGES range in case of large
  447. * memory holes within the zone
  448. */
  449. if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
  450. if (!pfn_valid(low_pfn)) {
  451. low_pfn += MAX_ORDER_NR_PAGES - 1;
  452. continue;
  453. }
  454. }
  455. if (!pfn_valid_within(low_pfn))
  456. continue;
  457. nr_scanned++;
  458. /*
  459. * Get the page and ensure the page is within the same zone.
  460. * See the comment in isolate_freepages about overlapping
  461. * nodes. It is deliberate that the new zone lock is not taken
  462. * as memory compaction should not move pages between nodes.
  463. */
  464. page = pfn_to_page(low_pfn);
  465. if (page_zone(page) != zone)
  466. continue;
  467. if (!valid_page)
  468. valid_page = page;
  469. /* If isolation recently failed, do not retry */
  470. pageblock_nr = low_pfn >> pageblock_order;
  471. if (!isolation_suitable(cc, page))
  472. goto next_pageblock;
  473. /* Skip if free */
  474. if (PageBuddy(page))
  475. continue;
  476. /*
  477. * For async migration, also only scan in MOVABLE blocks. Async
  478. * migration is optimistic to see if the minimum amount of work
  479. * satisfies the allocation
  480. */
  481. if (!cc->sync && last_pageblock_nr != pageblock_nr &&
  482. !migrate_async_suitable(get_pageblock_migratetype(page))) {
  483. cc->finished_update_migrate = true;
  484. goto next_pageblock;
  485. }
  486. /* Check may be lockless but that's ok as we recheck later */
  487. if (!PageLRU(page))
  488. continue;
  489. /*
  490. * PageLRU is set. lru_lock normally excludes isolation
  491. * splitting and collapsing (collapsing has already happened
  492. * if PageLRU is set) but the lock is not necessarily taken
  493. * here and it is wasteful to take it just to check transhuge.
  494. * Check TransHuge without lock and skip the whole pageblock if
  495. * it's either a transhuge or hugetlbfs page, as calling
  496. * compound_order() without preventing THP from splitting the
  497. * page underneath us may return surprising results.
  498. */
  499. if (PageTransHuge(page)) {
  500. if (!locked)
  501. goto next_pageblock;
  502. low_pfn += (1 << compound_order(page)) - 1;
  503. continue;
  504. }
  505. /* Check if it is ok to still hold the lock */
  506. locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
  507. locked, cc);
  508. if (!locked || fatal_signal_pending(current))
  509. break;
  510. /* Recheck PageLRU and PageTransHuge under lock */
  511. if (!PageLRU(page))
  512. continue;
  513. if (PageTransHuge(page)) {
  514. low_pfn += (1 << compound_order(page)) - 1;
  515. continue;
  516. }
  517. if (!cc->sync)
  518. mode |= ISOLATE_ASYNC_MIGRATE;
  519. if (unevictable)
  520. mode |= ISOLATE_UNEVICTABLE;
  521. lruvec = mem_cgroup_page_lruvec(page, zone);
  522. /* Try isolate the page */
  523. if (__isolate_lru_page(page, mode) != 0)
  524. continue;
  525. VM_BUG_ON(PageTransCompound(page));
  526. /* Successfully isolated */
  527. cc->finished_update_migrate = true;
  528. del_page_from_lru_list(page, lruvec, page_lru(page));
  529. list_add(&page->lru, migratelist);
  530. cc->nr_migratepages++;
  531. nr_isolated++;
  532. /* Avoid isolating too much */
  533. if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
  534. ++low_pfn;
  535. break;
  536. }
  537. continue;
  538. next_pageblock:
  539. low_pfn += pageblock_nr_pages;
  540. low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
  541. last_pageblock_nr = pageblock_nr;
  542. }
  543. acct_isolated(zone, locked, cc);
  544. if (locked)
  545. spin_unlock_irqrestore(&zone->lru_lock, flags);
  546. /* Update the pageblock-skip if the whole pageblock was scanned */
  547. if (low_pfn == end_pfn)
  548. update_pageblock_skip(cc, valid_page, nr_isolated, true);
  549. trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
  550. return low_pfn;
  551. }
  552. #endif /* CONFIG_COMPACTION || CONFIG_CMA */
  553. #ifdef CONFIG_COMPACTION
  554. /*
  555. * Based on information in the current compact_control, find blocks
  556. * suitable for isolating free pages from and then isolate them.
  557. */
  558. static void isolate_freepages(struct zone *zone,
  559. struct compact_control *cc)
  560. {
  561. struct page *page;
  562. unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
  563. int nr_freepages = cc->nr_freepages;
  564. struct list_head *freelist = &cc->freepages;
  565. /*
  566. * Initialise the free scanner. The starting point is where we last
  567. * scanned from (or the end of the zone if starting). The low point
  568. * is the end of the pageblock the migration scanner is using.
  569. */
  570. pfn = cc->free_pfn;
  571. low_pfn = cc->migrate_pfn + pageblock_nr_pages;
  572. /*
  573. * Take care that if the migration scanner is at the end of the zone
  574. * that the free scanner does not accidentally move to the next zone
  575. * in the next isolation cycle.
  576. */
  577. high_pfn = min(low_pfn, pfn);
  578. zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
  579. /*
  580. * Isolate free pages until enough are available to migrate the
  581. * pages on cc->migratepages. We stop searching if the migrate
  582. * and free page scanners meet or enough free pages are isolated.
  583. */
  584. for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
  585. pfn -= pageblock_nr_pages) {
  586. unsigned long isolated;
  587. if (!pfn_valid(pfn))
  588. continue;
  589. /*
  590. * Check for overlapping nodes/zones. It's possible on some
  591. * configurations to have a setup like
  592. * node0 node1 node0
  593. * i.e. it's possible that all pages within a zones range of
  594. * pages do not belong to a single zone.
  595. */
  596. page = pfn_to_page(pfn);
  597. if (page_zone(page) != zone)
  598. continue;
  599. /* Check the block is suitable for migration */
  600. if (!suitable_migration_target(page))
  601. continue;
  602. /* If isolation recently failed, do not retry */
  603. if (!isolation_suitable(cc, page))
  604. continue;
  605. /* Found a block suitable for isolating free pages from */
  606. isolated = 0;
  607. /*
  608. * As pfn may not start aligned, pfn+pageblock_nr_page
  609. * may cross a MAX_ORDER_NR_PAGES boundary and miss
  610. * a pfn_valid check. Ensure isolate_freepages_block()
  611. * only scans within a pageblock
  612. */
  613. end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
  614. end_pfn = min(end_pfn, zone_end_pfn);
  615. isolated = isolate_freepages_block(cc, pfn, end_pfn,
  616. freelist, false);
  617. nr_freepages += isolated;
  618. /*
  619. * Record the highest PFN we isolated pages from. When next
  620. * looking for free pages, the search will restart here as
  621. * page migration may have returned some pages to the allocator
  622. */
  623. if (isolated) {
  624. cc->finished_update_free = true;
  625. high_pfn = max(high_pfn, pfn);
  626. }
  627. }
  628. /* split_free_page does not map the pages */
  629. map_pages(freelist);
  630. cc->free_pfn = high_pfn;
  631. cc->nr_freepages = nr_freepages;
  632. }
  633. /*
  634. * This is a migrate-callback that "allocates" freepages by taking pages
  635. * from the isolated freelists in the block we are migrating to.
  636. */
  637. static struct page *compaction_alloc(struct page *migratepage,
  638. unsigned long data,
  639. int **result)
  640. {
  641. struct compact_control *cc = (struct compact_control *)data;
  642. struct page *freepage;
  643. /* Isolate free pages if necessary */
  644. if (list_empty(&cc->freepages)) {
  645. isolate_freepages(cc->zone, cc);
  646. if (list_empty(&cc->freepages))
  647. return NULL;
  648. }
  649. freepage = list_entry(cc->freepages.next, struct page, lru);
  650. list_del(&freepage->lru);
  651. cc->nr_freepages--;
  652. return freepage;
  653. }
  654. /*
  655. * We cannot control nr_migratepages and nr_freepages fully when migration is
  656. * running as migrate_pages() has no knowledge of compact_control. When
  657. * migration is complete, we count the number of pages on the lists by hand.
  658. */
  659. static void update_nr_listpages(struct compact_control *cc)
  660. {
  661. int nr_migratepages = 0;
  662. int nr_freepages = 0;
  663. struct page *page;
  664. list_for_each_entry(page, &cc->migratepages, lru)
  665. nr_migratepages++;
  666. list_for_each_entry(page, &cc->freepages, lru)
  667. nr_freepages++;
  668. cc->nr_migratepages = nr_migratepages;
  669. cc->nr_freepages = nr_freepages;
  670. }
  671. /* possible outcome of isolate_migratepages */
  672. typedef enum {
  673. ISOLATE_ABORT, /* Abort compaction now */
  674. ISOLATE_NONE, /* No pages isolated, continue scanning */
  675. ISOLATE_SUCCESS, /* Pages isolated, migrate */
  676. } isolate_migrate_t;
  677. /*
  678. * Isolate all pages that can be migrated from the block pointed to by
  679. * the migrate scanner within compact_control.
  680. */
  681. static isolate_migrate_t isolate_migratepages(struct zone *zone,
  682. struct compact_control *cc)
  683. {
  684. unsigned long low_pfn, end_pfn;
  685. /* Do not scan outside zone boundaries */
  686. low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
  687. /* Only scan within a pageblock boundary */
  688. end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
  689. /* Do not cross the free scanner or scan within a memory hole */
  690. if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
  691. cc->migrate_pfn = end_pfn;
  692. return ISOLATE_NONE;
  693. }
  694. /* Perform the isolation */
  695. low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn, false);
  696. if (!low_pfn || cc->contended)
  697. return ISOLATE_ABORT;
  698. cc->migrate_pfn = low_pfn;
  699. return ISOLATE_SUCCESS;
  700. }
  701. static int compact_finished(struct zone *zone,
  702. struct compact_control *cc)
  703. {
  704. unsigned long watermark;
  705. if (fatal_signal_pending(current))
  706. return COMPACT_PARTIAL;
  707. /* Compaction run completes if the migrate and free scanner meet */
  708. if (cc->free_pfn <= cc->migrate_pfn) {
  709. /*
  710. * Mark that the PG_migrate_skip information should be cleared
  711. * by kswapd when it goes to sleep. kswapd does not set the
  712. * flag itself as the decision to be clear should be directly
  713. * based on an allocation request.
  714. */
  715. if (!current_is_kswapd())
  716. zone->compact_blockskip_flush = true;
  717. return COMPACT_COMPLETE;
  718. }
  719. /*
  720. * order == -1 is expected when compacting via
  721. * /proc/sys/vm/compact_memory
  722. */
  723. if (cc->order == -1)
  724. return COMPACT_CONTINUE;
  725. /* Compaction run is not finished if the watermark is not met */
  726. watermark = low_wmark_pages(zone);
  727. watermark += (1 << cc->order);
  728. if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
  729. return COMPACT_CONTINUE;
  730. /* Direct compactor: Is a suitable page free? */
  731. if (cc->page) {
  732. /* Was a suitable page captured? */
  733. if (*cc->page)
  734. return COMPACT_PARTIAL;
  735. } else {
  736. unsigned int order;
  737. for (order = cc->order; order < MAX_ORDER; order++) {
  738. struct free_area *area = &zone->free_area[cc->order];
  739. /* Job done if page is free of the right migratetype */
  740. if (!list_empty(&area->free_list[cc->migratetype]))
  741. return COMPACT_PARTIAL;
  742. /* Job done if allocation would set block type */
  743. if (cc->order >= pageblock_order && area->nr_free)
  744. return COMPACT_PARTIAL;
  745. }
  746. }
  747. return COMPACT_CONTINUE;
  748. }
  749. /*
  750. * compaction_suitable: Is this suitable to run compaction on this zone now?
  751. * Returns
  752. * COMPACT_SKIPPED - If there are too few free pages for compaction
  753. * COMPACT_PARTIAL - If the allocation would succeed without compaction
  754. * COMPACT_CONTINUE - If compaction should run now
  755. */
  756. unsigned long compaction_suitable(struct zone *zone, int order)
  757. {
  758. int fragindex;
  759. unsigned long watermark;
  760. /*
  761. * order == -1 is expected when compacting via
  762. * /proc/sys/vm/compact_memory
  763. */
  764. if (order == -1)
  765. return COMPACT_CONTINUE;
  766. /*
  767. * Watermarks for order-0 must be met for compaction. Note the 2UL.
  768. * This is because during migration, copies of pages need to be
  769. * allocated and for a short time, the footprint is higher
  770. */
  771. watermark = low_wmark_pages(zone) + (2UL << order);
  772. if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
  773. return COMPACT_SKIPPED;
  774. /*
  775. * fragmentation index determines if allocation failures are due to
  776. * low memory or external fragmentation
  777. *
  778. * index of -1000 implies allocations might succeed depending on
  779. * watermarks
  780. * index towards 0 implies failure is due to lack of memory
  781. * index towards 1000 implies failure is due to fragmentation
  782. *
  783. * Only compact if a failure would be due to fragmentation.
  784. */
  785. fragindex = fragmentation_index(zone, order);
  786. if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
  787. return COMPACT_SKIPPED;
  788. if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
  789. 0, 0))
  790. return COMPACT_PARTIAL;
  791. return COMPACT_CONTINUE;
  792. }
  793. static int compact_zone(struct zone *zone, struct compact_control *cc)
  794. {
  795. int ret;
  796. unsigned long start_pfn = zone->zone_start_pfn;
  797. unsigned long end_pfn = zone->zone_start_pfn + zone->spanned_pages;
  798. ret = compaction_suitable(zone, cc->order);
  799. switch (ret) {
  800. case COMPACT_PARTIAL:
  801. case COMPACT_SKIPPED:
  802. /* Compaction is likely to fail */
  803. return ret;
  804. case COMPACT_CONTINUE:
  805. /* Fall through to compaction */
  806. ;
  807. }
  808. /*
  809. * Setup to move all movable pages to the end of the zone. Used cached
  810. * information on where the scanners should start but check that it
  811. * is initialised by ensuring the values are within zone boundaries.
  812. */
  813. cc->migrate_pfn = zone->compact_cached_migrate_pfn;
  814. cc->free_pfn = zone->compact_cached_free_pfn;
  815. if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
  816. cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
  817. zone->compact_cached_free_pfn = cc->free_pfn;
  818. }
  819. if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
  820. cc->migrate_pfn = start_pfn;
  821. zone->compact_cached_migrate_pfn = cc->migrate_pfn;
  822. }
  823. /*
  824. * Clear pageblock skip if there were failures recently and compaction
  825. * is about to be retried after being deferred. kswapd does not do
  826. * this reset as it'll reset the cached information when going to sleep.
  827. */
  828. if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
  829. __reset_isolation_suitable(zone);
  830. migrate_prep_local();
  831. while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
  832. unsigned long nr_migrate, nr_remaining;
  833. int err;
  834. switch (isolate_migratepages(zone, cc)) {
  835. case ISOLATE_ABORT:
  836. ret = COMPACT_PARTIAL;
  837. putback_lru_pages(&cc->migratepages);
  838. cc->nr_migratepages = 0;
  839. goto out;
  840. case ISOLATE_NONE:
  841. continue;
  842. case ISOLATE_SUCCESS:
  843. ;
  844. }
  845. nr_migrate = cc->nr_migratepages;
  846. err = migrate_pages(&cc->migratepages, compaction_alloc,
  847. (unsigned long)cc, false,
  848. cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
  849. update_nr_listpages(cc);
  850. nr_remaining = cc->nr_migratepages;
  851. count_vm_event(COMPACTBLOCKS);
  852. count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
  853. if (nr_remaining)
  854. count_vm_events(COMPACTPAGEFAILED, nr_remaining);
  855. trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
  856. nr_remaining);
  857. /* Release LRU pages not migrated */
  858. if (err) {
  859. putback_lru_pages(&cc->migratepages);
  860. cc->nr_migratepages = 0;
  861. if (err == -ENOMEM) {
  862. ret = COMPACT_PARTIAL;
  863. goto out;
  864. }
  865. }
  866. /* Capture a page now if it is a suitable size */
  867. compact_capture_page(cc);
  868. }
  869. out:
  870. /* Release free pages and check accounting */
  871. cc->nr_freepages -= release_freepages(&cc->freepages);
  872. VM_BUG_ON(cc->nr_freepages != 0);
  873. return ret;
  874. }
  875. static unsigned long compact_zone_order(struct zone *zone,
  876. int order, gfp_t gfp_mask,
  877. bool sync, bool *contended,
  878. struct page **page)
  879. {
  880. unsigned long ret;
  881. struct compact_control cc = {
  882. .nr_freepages = 0,
  883. .nr_migratepages = 0,
  884. .order = order,
  885. .migratetype = allocflags_to_migratetype(gfp_mask),
  886. .zone = zone,
  887. .sync = sync,
  888. .page = page,
  889. };
  890. INIT_LIST_HEAD(&cc.freepages);
  891. INIT_LIST_HEAD(&cc.migratepages);
  892. ret = compact_zone(zone, &cc);
  893. VM_BUG_ON(!list_empty(&cc.freepages));
  894. VM_BUG_ON(!list_empty(&cc.migratepages));
  895. *contended = cc.contended;
  896. return ret;
  897. }
  898. int sysctl_extfrag_threshold = 500;
  899. /**
  900. * try_to_compact_pages - Direct compact to satisfy a high-order allocation
  901. * @zonelist: The zonelist used for the current allocation
  902. * @order: The order of the current allocation
  903. * @gfp_mask: The GFP mask of the current allocation
  904. * @nodemask: The allowed nodes to allocate from
  905. * @sync: Whether migration is synchronous or not
  906. * @contended: Return value that is true if compaction was aborted due to lock contention
  907. * @page: Optionally capture a free page of the requested order during compaction
  908. *
  909. * This is the main entry point for direct page compaction.
  910. */
  911. unsigned long try_to_compact_pages(struct zonelist *zonelist,
  912. int order, gfp_t gfp_mask, nodemask_t *nodemask,
  913. bool sync, bool *contended, struct page **page)
  914. {
  915. enum zone_type high_zoneidx = gfp_zone(gfp_mask);
  916. int may_enter_fs = gfp_mask & __GFP_FS;
  917. int may_perform_io = gfp_mask & __GFP_IO;
  918. struct zoneref *z;
  919. struct zone *zone;
  920. int rc = COMPACT_SKIPPED;
  921. int alloc_flags = 0;
  922. /* Check if the GFP flags allow compaction */
  923. if (!order || !may_enter_fs || !may_perform_io)
  924. return rc;
  925. count_vm_event(COMPACTSTALL);
  926. #ifdef CONFIG_CMA
  927. if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
  928. alloc_flags |= ALLOC_CMA;
  929. #endif
  930. /* Compact each zone in the list */
  931. for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
  932. nodemask) {
  933. int status;
  934. status = compact_zone_order(zone, order, gfp_mask, sync,
  935. contended, page);
  936. rc = max(status, rc);
  937. /* If a normal allocation would succeed, stop compacting */
  938. if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
  939. alloc_flags))
  940. break;
  941. }
  942. return rc;
  943. }
  944. /* Compact all zones within a node */
  945. static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
  946. {
  947. int zoneid;
  948. struct zone *zone;
  949. for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
  950. zone = &pgdat->node_zones[zoneid];
  951. if (!populated_zone(zone))
  952. continue;
  953. cc->nr_freepages = 0;
  954. cc->nr_migratepages = 0;
  955. cc->zone = zone;
  956. INIT_LIST_HEAD(&cc->freepages);
  957. INIT_LIST_HEAD(&cc->migratepages);
  958. if (cc->order == -1 || !compaction_deferred(zone, cc->order))
  959. compact_zone(zone, cc);
  960. if (cc->order > 0) {
  961. int ok = zone_watermark_ok(zone, cc->order,
  962. low_wmark_pages(zone), 0, 0);
  963. if (ok && cc->order >= zone->compact_order_failed)
  964. zone->compact_order_failed = cc->order + 1;
  965. /* Currently async compaction is never deferred. */
  966. else if (!ok && cc->sync)
  967. defer_compaction(zone, cc->order);
  968. }
  969. VM_BUG_ON(!list_empty(&cc->freepages));
  970. VM_BUG_ON(!list_empty(&cc->migratepages));
  971. }
  972. return 0;
  973. }
  974. int compact_pgdat(pg_data_t *pgdat, int order)
  975. {
  976. struct compact_control cc = {
  977. .order = order,
  978. .sync = false,
  979. .page = NULL,
  980. };
  981. return __compact_pgdat(pgdat, &cc);
  982. }
  983. static int compact_node(int nid)
  984. {
  985. struct compact_control cc = {
  986. .order = -1,
  987. .sync = true,
  988. .page = NULL,
  989. };
  990. return __compact_pgdat(NODE_DATA(nid), &cc);
  991. }
  992. /* Compact all nodes in the system */
  993. static int compact_nodes(void)
  994. {
  995. int nid;
  996. /* Flush pending updates to the LRU lists */
  997. lru_add_drain_all();
  998. for_each_online_node(nid)
  999. compact_node(nid);
  1000. return COMPACT_COMPLETE;
  1001. }
  1002. /* The written value is actually unused, all memory is compacted */
  1003. int sysctl_compact_memory;
  1004. /* This is the entry point for compacting all nodes via /proc/sys/vm */
  1005. int sysctl_compaction_handler(struct ctl_table *table, int write,
  1006. void __user *buffer, size_t *length, loff_t *ppos)
  1007. {
  1008. if (write)
  1009. return compact_nodes();
  1010. return 0;
  1011. }
  1012. int sysctl_extfrag_handler(struct ctl_table *table, int write,
  1013. void __user *buffer, size_t *length, loff_t *ppos)
  1014. {
  1015. proc_dointvec_minmax(table, write, buffer, length, ppos);
  1016. return 0;
  1017. }
  1018. #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
  1019. ssize_t sysfs_compact_node(struct device *dev,
  1020. struct device_attribute *attr,
  1021. const char *buf, size_t count)
  1022. {
  1023. int nid = dev->id;
  1024. if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
  1025. /* Flush pending updates to the LRU lists */
  1026. lru_add_drain_all();
  1027. compact_node(nid);
  1028. }
  1029. return count;
  1030. }
  1031. static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
  1032. int compaction_register_node(struct node *node)
  1033. {
  1034. return device_create_file(&node->dev, &dev_attr_compact);
  1035. }
  1036. void compaction_unregister_node(struct node *node)
  1037. {
  1038. return device_remove_file(&node->dev, &dev_attr_compact);
  1039. }
  1040. #endif /* CONFIG_SYSFS && CONFIG_NUMA */
  1041. #endif /* CONFIG_COMPACTION */