compaction.c 29 KB

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