compaction.c 12 KB

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