compaction.c 26 KB

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