compaction.c 33 KB

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