compaction.c 24 KB

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