compaction.c 29 KB

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