compaction.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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 active, inactive, isolated;
  183. inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
  184. zone_page_state(zone, NR_INACTIVE_ANON);
  185. active = zone_page_state(zone, NR_ACTIVE_FILE) +
  186. zone_page_state(zone, NR_ACTIVE_ANON);
  187. isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
  188. zone_page_state(zone, NR_ISOLATED_ANON);
  189. return isolated > (inactive + active) / 2;
  190. }
  191. /*
  192. * Isolate all pages that can be migrated from the block pointed to by
  193. * the migrate scanner within compact_control.
  194. */
  195. static unsigned long isolate_migratepages(struct zone *zone,
  196. struct compact_control *cc)
  197. {
  198. unsigned long low_pfn, end_pfn;
  199. struct list_head *migratelist = &cc->migratepages;
  200. /* Do not scan outside zone boundaries */
  201. low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
  202. /* Only scan within a pageblock boundary */
  203. end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
  204. /* Do not cross the free scanner or scan within a memory hole */
  205. if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
  206. cc->migrate_pfn = end_pfn;
  207. return 0;
  208. }
  209. /*
  210. * Ensure that there are not too many pages isolated from the LRU
  211. * list by either parallel reclaimers or compaction. If there are,
  212. * delay for some time until fewer pages are isolated
  213. */
  214. while (unlikely(too_many_isolated(zone))) {
  215. congestion_wait(BLK_RW_ASYNC, HZ/10);
  216. if (fatal_signal_pending(current))
  217. return 0;
  218. }
  219. /* Time to isolate some pages for migration */
  220. spin_lock_irq(&zone->lru_lock);
  221. for (; low_pfn < end_pfn; low_pfn++) {
  222. struct page *page;
  223. if (!pfn_valid_within(low_pfn))
  224. continue;
  225. /* Get the page and skip if free */
  226. page = pfn_to_page(low_pfn);
  227. if (PageBuddy(page))
  228. continue;
  229. /* Try isolate the page */
  230. if (__isolate_lru_page(page, ISOLATE_BOTH, 0) != 0)
  231. continue;
  232. /* Successfully isolated */
  233. del_page_from_lru_list(zone, page, page_lru(page));
  234. list_add(&page->lru, migratelist);
  235. mem_cgroup_del_lru(page);
  236. cc->nr_migratepages++;
  237. /* Avoid isolating too much */
  238. if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
  239. break;
  240. }
  241. acct_isolated(zone, cc);
  242. spin_unlock_irq(&zone->lru_lock);
  243. cc->migrate_pfn = low_pfn;
  244. return cc->nr_migratepages;
  245. }
  246. /*
  247. * This is a migrate-callback that "allocates" freepages by taking pages
  248. * from the isolated freelists in the block we are migrating to.
  249. */
  250. static struct page *compaction_alloc(struct page *migratepage,
  251. unsigned long data,
  252. int **result)
  253. {
  254. struct compact_control *cc = (struct compact_control *)data;
  255. struct page *freepage;
  256. /* Isolate free pages if necessary */
  257. if (list_empty(&cc->freepages)) {
  258. isolate_freepages(cc->zone, cc);
  259. if (list_empty(&cc->freepages))
  260. return NULL;
  261. }
  262. freepage = list_entry(cc->freepages.next, struct page, lru);
  263. list_del(&freepage->lru);
  264. cc->nr_freepages--;
  265. return freepage;
  266. }
  267. /*
  268. * We cannot control nr_migratepages and nr_freepages fully when migration is
  269. * running as migrate_pages() has no knowledge of compact_control. When
  270. * migration is complete, we count the number of pages on the lists by hand.
  271. */
  272. static void update_nr_listpages(struct compact_control *cc)
  273. {
  274. int nr_migratepages = 0;
  275. int nr_freepages = 0;
  276. struct page *page;
  277. list_for_each_entry(page, &cc->migratepages, lru)
  278. nr_migratepages++;
  279. list_for_each_entry(page, &cc->freepages, lru)
  280. nr_freepages++;
  281. cc->nr_migratepages = nr_migratepages;
  282. cc->nr_freepages = nr_freepages;
  283. }
  284. static int compact_finished(struct zone *zone,
  285. struct compact_control *cc)
  286. {
  287. unsigned int order;
  288. unsigned long watermark = low_wmark_pages(zone) + (1 << cc->order);
  289. if (fatal_signal_pending(current))
  290. return COMPACT_PARTIAL;
  291. /* Compaction run completes if the migrate and free scanner meet */
  292. if (cc->free_pfn <= cc->migrate_pfn)
  293. return COMPACT_COMPLETE;
  294. /* Compaction run is not finished if the watermark is not met */
  295. if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
  296. return COMPACT_CONTINUE;
  297. if (cc->order == -1)
  298. return COMPACT_CONTINUE;
  299. /* Direct compactor: Is a suitable page free? */
  300. for (order = cc->order; order < MAX_ORDER; order++) {
  301. /* Job done if page is free of the right migratetype */
  302. if (!list_empty(&zone->free_area[order].free_list[cc->migratetype]))
  303. return COMPACT_PARTIAL;
  304. /* Job done if allocation would set block type */
  305. if (order >= pageblock_order && zone->free_area[order].nr_free)
  306. return COMPACT_PARTIAL;
  307. }
  308. return COMPACT_CONTINUE;
  309. }
  310. static int compact_zone(struct zone *zone, struct compact_control *cc)
  311. {
  312. int ret;
  313. /* Setup to move all movable pages to the end of the zone */
  314. cc->migrate_pfn = zone->zone_start_pfn;
  315. cc->free_pfn = cc->migrate_pfn + zone->spanned_pages;
  316. cc->free_pfn &= ~(pageblock_nr_pages-1);
  317. migrate_prep_local();
  318. while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
  319. unsigned long nr_migrate, nr_remaining;
  320. if (!isolate_migratepages(zone, cc))
  321. continue;
  322. nr_migrate = cc->nr_migratepages;
  323. migrate_pages(&cc->migratepages, compaction_alloc,
  324. (unsigned long)cc, 0);
  325. update_nr_listpages(cc);
  326. nr_remaining = cc->nr_migratepages;
  327. count_vm_event(COMPACTBLOCKS);
  328. count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
  329. if (nr_remaining)
  330. count_vm_events(COMPACTPAGEFAILED, nr_remaining);
  331. /* Release LRU pages not migrated */
  332. if (!list_empty(&cc->migratepages)) {
  333. putback_lru_pages(&cc->migratepages);
  334. cc->nr_migratepages = 0;
  335. }
  336. }
  337. /* Release free pages and check accounting */
  338. cc->nr_freepages -= release_freepages(&cc->freepages);
  339. VM_BUG_ON(cc->nr_freepages != 0);
  340. return ret;
  341. }
  342. static unsigned long compact_zone_order(struct zone *zone,
  343. int order, gfp_t gfp_mask)
  344. {
  345. struct compact_control cc = {
  346. .nr_freepages = 0,
  347. .nr_migratepages = 0,
  348. .order = order,
  349. .migratetype = allocflags_to_migratetype(gfp_mask),
  350. .zone = zone,
  351. };
  352. INIT_LIST_HEAD(&cc.freepages);
  353. INIT_LIST_HEAD(&cc.migratepages);
  354. return compact_zone(zone, &cc);
  355. }
  356. int sysctl_extfrag_threshold = 500;
  357. /**
  358. * try_to_compact_pages - Direct compact to satisfy a high-order allocation
  359. * @zonelist: The zonelist used for the current allocation
  360. * @order: The order of the current allocation
  361. * @gfp_mask: The GFP mask of the current allocation
  362. * @nodemask: The allowed nodes to allocate from
  363. *
  364. * This is the main entry point for direct page compaction.
  365. */
  366. unsigned long try_to_compact_pages(struct zonelist *zonelist,
  367. int order, gfp_t gfp_mask, nodemask_t *nodemask)
  368. {
  369. enum zone_type high_zoneidx = gfp_zone(gfp_mask);
  370. int may_enter_fs = gfp_mask & __GFP_FS;
  371. int may_perform_io = gfp_mask & __GFP_IO;
  372. unsigned long watermark;
  373. struct zoneref *z;
  374. struct zone *zone;
  375. int rc = COMPACT_SKIPPED;
  376. /*
  377. * Check whether it is worth even starting compaction. The order check is
  378. * made because an assumption is made that the page allocator can satisfy
  379. * the "cheaper" orders without taking special steps
  380. */
  381. if (order <= PAGE_ALLOC_COSTLY_ORDER || !may_enter_fs || !may_perform_io)
  382. return rc;
  383. count_vm_event(COMPACTSTALL);
  384. /* Compact each zone in the list */
  385. for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
  386. nodemask) {
  387. int fragindex;
  388. int status;
  389. /*
  390. * Watermarks for order-0 must be met for compaction. Note
  391. * the 2UL. This is because during migration, copies of
  392. * pages need to be allocated and for a short time, the
  393. * footprint is higher
  394. */
  395. watermark = low_wmark_pages(zone) + (2UL << order);
  396. if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
  397. continue;
  398. /*
  399. * fragmentation index determines if allocation failures are
  400. * due to low memory or external fragmentation
  401. *
  402. * index of -1 implies allocations might succeed depending
  403. * on watermarks
  404. * index towards 0 implies failure is due to lack of memory
  405. * index towards 1000 implies failure is due to fragmentation
  406. *
  407. * Only compact if a failure would be due to fragmentation.
  408. */
  409. fragindex = fragmentation_index(zone, order);
  410. if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
  411. continue;
  412. if (fragindex == -1 && zone_watermark_ok(zone, order, watermark, 0, 0)) {
  413. rc = COMPACT_PARTIAL;
  414. break;
  415. }
  416. status = compact_zone_order(zone, order, gfp_mask);
  417. rc = max(status, rc);
  418. if (zone_watermark_ok(zone, order, watermark, 0, 0))
  419. break;
  420. }
  421. return rc;
  422. }
  423. /* Compact all zones within a node */
  424. static int compact_node(int nid)
  425. {
  426. int zoneid;
  427. pg_data_t *pgdat;
  428. struct zone *zone;
  429. if (nid < 0 || nid >= nr_node_ids || !node_online(nid))
  430. return -EINVAL;
  431. pgdat = NODE_DATA(nid);
  432. /* Flush pending updates to the LRU lists */
  433. lru_add_drain_all();
  434. for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
  435. struct compact_control cc = {
  436. .nr_freepages = 0,
  437. .nr_migratepages = 0,
  438. .order = -1,
  439. };
  440. zone = &pgdat->node_zones[zoneid];
  441. if (!populated_zone(zone))
  442. continue;
  443. cc.zone = zone;
  444. INIT_LIST_HEAD(&cc.freepages);
  445. INIT_LIST_HEAD(&cc.migratepages);
  446. compact_zone(zone, &cc);
  447. VM_BUG_ON(!list_empty(&cc.freepages));
  448. VM_BUG_ON(!list_empty(&cc.migratepages));
  449. }
  450. return 0;
  451. }
  452. /* Compact all nodes in the system */
  453. static int compact_nodes(void)
  454. {
  455. int nid;
  456. for_each_online_node(nid)
  457. compact_node(nid);
  458. return COMPACT_COMPLETE;
  459. }
  460. /* The written value is actually unused, all memory is compacted */
  461. int sysctl_compact_memory;
  462. /* This is the entry point for compacting all nodes via /proc/sys/vm */
  463. int sysctl_compaction_handler(struct ctl_table *table, int write,
  464. void __user *buffer, size_t *length, loff_t *ppos)
  465. {
  466. if (write)
  467. return compact_nodes();
  468. return 0;
  469. }
  470. int sysctl_extfrag_handler(struct ctl_table *table, int write,
  471. void __user *buffer, size_t *length, loff_t *ppos)
  472. {
  473. proc_dointvec_minmax(table, write, buffer, length, ppos);
  474. return 0;
  475. }
  476. #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
  477. ssize_t sysfs_compact_node(struct sys_device *dev,
  478. struct sysdev_attribute *attr,
  479. const char *buf, size_t count)
  480. {
  481. compact_node(dev->id);
  482. return count;
  483. }
  484. static SYSDEV_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
  485. int compaction_register_node(struct node *node)
  486. {
  487. return sysdev_create_file(&node->sysdev, &attr_compact);
  488. }
  489. void compaction_unregister_node(struct node *node)
  490. {
  491. return sysdev_remove_file(&node->sysdev, &attr_compact);
  492. }
  493. #endif /* CONFIG_SYSFS && CONFIG_NUMA */