compaction.c 34 KB

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