compaction.c 32 KB

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