compaction.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 "internal.h"
  16. /*
  17. * compact_control is used to track pages being migrated and the free pages
  18. * they are being migrated to during memory compaction. The free_pfn starts
  19. * at the end of a zone and migrate_pfn begins at the start. Movable pages
  20. * are moved to the end of a zone during a compaction run and the run
  21. * completes when free_pfn <= migrate_pfn
  22. */
  23. struct compact_control {
  24. struct list_head freepages; /* List of free pages to migrate to */
  25. struct list_head migratepages; /* List of pages being migrated */
  26. unsigned long nr_freepages; /* Number of isolated free pages */
  27. unsigned long nr_migratepages; /* Number of pages to migrate */
  28. unsigned long free_pfn; /* isolate_freepages search base */
  29. unsigned long migrate_pfn; /* isolate_migratepages search base */
  30. /* Account for isolated anon and file pages */
  31. unsigned long nr_anon;
  32. unsigned long nr_file;
  33. struct zone *zone;
  34. };
  35. static unsigned long release_freepages(struct list_head *freelist)
  36. {
  37. struct page *page, *next;
  38. unsigned long count = 0;
  39. list_for_each_entry_safe(page, next, freelist, lru) {
  40. list_del(&page->lru);
  41. __free_page(page);
  42. count++;
  43. }
  44. return count;
  45. }
  46. /* Isolate free pages onto a private freelist. Must hold zone->lock */
  47. static unsigned long isolate_freepages_block(struct zone *zone,
  48. unsigned long blockpfn,
  49. struct list_head *freelist)
  50. {
  51. unsigned long zone_end_pfn, end_pfn;
  52. int total_isolated = 0;
  53. struct page *cursor;
  54. /* Get the last PFN we should scan for free pages at */
  55. zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
  56. end_pfn = min(blockpfn + pageblock_nr_pages, zone_end_pfn);
  57. /* Find the first usable PFN in the block to initialse page cursor */
  58. for (; blockpfn < end_pfn; blockpfn++) {
  59. if (pfn_valid_within(blockpfn))
  60. break;
  61. }
  62. cursor = pfn_to_page(blockpfn);
  63. /* Isolate free pages. This assumes the block is valid */
  64. for (; blockpfn < end_pfn; blockpfn++, cursor++) {
  65. int isolated, i;
  66. struct page *page = cursor;
  67. if (!pfn_valid_within(blockpfn))
  68. continue;
  69. if (!PageBuddy(page))
  70. continue;
  71. /* Found a free page, break it into order-0 pages */
  72. isolated = split_free_page(page);
  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. return total_isolated;
  85. }
  86. /* Returns true if the page is within a block suitable for migration to */
  87. static bool suitable_migration_target(struct page *page)
  88. {
  89. int migratetype = get_pageblock_migratetype(page);
  90. /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
  91. if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
  92. return false;
  93. /* If the page is a large free page, then allow migration */
  94. if (PageBuddy(page) && page_order(page) >= pageblock_order)
  95. return true;
  96. /* If the block is MIGRATE_MOVABLE, allow migration */
  97. if (migratetype == MIGRATE_MOVABLE)
  98. return true;
  99. /* Otherwise skip the block */
  100. return false;
  101. }
  102. /*
  103. * Based on information in the current compact_control, find blocks
  104. * suitable for isolating free pages from and then isolate them.
  105. */
  106. static void isolate_freepages(struct zone *zone,
  107. struct compact_control *cc)
  108. {
  109. struct page *page;
  110. unsigned long high_pfn, low_pfn, pfn;
  111. unsigned long flags;
  112. int nr_freepages = cc->nr_freepages;
  113. struct list_head *freelist = &cc->freepages;
  114. pfn = cc->free_pfn;
  115. low_pfn = cc->migrate_pfn + pageblock_nr_pages;
  116. high_pfn = low_pfn;
  117. /*
  118. * Isolate free pages until enough are available to migrate the
  119. * pages on cc->migratepages. We stop searching if the migrate
  120. * and free page scanners meet or enough free pages are isolated.
  121. */
  122. spin_lock_irqsave(&zone->lock, flags);
  123. for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
  124. pfn -= pageblock_nr_pages) {
  125. unsigned long isolated;
  126. if (!pfn_valid(pfn))
  127. continue;
  128. /*
  129. * Check for overlapping nodes/zones. It's possible on some
  130. * configurations to have a setup like
  131. * node0 node1 node0
  132. * i.e. it's possible that all pages within a zones range of
  133. * pages do not belong to a single zone.
  134. */
  135. page = pfn_to_page(pfn);
  136. if (page_zone(page) != zone)
  137. continue;
  138. /* Check the block is suitable for migration */
  139. if (!suitable_migration_target(page))
  140. continue;
  141. /* Found a block suitable for isolating free pages from */
  142. isolated = isolate_freepages_block(zone, pfn, freelist);
  143. nr_freepages += isolated;
  144. /*
  145. * Record the highest PFN we isolated pages from. When next
  146. * looking for free pages, the search will restart here as
  147. * page migration may have returned some pages to the allocator
  148. */
  149. if (isolated)
  150. high_pfn = max(high_pfn, pfn);
  151. }
  152. spin_unlock_irqrestore(&zone->lock, flags);
  153. /* split_free_page does not map the pages */
  154. list_for_each_entry(page, freelist, lru) {
  155. arch_alloc_page(page, 0);
  156. kernel_map_pages(page, 1, 1);
  157. }
  158. cc->free_pfn = high_pfn;
  159. cc->nr_freepages = nr_freepages;
  160. }
  161. /* Update the number of anon and file isolated pages in the zone */
  162. static void acct_isolated(struct zone *zone, struct compact_control *cc)
  163. {
  164. struct page *page;
  165. unsigned int count[NR_LRU_LISTS] = { 0, };
  166. list_for_each_entry(page, &cc->migratepages, lru) {
  167. int lru = page_lru_base_type(page);
  168. count[lru]++;
  169. }
  170. cc->nr_anon = count[LRU_ACTIVE_ANON] + count[LRU_INACTIVE_ANON];
  171. cc->nr_file = count[LRU_ACTIVE_FILE] + count[LRU_INACTIVE_FILE];
  172. __mod_zone_page_state(zone, NR_ISOLATED_ANON, cc->nr_anon);
  173. __mod_zone_page_state(zone, NR_ISOLATED_FILE, cc->nr_file);
  174. }
  175. /* Similar to reclaim, but different enough that they don't share logic */
  176. static bool too_many_isolated(struct zone *zone)
  177. {
  178. unsigned long inactive, isolated;
  179. inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
  180. zone_page_state(zone, NR_INACTIVE_ANON);
  181. isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
  182. zone_page_state(zone, NR_ISOLATED_ANON);
  183. return isolated > inactive;
  184. }
  185. /*
  186. * Isolate all pages that can be migrated from the block pointed to by
  187. * the migrate scanner within compact_control.
  188. */
  189. static unsigned long isolate_migratepages(struct zone *zone,
  190. struct compact_control *cc)
  191. {
  192. unsigned long low_pfn, end_pfn;
  193. struct list_head *migratelist = &cc->migratepages;
  194. /* Do not scan outside zone boundaries */
  195. low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
  196. /* Only scan within a pageblock boundary */
  197. end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
  198. /* Do not cross the free scanner or scan within a memory hole */
  199. if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
  200. cc->migrate_pfn = end_pfn;
  201. return 0;
  202. }
  203. /*
  204. * Ensure that there are not too many pages isolated from the LRU
  205. * list by either parallel reclaimers or compaction. If there are,
  206. * delay for some time until fewer pages are isolated
  207. */
  208. while (unlikely(too_many_isolated(zone))) {
  209. congestion_wait(BLK_RW_ASYNC, HZ/10);
  210. if (fatal_signal_pending(current))
  211. return 0;
  212. }
  213. /* Time to isolate some pages for migration */
  214. spin_lock_irq(&zone->lru_lock);
  215. for (; low_pfn < end_pfn; low_pfn++) {
  216. struct page *page;
  217. if (!pfn_valid_within(low_pfn))
  218. continue;
  219. /* Get the page and skip if free */
  220. page = pfn_to_page(low_pfn);
  221. if (PageBuddy(page))
  222. continue;
  223. /* Try isolate the page */
  224. if (__isolate_lru_page(page, ISOLATE_BOTH, 0) != 0)
  225. continue;
  226. /* Successfully isolated */
  227. del_page_from_lru_list(zone, page, page_lru(page));
  228. list_add(&page->lru, migratelist);
  229. mem_cgroup_del_lru(page);
  230. cc->nr_migratepages++;
  231. /* Avoid isolating too much */
  232. if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
  233. break;
  234. }
  235. acct_isolated(zone, cc);
  236. spin_unlock_irq(&zone->lru_lock);
  237. cc->migrate_pfn = low_pfn;
  238. return cc->nr_migratepages;
  239. }
  240. /*
  241. * This is a migrate-callback that "allocates" freepages by taking pages
  242. * from the isolated freelists in the block we are migrating to.
  243. */
  244. static struct page *compaction_alloc(struct page *migratepage,
  245. unsigned long data,
  246. int **result)
  247. {
  248. struct compact_control *cc = (struct compact_control *)data;
  249. struct page *freepage;
  250. /* Isolate free pages if necessary */
  251. if (list_empty(&cc->freepages)) {
  252. isolate_freepages(cc->zone, cc);
  253. if (list_empty(&cc->freepages))
  254. return NULL;
  255. }
  256. freepage = list_entry(cc->freepages.next, struct page, lru);
  257. list_del(&freepage->lru);
  258. cc->nr_freepages--;
  259. return freepage;
  260. }
  261. /*
  262. * We cannot control nr_migratepages and nr_freepages fully when migration is
  263. * running as migrate_pages() has no knowledge of compact_control. When
  264. * migration is complete, we count the number of pages on the lists by hand.
  265. */
  266. static void update_nr_listpages(struct compact_control *cc)
  267. {
  268. int nr_migratepages = 0;
  269. int nr_freepages = 0;
  270. struct page *page;
  271. list_for_each_entry(page, &cc->migratepages, lru)
  272. nr_migratepages++;
  273. list_for_each_entry(page, &cc->freepages, lru)
  274. nr_freepages++;
  275. cc->nr_migratepages = nr_migratepages;
  276. cc->nr_freepages = nr_freepages;
  277. }
  278. static int compact_finished(struct zone *zone,
  279. struct compact_control *cc)
  280. {
  281. if (fatal_signal_pending(current))
  282. return COMPACT_PARTIAL;
  283. /* Compaction run completes if the migrate and free scanner meet */
  284. if (cc->free_pfn <= cc->migrate_pfn)
  285. return COMPACT_COMPLETE;
  286. return COMPACT_CONTINUE;
  287. }
  288. static int compact_zone(struct zone *zone, struct compact_control *cc)
  289. {
  290. int ret;
  291. /* Setup to move all movable pages to the end of the zone */
  292. cc->migrate_pfn = zone->zone_start_pfn;
  293. cc->free_pfn = cc->migrate_pfn + zone->spanned_pages;
  294. cc->free_pfn &= ~(pageblock_nr_pages-1);
  295. migrate_prep_local();
  296. while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
  297. unsigned long nr_migrate, nr_remaining;
  298. if (!isolate_migratepages(zone, cc))
  299. continue;
  300. nr_migrate = cc->nr_migratepages;
  301. migrate_pages(&cc->migratepages, compaction_alloc,
  302. (unsigned long)cc, 0);
  303. update_nr_listpages(cc);
  304. nr_remaining = cc->nr_migratepages;
  305. count_vm_event(COMPACTBLOCKS);
  306. count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
  307. if (nr_remaining)
  308. count_vm_events(COMPACTPAGEFAILED, nr_remaining);
  309. /* Release LRU pages not migrated */
  310. if (!list_empty(&cc->migratepages)) {
  311. putback_lru_pages(&cc->migratepages);
  312. cc->nr_migratepages = 0;
  313. }
  314. }
  315. /* Release free pages and check accounting */
  316. cc->nr_freepages -= release_freepages(&cc->freepages);
  317. VM_BUG_ON(cc->nr_freepages != 0);
  318. return ret;
  319. }