compaction.c 26 KB

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