compaction.c 24 KB

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