compaction.c 12 KB

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