compaction.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. unsigned int order; /* order a direct compactor needs */
  36. int migratetype; /* MOVABLE, RECLAIMABLE etc */
  37. struct zone *zone;
  38. };
  39. static unsigned long release_freepages(struct list_head *freelist)
  40. {
  41. struct page *page, *next;
  42. unsigned long count = 0;
  43. list_for_each_entry_safe(page, next, freelist, lru) {
  44. list_del(&page->lru);
  45. __free_page(page);
  46. count++;
  47. }
  48. return count;
  49. }
  50. /* Isolate free pages onto a private freelist. Must hold zone->lock */
  51. static unsigned long isolate_freepages_block(struct zone *zone,
  52. unsigned long blockpfn,
  53. struct list_head *freelist)
  54. {
  55. unsigned long zone_end_pfn, end_pfn;
  56. int total_isolated = 0;
  57. struct page *cursor;
  58. /* Get the last PFN we should scan for free pages at */
  59. zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
  60. end_pfn = min(blockpfn + pageblock_nr_pages, zone_end_pfn);
  61. /* Find the first usable PFN in the block to initialse page cursor */
  62. for (; blockpfn < end_pfn; blockpfn++) {
  63. if (pfn_valid_within(blockpfn))
  64. break;
  65. }
  66. cursor = pfn_to_page(blockpfn);
  67. /* Isolate free pages. This assumes the block is valid */
  68. for (; blockpfn < end_pfn; blockpfn++, cursor++) {
  69. int isolated, i;
  70. struct page *page = cursor;
  71. if (!pfn_valid_within(blockpfn))
  72. continue;
  73. if (!PageBuddy(page))
  74. continue;
  75. /* Found a free page, break it into order-0 pages */
  76. isolated = split_free_page(page);
  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. return total_isolated;
  89. }
  90. /* Returns true if the page is within a block suitable for migration to */
  91. static bool suitable_migration_target(struct page *page)
  92. {
  93. int migratetype = get_pageblock_migratetype(page);
  94. /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
  95. if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
  96. return false;
  97. /* If the page is a large free page, then allow migration */
  98. if (PageBuddy(page) && page_order(page) >= pageblock_order)
  99. return true;
  100. /* If the block is MIGRATE_MOVABLE, allow migration */
  101. if (migratetype == MIGRATE_MOVABLE)
  102. return true;
  103. /* Otherwise skip the block */
  104. return false;
  105. }
  106. /*
  107. * Based on information in the current compact_control, find blocks
  108. * suitable for isolating free pages from and then isolate them.
  109. */
  110. static void isolate_freepages(struct zone *zone,
  111. struct compact_control *cc)
  112. {
  113. struct page *page;
  114. unsigned long high_pfn, low_pfn, pfn;
  115. unsigned long flags;
  116. int nr_freepages = cc->nr_freepages;
  117. struct list_head *freelist = &cc->freepages;
  118. pfn = cc->free_pfn;
  119. low_pfn = cc->migrate_pfn + pageblock_nr_pages;
  120. high_pfn = low_pfn;
  121. /*
  122. * Isolate free pages until enough are available to migrate the
  123. * pages on cc->migratepages. We stop searching if the migrate
  124. * and free page scanners meet or enough free pages are isolated.
  125. */
  126. spin_lock_irqsave(&zone->lock, flags);
  127. for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
  128. pfn -= pageblock_nr_pages) {
  129. unsigned long isolated;
  130. if (!pfn_valid(pfn))
  131. continue;
  132. /*
  133. * Check for overlapping nodes/zones. It's possible on some
  134. * configurations to have a setup like
  135. * node0 node1 node0
  136. * i.e. it's possible that all pages within a zones range of
  137. * pages do not belong to a single zone.
  138. */
  139. page = pfn_to_page(pfn);
  140. if (page_zone(page) != zone)
  141. continue;
  142. /* Check the block is suitable for migration */
  143. if (!suitable_migration_target(page))
  144. continue;
  145. /* Found a block suitable for isolating free pages from */
  146. isolated = isolate_freepages_block(zone, pfn, freelist);
  147. nr_freepages += isolated;
  148. /*
  149. * Record the highest PFN we isolated pages from. When next
  150. * looking for free pages, the search will restart here as
  151. * page migration may have returned some pages to the allocator
  152. */
  153. if (isolated)
  154. high_pfn = max(high_pfn, pfn);
  155. }
  156. spin_unlock_irqrestore(&zone->lock, flags);
  157. /* split_free_page does not map the pages */
  158. list_for_each_entry(page, freelist, lru) {
  159. arch_alloc_page(page, 0);
  160. kernel_map_pages(page, 1, 1);
  161. }
  162. cc->free_pfn = high_pfn;
  163. cc->nr_freepages = nr_freepages;
  164. }
  165. /* Update the number of anon and file isolated pages in the zone */
  166. static void acct_isolated(struct zone *zone, struct compact_control *cc)
  167. {
  168. struct page *page;
  169. unsigned int count[NR_LRU_LISTS] = { 0, };
  170. list_for_each_entry(page, &cc->migratepages, lru) {
  171. int lru = page_lru_base_type(page);
  172. count[lru]++;
  173. }
  174. cc->nr_anon = count[LRU_ACTIVE_ANON] + count[LRU_INACTIVE_ANON];
  175. cc->nr_file = count[LRU_ACTIVE_FILE] + count[LRU_INACTIVE_FILE];
  176. __mod_zone_page_state(zone, NR_ISOLATED_ANON, cc->nr_anon);
  177. __mod_zone_page_state(zone, NR_ISOLATED_FILE, cc->nr_file);
  178. }
  179. /* Similar to reclaim, but different enough that they don't share logic */
  180. static bool too_many_isolated(struct zone *zone)
  181. {
  182. unsigned long inactive, isolated;
  183. inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
  184. zone_page_state(zone, NR_INACTIVE_ANON);
  185. isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
  186. zone_page_state(zone, NR_ISOLATED_ANON);
  187. return isolated > inactive;
  188. }
  189. /*
  190. * Isolate all pages that can be migrated from the block pointed to by
  191. * the migrate scanner within compact_control.
  192. */
  193. static unsigned long isolate_migratepages(struct zone *zone,
  194. struct compact_control *cc)
  195. {
  196. unsigned long low_pfn, end_pfn;
  197. struct list_head *migratelist = &cc->migratepages;
  198. /* Do not scan outside zone boundaries */
  199. low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
  200. /* Only scan within a pageblock boundary */
  201. end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
  202. /* Do not cross the free scanner or scan within a memory hole */
  203. if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
  204. cc->migrate_pfn = end_pfn;
  205. return 0;
  206. }
  207. /*
  208. * Ensure that there are not too many pages isolated from the LRU
  209. * list by either parallel reclaimers or compaction. If there are,
  210. * delay for some time until fewer pages are isolated
  211. */
  212. while (unlikely(too_many_isolated(zone))) {
  213. congestion_wait(BLK_RW_ASYNC, HZ/10);
  214. if (fatal_signal_pending(current))
  215. return 0;
  216. }
  217. /* Time to isolate some pages for migration */
  218. spin_lock_irq(&zone->lru_lock);
  219. for (; low_pfn < end_pfn; low_pfn++) {
  220. struct page *page;
  221. if (!pfn_valid_within(low_pfn))
  222. continue;
  223. /* Get the page and skip if free */
  224. page = pfn_to_page(low_pfn);
  225. if (PageBuddy(page))
  226. continue;
  227. /* Try isolate the page */
  228. if (__isolate_lru_page(page, ISOLATE_BOTH, 0) != 0)
  229. continue;
  230. /* Successfully isolated */
  231. del_page_from_lru_list(zone, page, page_lru(page));
  232. list_add(&page->lru, migratelist);
  233. mem_cgroup_del_lru(page);
  234. cc->nr_migratepages++;
  235. /* Avoid isolating too much */
  236. if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
  237. break;
  238. }
  239. acct_isolated(zone, cc);
  240. spin_unlock_irq(&zone->lru_lock);
  241. cc->migrate_pfn = low_pfn;
  242. return cc->nr_migratepages;
  243. }
  244. /*
  245. * This is a migrate-callback that "allocates" freepages by taking pages
  246. * from the isolated freelists in the block we are migrating to.
  247. */
  248. static struct page *compaction_alloc(struct page *migratepage,
  249. unsigned long data,
  250. int **result)
  251. {
  252. struct compact_control *cc = (struct compact_control *)data;
  253. struct page *freepage;
  254. /* Isolate free pages if necessary */
  255. if (list_empty(&cc->freepages)) {
  256. isolate_freepages(cc->zone, cc);
  257. if (list_empty(&cc->freepages))
  258. return NULL;
  259. }
  260. freepage = list_entry(cc->freepages.next, struct page, lru);
  261. list_del(&freepage->lru);
  262. cc->nr_freepages--;
  263. return freepage;
  264. }
  265. /*
  266. * We cannot control nr_migratepages and nr_freepages fully when migration is
  267. * running as migrate_pages() has no knowledge of compact_control. When
  268. * migration is complete, we count the number of pages on the lists by hand.
  269. */
  270. static void update_nr_listpages(struct compact_control *cc)
  271. {
  272. int nr_migratepages = 0;
  273. int nr_freepages = 0;
  274. struct page *page;
  275. list_for_each_entry(page, &cc->migratepages, lru)
  276. nr_migratepages++;
  277. list_for_each_entry(page, &cc->freepages, lru)
  278. nr_freepages++;
  279. cc->nr_migratepages = nr_migratepages;
  280. cc->nr_freepages = nr_freepages;
  281. }
  282. static int compact_finished(struct zone *zone,
  283. struct compact_control *cc)
  284. {
  285. unsigned int order;
  286. unsigned long watermark = low_wmark_pages(zone) + (1 << cc->order);
  287. if (fatal_signal_pending(current))
  288. return COMPACT_PARTIAL;
  289. /* Compaction run completes if the migrate and free scanner meet */
  290. if (cc->free_pfn <= cc->migrate_pfn)
  291. return COMPACT_COMPLETE;
  292. /* Compaction run is not finished if the watermark is not met */
  293. if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
  294. return COMPACT_CONTINUE;
  295. if (cc->order == -1)
  296. return COMPACT_CONTINUE;
  297. /* Direct compactor: Is a suitable page free? */
  298. for (order = cc->order; order < MAX_ORDER; order++) {
  299. /* Job done if page is free of the right migratetype */
  300. if (!list_empty(&zone->free_area[order].free_list[cc->migratetype]))
  301. return COMPACT_PARTIAL;
  302. /* Job done if allocation would set block type */
  303. if (order >= pageblock_order && zone->free_area[order].nr_free)
  304. return COMPACT_PARTIAL;
  305. }
  306. return COMPACT_CONTINUE;
  307. }
  308. static int compact_zone(struct zone *zone, struct compact_control *cc)
  309. {
  310. int ret;
  311. /* Setup to move all movable pages to the end of the zone */
  312. cc->migrate_pfn = zone->zone_start_pfn;
  313. cc->free_pfn = cc->migrate_pfn + zone->spanned_pages;
  314. cc->free_pfn &= ~(pageblock_nr_pages-1);
  315. migrate_prep_local();
  316. while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
  317. unsigned long nr_migrate, nr_remaining;
  318. if (!isolate_migratepages(zone, cc))
  319. continue;
  320. nr_migrate = cc->nr_migratepages;
  321. migrate_pages(&cc->migratepages, compaction_alloc,
  322. (unsigned long)cc, 0);
  323. update_nr_listpages(cc);
  324. nr_remaining = cc->nr_migratepages;
  325. count_vm_event(COMPACTBLOCKS);
  326. count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
  327. if (nr_remaining)
  328. count_vm_events(COMPACTPAGEFAILED, nr_remaining);
  329. /* Release LRU pages not migrated */
  330. if (!list_empty(&cc->migratepages)) {
  331. putback_lru_pages(&cc->migratepages);
  332. cc->nr_migratepages = 0;
  333. }
  334. }
  335. /* Release free pages and check accounting */
  336. cc->nr_freepages -= release_freepages(&cc->freepages);
  337. VM_BUG_ON(cc->nr_freepages != 0);
  338. return ret;
  339. }
  340. static unsigned long compact_zone_order(struct zone *zone,
  341. int order, gfp_t gfp_mask)
  342. {
  343. struct compact_control cc = {
  344. .nr_freepages = 0,
  345. .nr_migratepages = 0,
  346. .order = order,
  347. .migratetype = allocflags_to_migratetype(gfp_mask),
  348. .zone = zone,
  349. };
  350. INIT_LIST_HEAD(&cc.freepages);
  351. INIT_LIST_HEAD(&cc.migratepages);
  352. return compact_zone(zone, &cc);
  353. }
  354. int sysctl_extfrag_threshold = 500;
  355. /**
  356. * try_to_compact_pages - Direct compact to satisfy a high-order allocation
  357. * @zonelist: The zonelist used for the current allocation
  358. * @order: The order of the current allocation
  359. * @gfp_mask: The GFP mask of the current allocation
  360. * @nodemask: The allowed nodes to allocate from
  361. *
  362. * This is the main entry point for direct page compaction.
  363. */
  364. unsigned long try_to_compact_pages(struct zonelist *zonelist,
  365. int order, gfp_t gfp_mask, nodemask_t *nodemask)
  366. {
  367. enum zone_type high_zoneidx = gfp_zone(gfp_mask);
  368. int may_enter_fs = gfp_mask & __GFP_FS;
  369. int may_perform_io = gfp_mask & __GFP_IO;
  370. unsigned long watermark;
  371. struct zoneref *z;
  372. struct zone *zone;
  373. int rc = COMPACT_SKIPPED;
  374. /*
  375. * Check whether it is worth even starting compaction. The order check is
  376. * made because an assumption is made that the page allocator can satisfy
  377. * the "cheaper" orders without taking special steps
  378. */
  379. if (order <= PAGE_ALLOC_COSTLY_ORDER || !may_enter_fs || !may_perform_io)
  380. return rc;
  381. count_vm_event(COMPACTSTALL);
  382. /* Compact each zone in the list */
  383. for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
  384. nodemask) {
  385. int fragindex;
  386. int status;
  387. /*
  388. * Watermarks for order-0 must be met for compaction. Note
  389. * the 2UL. This is because during migration, copies of
  390. * pages need to be allocated and for a short time, the
  391. * footprint is higher
  392. */
  393. watermark = low_wmark_pages(zone) + (2UL << order);
  394. if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
  395. continue;
  396. /*
  397. * fragmentation index determines if allocation failures are
  398. * due to low memory or external fragmentation
  399. *
  400. * index of -1 implies allocations might succeed depending
  401. * on watermarks
  402. * index towards 0 implies failure is due to lack of memory
  403. * index towards 1000 implies failure is due to fragmentation
  404. *
  405. * Only compact if a failure would be due to fragmentation.
  406. */
  407. fragindex = fragmentation_index(zone, order);
  408. if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
  409. continue;
  410. if (fragindex == -1 && zone_watermark_ok(zone, order, watermark, 0, 0)) {
  411. rc = COMPACT_PARTIAL;
  412. break;
  413. }
  414. status = compact_zone_order(zone, order, gfp_mask);
  415. rc = max(status, rc);
  416. if (zone_watermark_ok(zone, order, watermark, 0, 0))
  417. break;
  418. }
  419. return rc;
  420. }
  421. /* Compact all zones within a node */
  422. static int compact_node(int nid)
  423. {
  424. int zoneid;
  425. pg_data_t *pgdat;
  426. struct zone *zone;
  427. if (nid < 0 || nid >= nr_node_ids || !node_online(nid))
  428. return -EINVAL;
  429. pgdat = NODE_DATA(nid);
  430. /* Flush pending updates to the LRU lists */
  431. lru_add_drain_all();
  432. for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
  433. struct compact_control cc = {
  434. .nr_freepages = 0,
  435. .nr_migratepages = 0,
  436. .order = -1,
  437. };
  438. zone = &pgdat->node_zones[zoneid];
  439. if (!populated_zone(zone))
  440. continue;
  441. cc.zone = zone;
  442. INIT_LIST_HEAD(&cc.freepages);
  443. INIT_LIST_HEAD(&cc.migratepages);
  444. compact_zone(zone, &cc);
  445. VM_BUG_ON(!list_empty(&cc.freepages));
  446. VM_BUG_ON(!list_empty(&cc.migratepages));
  447. }
  448. return 0;
  449. }
  450. /* Compact all nodes in the system */
  451. static int compact_nodes(void)
  452. {
  453. int nid;
  454. for_each_online_node(nid)
  455. compact_node(nid);
  456. return COMPACT_COMPLETE;
  457. }
  458. /* The written value is actually unused, all memory is compacted */
  459. int sysctl_compact_memory;
  460. /* This is the entry point for compacting all nodes via /proc/sys/vm */
  461. int sysctl_compaction_handler(struct ctl_table *table, int write,
  462. void __user *buffer, size_t *length, loff_t *ppos)
  463. {
  464. if (write)
  465. return compact_nodes();
  466. return 0;
  467. }
  468. int sysctl_extfrag_handler(struct ctl_table *table, int write,
  469. void __user *buffer, size_t *length, loff_t *ppos)
  470. {
  471. proc_dointvec_minmax(table, write, buffer, length, ppos);
  472. return 0;
  473. }
  474. #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
  475. ssize_t sysfs_compact_node(struct sys_device *dev,
  476. struct sysdev_attribute *attr,
  477. const char *buf, size_t count)
  478. {
  479. compact_node(dev->id);
  480. return count;
  481. }
  482. static SYSDEV_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
  483. int compaction_register_node(struct node *node)
  484. {
  485. return sysdev_create_file(&node->sysdev, &attr_compact);
  486. }
  487. void compaction_unregister_node(struct node *node)
  488. {
  489. return sysdev_remove_file(&node->sysdev, &attr_compact);
  490. }
  491. #endif /* CONFIG_SYSFS && CONFIG_NUMA */