compaction.c 23 KB

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