compaction.c 25 KB

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