compaction.c 33 KB

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