compaction.c 29 KB

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