compaction.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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. #if defined CONFIG_COMPACTION || defined CONFIG_CMA
  19. #define CREATE_TRACE_POINTS
  20. #include <trace/events/compaction.h>
  21. static unsigned long release_freepages(struct list_head *freelist)
  22. {
  23. struct page *page, *next;
  24. unsigned long count = 0;
  25. list_for_each_entry_safe(page, next, freelist, lru) {
  26. list_del(&page->lru);
  27. __free_page(page);
  28. count++;
  29. }
  30. return count;
  31. }
  32. static void map_pages(struct list_head *list)
  33. {
  34. struct page *page;
  35. list_for_each_entry(page, list, lru) {
  36. arch_alloc_page(page, 0);
  37. kernel_map_pages(page, 1, 1);
  38. }
  39. }
  40. static inline bool migrate_async_suitable(int migratetype)
  41. {
  42. return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
  43. }
  44. /*
  45. * Compaction requires the taking of some coarse locks that are potentially
  46. * very heavily contended. Check if the process needs to be scheduled or
  47. * if the lock is contended. For async compaction, back out in the event
  48. * if contention is severe. For sync compaction, schedule.
  49. *
  50. * Returns true if the lock is held.
  51. * Returns false if the lock is released and compaction should abort
  52. */
  53. static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags,
  54. bool locked, struct compact_control *cc)
  55. {
  56. if (need_resched() || spin_is_contended(lock)) {
  57. if (locked) {
  58. spin_unlock_irqrestore(lock, *flags);
  59. locked = false;
  60. }
  61. /* async aborts if taking too long or contended */
  62. if (!cc->sync) {
  63. if (cc->contended)
  64. *cc->contended = true;
  65. return false;
  66. }
  67. cond_resched();
  68. if (fatal_signal_pending(current))
  69. return false;
  70. }
  71. if (!locked)
  72. spin_lock_irqsave(lock, *flags);
  73. return true;
  74. }
  75. static inline bool compact_trylock_irqsave(spinlock_t *lock,
  76. unsigned long *flags, struct compact_control *cc)
  77. {
  78. return compact_checklock_irqsave(lock, flags, false, cc);
  79. }
  80. /*
  81. * Isolate free pages onto a private freelist. Caller must hold zone->lock.
  82. * If @strict is true, will abort returning 0 on any invalid PFNs or non-free
  83. * pages inside of the pageblock (even though it may still end up isolating
  84. * some pages).
  85. */
  86. static unsigned long isolate_freepages_block(unsigned long blockpfn,
  87. unsigned long end_pfn,
  88. struct list_head *freelist,
  89. bool strict)
  90. {
  91. int nr_scanned = 0, total_isolated = 0;
  92. struct page *cursor;
  93. cursor = pfn_to_page(blockpfn);
  94. /* Isolate free pages. This assumes the block is valid */
  95. for (; blockpfn < end_pfn; blockpfn++, cursor++) {
  96. int isolated, i;
  97. struct page *page = cursor;
  98. if (!pfn_valid_within(blockpfn)) {
  99. if (strict)
  100. return 0;
  101. continue;
  102. }
  103. nr_scanned++;
  104. if (!PageBuddy(page)) {
  105. if (strict)
  106. return 0;
  107. continue;
  108. }
  109. /* Found a free page, break it into order-0 pages */
  110. isolated = split_free_page(page);
  111. if (!isolated && strict)
  112. return 0;
  113. total_isolated += isolated;
  114. for (i = 0; i < isolated; i++) {
  115. list_add(&page->lru, freelist);
  116. page++;
  117. }
  118. /* If a page was split, advance to the end of it */
  119. if (isolated) {
  120. blockpfn += isolated - 1;
  121. cursor += isolated - 1;
  122. }
  123. }
  124. trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
  125. return total_isolated;
  126. }
  127. /**
  128. * isolate_freepages_range() - isolate free pages.
  129. * @start_pfn: The first PFN to start isolating.
  130. * @end_pfn: The one-past-last PFN.
  131. *
  132. * Non-free pages, invalid PFNs, or zone boundaries within the
  133. * [start_pfn, end_pfn) range are considered errors, cause function to
  134. * undo its actions and return zero.
  135. *
  136. * Otherwise, function returns one-past-the-last PFN of isolated page
  137. * (which may be greater then end_pfn if end fell in a middle of
  138. * a free page).
  139. */
  140. unsigned long
  141. isolate_freepages_range(unsigned long start_pfn, unsigned long end_pfn)
  142. {
  143. unsigned long isolated, pfn, block_end_pfn, flags;
  144. struct zone *zone = NULL;
  145. LIST_HEAD(freelist);
  146. if (pfn_valid(start_pfn))
  147. zone = page_zone(pfn_to_page(start_pfn));
  148. for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
  149. if (!pfn_valid(pfn) || zone != page_zone(pfn_to_page(pfn)))
  150. break;
  151. /*
  152. * On subsequent iterations ALIGN() is actually not needed,
  153. * but we keep it that we not to complicate the code.
  154. */
  155. block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
  156. block_end_pfn = min(block_end_pfn, end_pfn);
  157. spin_lock_irqsave(&zone->lock, flags);
  158. isolated = isolate_freepages_block(pfn, block_end_pfn,
  159. &freelist, true);
  160. spin_unlock_irqrestore(&zone->lock, flags);
  161. /*
  162. * In strict mode, isolate_freepages_block() returns 0 if
  163. * there are any holes in the block (ie. invalid PFNs or
  164. * non-free pages).
  165. */
  166. if (!isolated)
  167. break;
  168. /*
  169. * If we managed to isolate pages, it is always (1 << n) *
  170. * pageblock_nr_pages for some non-negative n. (Max order
  171. * page may span two pageblocks).
  172. */
  173. }
  174. /* split_free_page does not map the pages */
  175. map_pages(&freelist);
  176. if (pfn < end_pfn) {
  177. /* Loop terminated early, cleanup. */
  178. release_freepages(&freelist);
  179. return 0;
  180. }
  181. /* We don't use freelists for anything. */
  182. return pfn;
  183. }
  184. /* Update the number of anon and file isolated pages in the zone */
  185. static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc)
  186. {
  187. struct page *page;
  188. unsigned int count[2] = { 0, };
  189. list_for_each_entry(page, &cc->migratepages, lru)
  190. count[!!page_is_file_cache(page)]++;
  191. /* If locked we can use the interrupt unsafe versions */
  192. if (locked) {
  193. __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
  194. __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
  195. } else {
  196. mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
  197. mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
  198. }
  199. }
  200. /* Similar to reclaim, but different enough that they don't share logic */
  201. static bool too_many_isolated(struct zone *zone)
  202. {
  203. unsigned long active, inactive, isolated;
  204. inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
  205. zone_page_state(zone, NR_INACTIVE_ANON);
  206. active = zone_page_state(zone, NR_ACTIVE_FILE) +
  207. zone_page_state(zone, NR_ACTIVE_ANON);
  208. isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
  209. zone_page_state(zone, NR_ISOLATED_ANON);
  210. return isolated > (inactive + active) / 2;
  211. }
  212. /**
  213. * isolate_migratepages_range() - isolate all migrate-able pages in range.
  214. * @zone: Zone pages are in.
  215. * @cc: Compaction control structure.
  216. * @low_pfn: The first PFN of the range.
  217. * @end_pfn: The one-past-the-last PFN of the range.
  218. *
  219. * Isolate all pages that can be migrated from the range specified by
  220. * [low_pfn, end_pfn). Returns zero if there is a fatal signal
  221. * pending), otherwise PFN of the first page that was not scanned
  222. * (which may be both less, equal to or more then end_pfn).
  223. *
  224. * Assumes that cc->migratepages is empty and cc->nr_migratepages is
  225. * zero.
  226. *
  227. * Apart from cc->migratepages and cc->nr_migratetypes this function
  228. * does not modify any cc's fields, in particular it does not modify
  229. * (or read for that matter) cc->migrate_pfn.
  230. */
  231. unsigned long
  232. isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
  233. unsigned long low_pfn, unsigned long end_pfn)
  234. {
  235. unsigned long last_pageblock_nr = 0, pageblock_nr;
  236. unsigned long nr_scanned = 0, nr_isolated = 0;
  237. struct list_head *migratelist = &cc->migratepages;
  238. isolate_mode_t mode = 0;
  239. struct lruvec *lruvec;
  240. unsigned long flags;
  241. bool locked;
  242. /*
  243. * Ensure that there are not too many pages isolated from the LRU
  244. * list by either parallel reclaimers or compaction. If there are,
  245. * delay for some time until fewer pages are isolated
  246. */
  247. while (unlikely(too_many_isolated(zone))) {
  248. /* async migration should just abort */
  249. if (!cc->sync)
  250. return 0;
  251. congestion_wait(BLK_RW_ASYNC, HZ/10);
  252. if (fatal_signal_pending(current))
  253. return 0;
  254. }
  255. /* Time to isolate some pages for migration */
  256. cond_resched();
  257. spin_lock_irqsave(&zone->lru_lock, flags);
  258. locked = true;
  259. for (; low_pfn < end_pfn; low_pfn++) {
  260. struct page *page;
  261. /* give a chance to irqs before checking need_resched() */
  262. if (!((low_pfn+1) % SWAP_CLUSTER_MAX)) {
  263. spin_unlock_irqrestore(&zone->lru_lock, flags);
  264. locked = false;
  265. }
  266. /* Check if it is ok to still hold the lock */
  267. locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
  268. locked, cc);
  269. if (!locked)
  270. break;
  271. /*
  272. * migrate_pfn does not necessarily start aligned to a
  273. * pageblock. Ensure that pfn_valid is called when moving
  274. * into a new MAX_ORDER_NR_PAGES range in case of large
  275. * memory holes within the zone
  276. */
  277. if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
  278. if (!pfn_valid(low_pfn)) {
  279. low_pfn += MAX_ORDER_NR_PAGES - 1;
  280. continue;
  281. }
  282. }
  283. if (!pfn_valid_within(low_pfn))
  284. continue;
  285. nr_scanned++;
  286. /*
  287. * Get the page and ensure the page is within the same zone.
  288. * See the comment in isolate_freepages about overlapping
  289. * nodes. It is deliberate that the new zone lock is not taken
  290. * as memory compaction should not move pages between nodes.
  291. */
  292. page = pfn_to_page(low_pfn);
  293. if (page_zone(page) != zone)
  294. continue;
  295. /* Skip if free */
  296. if (PageBuddy(page))
  297. continue;
  298. /*
  299. * For async migration, also only scan in MOVABLE blocks. Async
  300. * migration is optimistic to see if the minimum amount of work
  301. * satisfies the allocation
  302. */
  303. pageblock_nr = low_pfn >> pageblock_order;
  304. if (!cc->sync && last_pageblock_nr != pageblock_nr &&
  305. !migrate_async_suitable(get_pageblock_migratetype(page))) {
  306. low_pfn += pageblock_nr_pages;
  307. low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
  308. last_pageblock_nr = pageblock_nr;
  309. continue;
  310. }
  311. if (!PageLRU(page))
  312. continue;
  313. /*
  314. * PageLRU is set, and lru_lock excludes isolation,
  315. * splitting and collapsing (collapsing has already
  316. * happened if PageLRU is set).
  317. */
  318. if (PageTransHuge(page)) {
  319. low_pfn += (1 << compound_order(page)) - 1;
  320. continue;
  321. }
  322. if (!cc->sync)
  323. mode |= ISOLATE_ASYNC_MIGRATE;
  324. lruvec = mem_cgroup_page_lruvec(page, zone);
  325. /* Try isolate the page */
  326. if (__isolate_lru_page(page, mode) != 0)
  327. continue;
  328. VM_BUG_ON(PageTransCompound(page));
  329. /* Successfully isolated */
  330. del_page_from_lru_list(page, lruvec, page_lru(page));
  331. list_add(&page->lru, migratelist);
  332. cc->nr_migratepages++;
  333. nr_isolated++;
  334. /* Avoid isolating too much */
  335. if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
  336. ++low_pfn;
  337. break;
  338. }
  339. }
  340. acct_isolated(zone, locked, cc);
  341. if (locked)
  342. spin_unlock_irqrestore(&zone->lru_lock, flags);
  343. trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
  344. return low_pfn;
  345. }
  346. #endif /* CONFIG_COMPACTION || CONFIG_CMA */
  347. #ifdef CONFIG_COMPACTION
  348. /* Returns true if the page is within a block suitable for migration to */
  349. static bool suitable_migration_target(struct page *page)
  350. {
  351. int migratetype = get_pageblock_migratetype(page);
  352. /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
  353. if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
  354. return false;
  355. /* If the page is a large free page, then allow migration */
  356. if (PageBuddy(page) && page_order(page) >= pageblock_order)
  357. return true;
  358. /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
  359. if (migrate_async_suitable(migratetype))
  360. return true;
  361. /* Otherwise skip the block */
  362. return false;
  363. }
  364. /*
  365. * Returns the start pfn of the last page block in a zone. This is the starting
  366. * point for full compaction of a zone. Compaction searches for free pages from
  367. * the end of each zone, while isolate_freepages_block scans forward inside each
  368. * page block.
  369. */
  370. static unsigned long start_free_pfn(struct zone *zone)
  371. {
  372. unsigned long free_pfn;
  373. free_pfn = zone->zone_start_pfn + zone->spanned_pages;
  374. free_pfn &= ~(pageblock_nr_pages-1);
  375. return free_pfn;
  376. }
  377. /*
  378. * Based on information in the current compact_control, find blocks
  379. * suitable for isolating free pages from and then isolate them.
  380. */
  381. static void isolate_freepages(struct zone *zone,
  382. struct compact_control *cc)
  383. {
  384. struct page *page;
  385. unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
  386. unsigned long flags;
  387. int nr_freepages = cc->nr_freepages;
  388. struct list_head *freelist = &cc->freepages;
  389. /*
  390. * Initialise the free scanner. The starting point is where we last
  391. * scanned from (or the end of the zone if starting). The low point
  392. * is the end of the pageblock the migration scanner is using.
  393. */
  394. pfn = cc->free_pfn;
  395. low_pfn = cc->migrate_pfn + pageblock_nr_pages;
  396. /*
  397. * Take care that if the migration scanner is at the end of the zone
  398. * that the free scanner does not accidentally move to the next zone
  399. * in the next isolation cycle.
  400. */
  401. high_pfn = min(low_pfn, pfn);
  402. zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
  403. /*
  404. * Isolate free pages until enough are available to migrate the
  405. * pages on cc->migratepages. We stop searching if the migrate
  406. * and free page scanners meet or enough free pages are isolated.
  407. */
  408. for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
  409. pfn -= pageblock_nr_pages) {
  410. unsigned long isolated;
  411. if (!pfn_valid(pfn))
  412. continue;
  413. /*
  414. * Check for overlapping nodes/zones. It's possible on some
  415. * configurations to have a setup like
  416. * node0 node1 node0
  417. * i.e. it's possible that all pages within a zones range of
  418. * pages do not belong to a single zone.
  419. */
  420. page = pfn_to_page(pfn);
  421. if (page_zone(page) != zone)
  422. continue;
  423. /* Check the block is suitable for migration */
  424. if (!suitable_migration_target(page))
  425. continue;
  426. /*
  427. * Found a block suitable for isolating free pages from. Now
  428. * we disabled interrupts, double check things are ok and
  429. * isolate the pages. This is to minimise the time IRQs
  430. * are disabled
  431. */
  432. isolated = 0;
  433. /*
  434. * The zone lock must be held to isolate freepages. This
  435. * unfortunately this is a very coarse lock and can be
  436. * heavily contended if there are parallel allocations
  437. * or parallel compactions. For async compaction do not
  438. * spin on the lock
  439. */
  440. if (!compact_trylock_irqsave(&zone->lock, &flags, cc))
  441. break;
  442. if (suitable_migration_target(page)) {
  443. end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn);
  444. isolated = isolate_freepages_block(pfn, end_pfn,
  445. freelist, false);
  446. nr_freepages += isolated;
  447. }
  448. spin_unlock_irqrestore(&zone->lock, flags);
  449. /*
  450. * Record the highest PFN we isolated pages from. When next
  451. * looking for free pages, the search will restart here as
  452. * page migration may have returned some pages to the allocator
  453. */
  454. if (isolated) {
  455. high_pfn = max(high_pfn, pfn);
  456. /*
  457. * If the free scanner has wrapped, update
  458. * compact_cached_free_pfn to point to the highest
  459. * pageblock with free pages. This reduces excessive
  460. * scanning of full pageblocks near the end of the
  461. * zone
  462. */
  463. if (cc->order > 0 && cc->wrapped)
  464. zone->compact_cached_free_pfn = high_pfn;
  465. }
  466. }
  467. /* split_free_page does not map the pages */
  468. map_pages(freelist);
  469. cc->free_pfn = high_pfn;
  470. cc->nr_freepages = nr_freepages;
  471. /* If compact_cached_free_pfn is reset then set it now */
  472. if (cc->order > 0 && !cc->wrapped &&
  473. zone->compact_cached_free_pfn == start_free_pfn(zone))
  474. zone->compact_cached_free_pfn = high_pfn;
  475. }
  476. /*
  477. * This is a migrate-callback that "allocates" freepages by taking pages
  478. * from the isolated freelists in the block we are migrating to.
  479. */
  480. static struct page *compaction_alloc(struct page *migratepage,
  481. unsigned long data,
  482. int **result)
  483. {
  484. struct compact_control *cc = (struct compact_control *)data;
  485. struct page *freepage;
  486. /* Isolate free pages if necessary */
  487. if (list_empty(&cc->freepages)) {
  488. isolate_freepages(cc->zone, cc);
  489. if (list_empty(&cc->freepages))
  490. return NULL;
  491. }
  492. freepage = list_entry(cc->freepages.next, struct page, lru);
  493. list_del(&freepage->lru);
  494. cc->nr_freepages--;
  495. return freepage;
  496. }
  497. /*
  498. * We cannot control nr_migratepages and nr_freepages fully when migration is
  499. * running as migrate_pages() has no knowledge of compact_control. When
  500. * migration is complete, we count the number of pages on the lists by hand.
  501. */
  502. static void update_nr_listpages(struct compact_control *cc)
  503. {
  504. int nr_migratepages = 0;
  505. int nr_freepages = 0;
  506. struct page *page;
  507. list_for_each_entry(page, &cc->migratepages, lru)
  508. nr_migratepages++;
  509. list_for_each_entry(page, &cc->freepages, lru)
  510. nr_freepages++;
  511. cc->nr_migratepages = nr_migratepages;
  512. cc->nr_freepages = nr_freepages;
  513. }
  514. /* possible outcome of isolate_migratepages */
  515. typedef enum {
  516. ISOLATE_ABORT, /* Abort compaction now */
  517. ISOLATE_NONE, /* No pages isolated, continue scanning */
  518. ISOLATE_SUCCESS, /* Pages isolated, migrate */
  519. } isolate_migrate_t;
  520. /*
  521. * Isolate all pages that can be migrated from the block pointed to by
  522. * the migrate scanner within compact_control.
  523. */
  524. static isolate_migrate_t isolate_migratepages(struct zone *zone,
  525. struct compact_control *cc)
  526. {
  527. unsigned long low_pfn, end_pfn;
  528. /* Do not scan outside zone boundaries */
  529. low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
  530. /* Only scan within a pageblock boundary */
  531. end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
  532. /* Do not cross the free scanner or scan within a memory hole */
  533. if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
  534. cc->migrate_pfn = end_pfn;
  535. return ISOLATE_NONE;
  536. }
  537. /* Perform the isolation */
  538. low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn);
  539. if (!low_pfn)
  540. return ISOLATE_ABORT;
  541. cc->migrate_pfn = low_pfn;
  542. return ISOLATE_SUCCESS;
  543. }
  544. static int compact_finished(struct zone *zone,
  545. struct compact_control *cc)
  546. {
  547. unsigned int order;
  548. unsigned long watermark;
  549. if (fatal_signal_pending(current))
  550. return COMPACT_PARTIAL;
  551. /*
  552. * A full (order == -1) compaction run starts at the beginning and
  553. * end of a zone; it completes when the migrate and free scanner meet.
  554. * A partial (order > 0) compaction can start with the free scanner
  555. * at a random point in the zone, and may have to restart.
  556. */
  557. if (cc->free_pfn <= cc->migrate_pfn) {
  558. if (cc->order > 0 && !cc->wrapped) {
  559. /* We started partway through; restart at the end. */
  560. unsigned long free_pfn = start_free_pfn(zone);
  561. zone->compact_cached_free_pfn = free_pfn;
  562. cc->free_pfn = free_pfn;
  563. cc->wrapped = 1;
  564. return COMPACT_CONTINUE;
  565. }
  566. return COMPACT_COMPLETE;
  567. }
  568. /* We wrapped around and ended up where we started. */
  569. if (cc->wrapped && cc->free_pfn <= cc->start_free_pfn)
  570. return COMPACT_COMPLETE;
  571. /*
  572. * order == -1 is expected when compacting via
  573. * /proc/sys/vm/compact_memory
  574. */
  575. if (cc->order == -1)
  576. return COMPACT_CONTINUE;
  577. /* Compaction run is not finished if the watermark is not met */
  578. watermark = low_wmark_pages(zone);
  579. watermark += (1 << cc->order);
  580. if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
  581. return COMPACT_CONTINUE;
  582. /* Direct compactor: Is a suitable page free? */
  583. for (order = cc->order; order < MAX_ORDER; order++) {
  584. /* Job done if page is free of the right migratetype */
  585. if (!list_empty(&zone->free_area[order].free_list[cc->migratetype]))
  586. return COMPACT_PARTIAL;
  587. /* Job done if allocation would set block type */
  588. if (order >= pageblock_order && zone->free_area[order].nr_free)
  589. return COMPACT_PARTIAL;
  590. }
  591. return COMPACT_CONTINUE;
  592. }
  593. /*
  594. * compaction_suitable: Is this suitable to run compaction on this zone now?
  595. * Returns
  596. * COMPACT_SKIPPED - If there are too few free pages for compaction
  597. * COMPACT_PARTIAL - If the allocation would succeed without compaction
  598. * COMPACT_CONTINUE - If compaction should run now
  599. */
  600. unsigned long compaction_suitable(struct zone *zone, int order)
  601. {
  602. int fragindex;
  603. unsigned long watermark;
  604. /*
  605. * order == -1 is expected when compacting via
  606. * /proc/sys/vm/compact_memory
  607. */
  608. if (order == -1)
  609. return COMPACT_CONTINUE;
  610. /*
  611. * Watermarks for order-0 must be met for compaction. Note the 2UL.
  612. * This is because during migration, copies of pages need to be
  613. * allocated and for a short time, the footprint is higher
  614. */
  615. watermark = low_wmark_pages(zone) + (2UL << order);
  616. if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
  617. return COMPACT_SKIPPED;
  618. /*
  619. * fragmentation index determines if allocation failures are due to
  620. * low memory or external fragmentation
  621. *
  622. * index of -1000 implies allocations might succeed depending on
  623. * watermarks
  624. * index towards 0 implies failure is due to lack of memory
  625. * index towards 1000 implies failure is due to fragmentation
  626. *
  627. * Only compact if a failure would be due to fragmentation.
  628. */
  629. fragindex = fragmentation_index(zone, order);
  630. if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
  631. return COMPACT_SKIPPED;
  632. if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
  633. 0, 0))
  634. return COMPACT_PARTIAL;
  635. return COMPACT_CONTINUE;
  636. }
  637. static int compact_zone(struct zone *zone, struct compact_control *cc)
  638. {
  639. int ret;
  640. ret = compaction_suitable(zone, cc->order);
  641. switch (ret) {
  642. case COMPACT_PARTIAL:
  643. case COMPACT_SKIPPED:
  644. /* Compaction is likely to fail */
  645. return ret;
  646. case COMPACT_CONTINUE:
  647. /* Fall through to compaction */
  648. ;
  649. }
  650. /* Setup to move all movable pages to the end of the zone */
  651. cc->migrate_pfn = zone->zone_start_pfn;
  652. if (cc->order > 0) {
  653. /* Incremental compaction. Start where the last one stopped. */
  654. cc->free_pfn = zone->compact_cached_free_pfn;
  655. cc->start_free_pfn = cc->free_pfn;
  656. } else {
  657. /* Order == -1 starts at the end of the zone. */
  658. cc->free_pfn = start_free_pfn(zone);
  659. }
  660. migrate_prep_local();
  661. while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
  662. unsigned long nr_migrate, nr_remaining;
  663. int err;
  664. switch (isolate_migratepages(zone, cc)) {
  665. case ISOLATE_ABORT:
  666. ret = COMPACT_PARTIAL;
  667. goto out;
  668. case ISOLATE_NONE:
  669. continue;
  670. case ISOLATE_SUCCESS:
  671. ;
  672. }
  673. nr_migrate = cc->nr_migratepages;
  674. err = migrate_pages(&cc->migratepages, compaction_alloc,
  675. (unsigned long)cc, false,
  676. cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
  677. update_nr_listpages(cc);
  678. nr_remaining = cc->nr_migratepages;
  679. count_vm_event(COMPACTBLOCKS);
  680. count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
  681. if (nr_remaining)
  682. count_vm_events(COMPACTPAGEFAILED, nr_remaining);
  683. trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
  684. nr_remaining);
  685. /* Release LRU pages not migrated */
  686. if (err) {
  687. putback_lru_pages(&cc->migratepages);
  688. cc->nr_migratepages = 0;
  689. if (err == -ENOMEM) {
  690. ret = COMPACT_PARTIAL;
  691. goto out;
  692. }
  693. }
  694. }
  695. out:
  696. /* Release free pages and check accounting */
  697. cc->nr_freepages -= release_freepages(&cc->freepages);
  698. VM_BUG_ON(cc->nr_freepages != 0);
  699. return ret;
  700. }
  701. static unsigned long compact_zone_order(struct zone *zone,
  702. int order, gfp_t gfp_mask,
  703. bool sync, bool *contended)
  704. {
  705. struct compact_control cc = {
  706. .nr_freepages = 0,
  707. .nr_migratepages = 0,
  708. .order = order,
  709. .migratetype = allocflags_to_migratetype(gfp_mask),
  710. .zone = zone,
  711. .sync = sync,
  712. .contended = contended,
  713. };
  714. INIT_LIST_HEAD(&cc.freepages);
  715. INIT_LIST_HEAD(&cc.migratepages);
  716. return compact_zone(zone, &cc);
  717. }
  718. int sysctl_extfrag_threshold = 500;
  719. /**
  720. * try_to_compact_pages - Direct compact to satisfy a high-order allocation
  721. * @zonelist: The zonelist used for the current allocation
  722. * @order: The order of the current allocation
  723. * @gfp_mask: The GFP mask of the current allocation
  724. * @nodemask: The allowed nodes to allocate from
  725. * @sync: Whether migration is synchronous or not
  726. *
  727. * This is the main entry point for direct page compaction.
  728. */
  729. unsigned long try_to_compact_pages(struct zonelist *zonelist,
  730. int order, gfp_t gfp_mask, nodemask_t *nodemask,
  731. bool sync, bool *contended)
  732. {
  733. enum zone_type high_zoneidx = gfp_zone(gfp_mask);
  734. int may_enter_fs = gfp_mask & __GFP_FS;
  735. int may_perform_io = gfp_mask & __GFP_IO;
  736. struct zoneref *z;
  737. struct zone *zone;
  738. int rc = COMPACT_SKIPPED;
  739. /* Check if the GFP flags allow compaction */
  740. if (!order || !may_enter_fs || !may_perform_io)
  741. return rc;
  742. count_vm_event(COMPACTSTALL);
  743. /* Compact each zone in the list */
  744. for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
  745. nodemask) {
  746. int status;
  747. status = compact_zone_order(zone, order, gfp_mask, sync,
  748. contended);
  749. rc = max(status, rc);
  750. /* If a normal allocation would succeed, stop compacting */
  751. if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0, 0))
  752. break;
  753. }
  754. return rc;
  755. }
  756. /* Compact all zones within a node */
  757. static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
  758. {
  759. int zoneid;
  760. struct zone *zone;
  761. for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
  762. zone = &pgdat->node_zones[zoneid];
  763. if (!populated_zone(zone))
  764. continue;
  765. cc->nr_freepages = 0;
  766. cc->nr_migratepages = 0;
  767. cc->zone = zone;
  768. INIT_LIST_HEAD(&cc->freepages);
  769. INIT_LIST_HEAD(&cc->migratepages);
  770. if (cc->order == -1 || !compaction_deferred(zone, cc->order))
  771. compact_zone(zone, cc);
  772. if (cc->order > 0) {
  773. int ok = zone_watermark_ok(zone, cc->order,
  774. low_wmark_pages(zone), 0, 0);
  775. if (ok && cc->order >= zone->compact_order_failed)
  776. zone->compact_order_failed = cc->order + 1;
  777. /* Currently async compaction is never deferred. */
  778. else if (!ok && cc->sync)
  779. defer_compaction(zone, cc->order);
  780. }
  781. VM_BUG_ON(!list_empty(&cc->freepages));
  782. VM_BUG_ON(!list_empty(&cc->migratepages));
  783. }
  784. return 0;
  785. }
  786. int compact_pgdat(pg_data_t *pgdat, int order)
  787. {
  788. struct compact_control cc = {
  789. .order = order,
  790. .sync = false,
  791. };
  792. return __compact_pgdat(pgdat, &cc);
  793. }
  794. static int compact_node(int nid)
  795. {
  796. struct compact_control cc = {
  797. .order = -1,
  798. .sync = true,
  799. };
  800. return __compact_pgdat(NODE_DATA(nid), &cc);
  801. }
  802. /* Compact all nodes in the system */
  803. static int compact_nodes(void)
  804. {
  805. int nid;
  806. /* Flush pending updates to the LRU lists */
  807. lru_add_drain_all();
  808. for_each_online_node(nid)
  809. compact_node(nid);
  810. return COMPACT_COMPLETE;
  811. }
  812. /* The written value is actually unused, all memory is compacted */
  813. int sysctl_compact_memory;
  814. /* This is the entry point for compacting all nodes via /proc/sys/vm */
  815. int sysctl_compaction_handler(struct ctl_table *table, int write,
  816. void __user *buffer, size_t *length, loff_t *ppos)
  817. {
  818. if (write)
  819. return compact_nodes();
  820. return 0;
  821. }
  822. int sysctl_extfrag_handler(struct ctl_table *table, int write,
  823. void __user *buffer, size_t *length, loff_t *ppos)
  824. {
  825. proc_dointvec_minmax(table, write, buffer, length, ppos);
  826. return 0;
  827. }
  828. #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
  829. ssize_t sysfs_compact_node(struct device *dev,
  830. struct device_attribute *attr,
  831. const char *buf, size_t count)
  832. {
  833. int nid = dev->id;
  834. if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
  835. /* Flush pending updates to the LRU lists */
  836. lru_add_drain_all();
  837. compact_node(nid);
  838. }
  839. return count;
  840. }
  841. static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
  842. int compaction_register_node(struct node *node)
  843. {
  844. return device_create_file(&node->dev, &dev_attr_compact);
  845. }
  846. void compaction_unregister_node(struct node *node)
  847. {
  848. return device_remove_file(&node->dev, &dev_attr_compact);
  849. }
  850. #endif /* CONFIG_SYSFS && CONFIG_NUMA */
  851. #endif /* CONFIG_COMPACTION */