compaction.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961
  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/cpu.h>
  11. #include <linux/swap.h>
  12. #include <linux/migrate.h>
  13. #include <linux/compaction.h>
  14. #include <linux/mm_inline.h>
  15. #include <linux/backing-dev.h>
  16. #include <linux/sysctl.h>
  17. #include <linux/sysfs.h>
  18. #include <linux/balloon_compaction.h>
  19. #include <linux/page-isolation.h>
  20. #include <linux/kasan.h>
  21. #include <linux/kthread.h>
  22. #include <linux/freezer.h>
  23. #include "internal.h"
  24. #ifdef CONFIG_COMPACTION
  25. static inline void count_compact_event(enum vm_event_item item)
  26. {
  27. count_vm_event(item);
  28. }
  29. static inline void count_compact_events(enum vm_event_item item, long delta)
  30. {
  31. count_vm_events(item, delta);
  32. }
  33. #else
  34. #define count_compact_event(item) do { } while (0)
  35. #define count_compact_events(item, delta) do { } while (0)
  36. #endif
  37. #if defined CONFIG_COMPACTION || defined CONFIG_CMA
  38. #define CREATE_TRACE_POINTS
  39. #include <trace/events/compaction.h>
  40. static unsigned long release_freepages(struct list_head *freelist)
  41. {
  42. struct page *page, *next;
  43. unsigned long high_pfn = 0;
  44. list_for_each_entry_safe(page, next, freelist, lru) {
  45. unsigned long pfn = page_to_pfn(page);
  46. list_del(&page->lru);
  47. __free_page(page);
  48. if (pfn > high_pfn)
  49. high_pfn = pfn;
  50. }
  51. return high_pfn;
  52. }
  53. static void map_pages(struct list_head *list)
  54. {
  55. struct page *page;
  56. list_for_each_entry(page, list, lru) {
  57. arch_alloc_page(page, 0);
  58. kernel_map_pages(page, 1, 1);
  59. kasan_alloc_pages(page, 0);
  60. }
  61. }
  62. static inline bool migrate_async_suitable(int migratetype)
  63. {
  64. return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
  65. }
  66. #ifdef CONFIG_COMPACTION
  67. /* Do not skip compaction more than 64 times */
  68. #define COMPACT_MAX_DEFER_SHIFT 6
  69. /*
  70. * Compaction is deferred when compaction fails to result in a page
  71. * allocation success. 1 << compact_defer_limit compactions are skipped up
  72. * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT
  73. */
  74. void defer_compaction(struct zone *zone, int order)
  75. {
  76. zone->compact_considered = 0;
  77. zone->compact_defer_shift++;
  78. if (order < zone->compact_order_failed)
  79. zone->compact_order_failed = order;
  80. if (zone->compact_defer_shift > COMPACT_MAX_DEFER_SHIFT)
  81. zone->compact_defer_shift = COMPACT_MAX_DEFER_SHIFT;
  82. trace_mm_compaction_defer_compaction(zone, order);
  83. }
  84. /* Returns true if compaction should be skipped this time */
  85. bool compaction_deferred(struct zone *zone, int order)
  86. {
  87. unsigned long defer_limit = 1UL << zone->compact_defer_shift;
  88. if (order < zone->compact_order_failed)
  89. return false;
  90. /* Avoid possible overflow */
  91. if (++zone->compact_considered > defer_limit)
  92. zone->compact_considered = defer_limit;
  93. if (zone->compact_considered >= defer_limit)
  94. return false;
  95. trace_mm_compaction_deferred(zone, order);
  96. return true;
  97. }
  98. /*
  99. * Update defer tracking counters after successful compaction of given order,
  100. * which means an allocation either succeeded (alloc_success == true) or is
  101. * expected to succeed.
  102. */
  103. void compaction_defer_reset(struct zone *zone, int order,
  104. bool alloc_success)
  105. {
  106. if (alloc_success) {
  107. zone->compact_considered = 0;
  108. zone->compact_defer_shift = 0;
  109. }
  110. if (order >= zone->compact_order_failed)
  111. zone->compact_order_failed = order + 1;
  112. trace_mm_compaction_defer_reset(zone, order);
  113. }
  114. /* Returns true if restarting compaction after many failures */
  115. bool compaction_restarting(struct zone *zone, int order)
  116. {
  117. if (order < zone->compact_order_failed)
  118. return false;
  119. return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT &&
  120. zone->compact_considered >= 1UL << zone->compact_defer_shift;
  121. }
  122. /* Returns true if the pageblock should be scanned for pages to isolate. */
  123. static inline bool isolation_suitable(struct compact_control *cc,
  124. struct page *page)
  125. {
  126. if (cc->ignore_skip_hint)
  127. return true;
  128. return !get_pageblock_skip(page);
  129. }
  130. static void reset_cached_positions(struct zone *zone)
  131. {
  132. zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
  133. zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
  134. zone->compact_cached_free_pfn =
  135. round_down(zone_end_pfn(zone) - 1, pageblock_nr_pages);
  136. }
  137. /*
  138. * This function is called to clear all cached information on pageblocks that
  139. * should be skipped for page isolation when the migrate and free page scanner
  140. * meet.
  141. */
  142. static void __reset_isolation_suitable(struct zone *zone)
  143. {
  144. unsigned long start_pfn = zone->zone_start_pfn;
  145. unsigned long end_pfn = zone_end_pfn(zone);
  146. unsigned long pfn;
  147. zone->compact_blockskip_flush = false;
  148. /* Walk the zone and mark every pageblock as suitable for isolation */
  149. for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
  150. struct page *page;
  151. cond_resched();
  152. if (!pfn_valid(pfn))
  153. continue;
  154. page = pfn_to_page(pfn);
  155. if (zone != page_zone(page))
  156. continue;
  157. clear_pageblock_skip(page);
  158. }
  159. reset_cached_positions(zone);
  160. }
  161. void reset_isolation_suitable(pg_data_t *pgdat)
  162. {
  163. int zoneid;
  164. for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
  165. struct zone *zone = &pgdat->node_zones[zoneid];
  166. if (!populated_zone(zone))
  167. continue;
  168. /* Only flush if a full compaction finished recently */
  169. if (zone->compact_blockskip_flush)
  170. __reset_isolation_suitable(zone);
  171. }
  172. }
  173. /*
  174. * If no pages were isolated then mark this pageblock to be skipped in the
  175. * future. The information is later cleared by __reset_isolation_suitable().
  176. */
  177. static void update_pageblock_skip(struct compact_control *cc,
  178. struct page *page, unsigned long nr_isolated,
  179. bool migrate_scanner)
  180. {
  181. struct zone *zone = cc->zone;
  182. unsigned long pfn;
  183. if (cc->ignore_skip_hint)
  184. return;
  185. if (!page)
  186. return;
  187. if (nr_isolated)
  188. return;
  189. set_pageblock_skip(page);
  190. pfn = page_to_pfn(page);
  191. /* Update where async and sync compaction should restart */
  192. if (migrate_scanner) {
  193. if (pfn > zone->compact_cached_migrate_pfn[0])
  194. zone->compact_cached_migrate_pfn[0] = pfn;
  195. if (cc->mode != MIGRATE_ASYNC &&
  196. pfn > zone->compact_cached_migrate_pfn[1])
  197. zone->compact_cached_migrate_pfn[1] = pfn;
  198. } else {
  199. if (pfn < zone->compact_cached_free_pfn)
  200. zone->compact_cached_free_pfn = pfn;
  201. }
  202. }
  203. #else
  204. static inline bool isolation_suitable(struct compact_control *cc,
  205. struct page *page)
  206. {
  207. return true;
  208. }
  209. static void update_pageblock_skip(struct compact_control *cc,
  210. struct page *page, unsigned long nr_isolated,
  211. bool migrate_scanner)
  212. {
  213. }
  214. #endif /* CONFIG_COMPACTION */
  215. /*
  216. * Compaction requires the taking of some coarse locks that are potentially
  217. * very heavily contended. For async compaction, back out if the lock cannot
  218. * be taken immediately. For sync compaction, spin on the lock if needed.
  219. *
  220. * Returns true if the lock is held
  221. * Returns false if the lock is not held and compaction should abort
  222. */
  223. static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags,
  224. struct compact_control *cc)
  225. {
  226. if (cc->mode == MIGRATE_ASYNC) {
  227. if (!spin_trylock_irqsave(lock, *flags)) {
  228. cc->contended = COMPACT_CONTENDED_LOCK;
  229. return false;
  230. }
  231. } else {
  232. spin_lock_irqsave(lock, *flags);
  233. }
  234. return true;
  235. }
  236. /*
  237. * Compaction requires the taking of some coarse locks that are potentially
  238. * very heavily contended. The lock should be periodically unlocked to avoid
  239. * having disabled IRQs for a long time, even when there is nobody waiting on
  240. * the lock. It might also be that allowing the IRQs will result in
  241. * need_resched() becoming true. If scheduling is needed, async compaction
  242. * aborts. Sync compaction schedules.
  243. * Either compaction type will also abort if a fatal signal is pending.
  244. * In either case if the lock was locked, it is dropped and not regained.
  245. *
  246. * Returns true if compaction should abort due to fatal signal pending, or
  247. * async compaction due to need_resched()
  248. * Returns false when compaction can continue (sync compaction might have
  249. * scheduled)
  250. */
  251. static bool compact_unlock_should_abort(spinlock_t *lock,
  252. unsigned long flags, bool *locked, struct compact_control *cc)
  253. {
  254. if (*locked) {
  255. spin_unlock_irqrestore(lock, flags);
  256. *locked = false;
  257. }
  258. if (fatal_signal_pending(current)) {
  259. cc->contended = COMPACT_CONTENDED_SCHED;
  260. return true;
  261. }
  262. if (need_resched()) {
  263. if (cc->mode == MIGRATE_ASYNC) {
  264. cc->contended = COMPACT_CONTENDED_SCHED;
  265. return true;
  266. }
  267. cond_resched();
  268. }
  269. return false;
  270. }
  271. /*
  272. * Aside from avoiding lock contention, compaction also periodically checks
  273. * need_resched() and either schedules in sync compaction or aborts async
  274. * compaction. This is similar to what compact_unlock_should_abort() does, but
  275. * is used where no lock is concerned.
  276. *
  277. * Returns false when no scheduling was needed, or sync compaction scheduled.
  278. * Returns true when async compaction should abort.
  279. */
  280. static inline bool compact_should_abort(struct compact_control *cc)
  281. {
  282. /* async compaction aborts if contended */
  283. if (need_resched()) {
  284. if (cc->mode == MIGRATE_ASYNC) {
  285. cc->contended = COMPACT_CONTENDED_SCHED;
  286. return true;
  287. }
  288. cond_resched();
  289. }
  290. return false;
  291. }
  292. /*
  293. * Isolate free pages onto a private freelist. If @strict is true, will abort
  294. * returning 0 on any invalid PFNs or non-free pages inside of the pageblock
  295. * (even though it may still end up isolating some pages).
  296. */
  297. static unsigned long isolate_freepages_block(struct compact_control *cc,
  298. unsigned long *start_pfn,
  299. unsigned long end_pfn,
  300. struct list_head *freelist,
  301. bool strict)
  302. {
  303. int nr_scanned = 0, total_isolated = 0;
  304. struct page *cursor, *valid_page = NULL;
  305. unsigned long flags = 0;
  306. bool locked = false;
  307. unsigned long blockpfn = *start_pfn;
  308. cursor = pfn_to_page(blockpfn);
  309. /* Isolate free pages. */
  310. for (; blockpfn < end_pfn; blockpfn++, cursor++) {
  311. int isolated, i;
  312. struct page *page = cursor;
  313. /*
  314. * Periodically drop the lock (if held) regardless of its
  315. * contention, to give chance to IRQs. Abort if fatal signal
  316. * pending or async compaction detects need_resched()
  317. */
  318. if (!(blockpfn % SWAP_CLUSTER_MAX)
  319. && compact_unlock_should_abort(&cc->zone->lock, flags,
  320. &locked, cc))
  321. break;
  322. nr_scanned++;
  323. if (!pfn_valid_within(blockpfn))
  324. goto isolate_fail;
  325. if (!valid_page)
  326. valid_page = page;
  327. /*
  328. * For compound pages such as THP and hugetlbfs, we can save
  329. * potentially a lot of iterations if we skip them at once.
  330. * The check is racy, but we can consider only valid values
  331. * and the only danger is skipping too much.
  332. */
  333. if (PageCompound(page)) {
  334. unsigned int comp_order = compound_order(page);
  335. if (likely(comp_order < MAX_ORDER)) {
  336. blockpfn += (1UL << comp_order) - 1;
  337. cursor += (1UL << comp_order) - 1;
  338. }
  339. goto isolate_fail;
  340. }
  341. if (!PageBuddy(page))
  342. goto isolate_fail;
  343. /*
  344. * If we already hold the lock, we can skip some rechecking.
  345. * Note that if we hold the lock now, checked_pageblock was
  346. * already set in some previous iteration (or strict is true),
  347. * so it is correct to skip the suitable migration target
  348. * recheck as well.
  349. */
  350. if (!locked) {
  351. /*
  352. * The zone lock must be held to isolate freepages.
  353. * Unfortunately this is a very coarse lock and can be
  354. * heavily contended if there are parallel allocations
  355. * or parallel compactions. For async compaction do not
  356. * spin on the lock and we acquire the lock as late as
  357. * possible.
  358. */
  359. locked = compact_trylock_irqsave(&cc->zone->lock,
  360. &flags, cc);
  361. if (!locked)
  362. break;
  363. /* Recheck this is a buddy page under lock */
  364. if (!PageBuddy(page))
  365. goto isolate_fail;
  366. }
  367. /* Found a free page, break it into order-0 pages */
  368. isolated = split_free_page(page);
  369. total_isolated += isolated;
  370. for (i = 0; i < isolated; i++) {
  371. list_add(&page->lru, freelist);
  372. page++;
  373. }
  374. /* If a page was split, advance to the end of it */
  375. if (isolated) {
  376. cc->nr_freepages += isolated;
  377. if (!strict &&
  378. cc->nr_migratepages <= cc->nr_freepages) {
  379. blockpfn += isolated;
  380. break;
  381. }
  382. blockpfn += isolated - 1;
  383. cursor += isolated - 1;
  384. continue;
  385. }
  386. isolate_fail:
  387. if (strict)
  388. break;
  389. else
  390. continue;
  391. }
  392. /*
  393. * There is a tiny chance that we have read bogus compound_order(),
  394. * so be careful to not go outside of the pageblock.
  395. */
  396. if (unlikely(blockpfn > end_pfn))
  397. blockpfn = end_pfn;
  398. trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn,
  399. nr_scanned, total_isolated);
  400. /* Record how far we have got within the block */
  401. *start_pfn = blockpfn;
  402. /*
  403. * If strict isolation is requested by CMA then check that all the
  404. * pages requested were isolated. If there were any failures, 0 is
  405. * returned and CMA will fail.
  406. */
  407. if (strict && blockpfn < end_pfn)
  408. total_isolated = 0;
  409. if (locked)
  410. spin_unlock_irqrestore(&cc->zone->lock, flags);
  411. /* Update the pageblock-skip if the whole pageblock was scanned */
  412. if (blockpfn == end_pfn)
  413. update_pageblock_skip(cc, valid_page, total_isolated, false);
  414. count_compact_events(COMPACTFREE_SCANNED, nr_scanned);
  415. if (total_isolated)
  416. count_compact_events(COMPACTISOLATED, total_isolated);
  417. return total_isolated;
  418. }
  419. /**
  420. * isolate_freepages_range() - isolate free pages.
  421. * @start_pfn: The first PFN to start isolating.
  422. * @end_pfn: The one-past-last PFN.
  423. *
  424. * Non-free pages, invalid PFNs, or zone boundaries within the
  425. * [start_pfn, end_pfn) range are considered errors, cause function to
  426. * undo its actions and return zero.
  427. *
  428. * Otherwise, function returns one-past-the-last PFN of isolated page
  429. * (which may be greater then end_pfn if end fell in a middle of
  430. * a free page).
  431. */
  432. unsigned long
  433. isolate_freepages_range(struct compact_control *cc,
  434. unsigned long start_pfn, unsigned long end_pfn)
  435. {
  436. unsigned long isolated, pfn, block_start_pfn, block_end_pfn;
  437. LIST_HEAD(freelist);
  438. pfn = start_pfn;
  439. block_start_pfn = pfn & ~(pageblock_nr_pages - 1);
  440. if (block_start_pfn < cc->zone->zone_start_pfn)
  441. block_start_pfn = cc->zone->zone_start_pfn;
  442. block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
  443. for (; pfn < end_pfn; pfn += isolated,
  444. block_start_pfn = block_end_pfn,
  445. block_end_pfn += pageblock_nr_pages) {
  446. /* Protect pfn from changing by isolate_freepages_block */
  447. unsigned long isolate_start_pfn = pfn;
  448. block_end_pfn = min(block_end_pfn, end_pfn);
  449. /*
  450. * pfn could pass the block_end_pfn if isolated freepage
  451. * is more than pageblock order. In this case, we adjust
  452. * scanning range to right one.
  453. */
  454. if (pfn >= block_end_pfn) {
  455. block_start_pfn = pfn & ~(pageblock_nr_pages - 1);
  456. block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
  457. block_end_pfn = min(block_end_pfn, end_pfn);
  458. }
  459. if (!pageblock_pfn_to_page(block_start_pfn,
  460. block_end_pfn, cc->zone))
  461. break;
  462. isolated = isolate_freepages_block(cc, &isolate_start_pfn,
  463. block_end_pfn, &freelist, true);
  464. /*
  465. * In strict mode, isolate_freepages_block() returns 0 if
  466. * there are any holes in the block (ie. invalid PFNs or
  467. * non-free pages).
  468. */
  469. if (!isolated)
  470. break;
  471. /*
  472. * If we managed to isolate pages, it is always (1 << n) *
  473. * pageblock_nr_pages for some non-negative n. (Max order
  474. * page may span two pageblocks).
  475. */
  476. }
  477. /* split_free_page does not map the pages */
  478. map_pages(&freelist);
  479. if (pfn < end_pfn) {
  480. /* Loop terminated early, cleanup. */
  481. release_freepages(&freelist);
  482. return 0;
  483. }
  484. /* We don't use freelists for anything. */
  485. return pfn;
  486. }
  487. /* Update the number of anon and file isolated pages in the zone */
  488. static void acct_isolated(struct zone *zone, struct compact_control *cc)
  489. {
  490. struct page *page;
  491. unsigned int count[2] = { 0, };
  492. if (list_empty(&cc->migratepages))
  493. return;
  494. list_for_each_entry(page, &cc->migratepages, lru)
  495. count[!!page_is_file_cache(page)]++;
  496. mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
  497. mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
  498. }
  499. /* Similar to reclaim, but different enough that they don't share logic */
  500. static bool too_many_isolated(struct zone *zone)
  501. {
  502. unsigned long active, inactive, isolated;
  503. inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
  504. zone_page_state(zone, NR_INACTIVE_ANON);
  505. active = zone_page_state(zone, NR_ACTIVE_FILE) +
  506. zone_page_state(zone, NR_ACTIVE_ANON);
  507. isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
  508. zone_page_state(zone, NR_ISOLATED_ANON);
  509. return isolated > (inactive + active) / 2;
  510. }
  511. /**
  512. * isolate_migratepages_block() - isolate all migrate-able pages within
  513. * a single pageblock
  514. * @cc: Compaction control structure.
  515. * @low_pfn: The first PFN to isolate
  516. * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock
  517. * @isolate_mode: Isolation mode to be used.
  518. *
  519. * Isolate all pages that can be migrated from the range specified by
  520. * [low_pfn, end_pfn). The range is expected to be within same pageblock.
  521. * Returns zero if there is a fatal signal pending, otherwise PFN of the
  522. * first page that was not scanned (which may be both less, equal to or more
  523. * than end_pfn).
  524. *
  525. * The pages are isolated on cc->migratepages list (not required to be empty),
  526. * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field
  527. * is neither read nor updated.
  528. */
  529. static unsigned long
  530. isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
  531. unsigned long end_pfn, isolate_mode_t isolate_mode)
  532. {
  533. struct zone *zone = cc->zone;
  534. unsigned long nr_scanned = 0, nr_isolated = 0;
  535. struct list_head *migratelist = &cc->migratepages;
  536. struct lruvec *lruvec;
  537. unsigned long flags = 0;
  538. bool locked = false;
  539. struct page *page = NULL, *valid_page = NULL;
  540. unsigned long start_pfn = low_pfn;
  541. /*
  542. * Ensure that there are not too many pages isolated from the LRU
  543. * list by either parallel reclaimers or compaction. If there are,
  544. * delay for some time until fewer pages are isolated
  545. */
  546. while (unlikely(too_many_isolated(zone))) {
  547. /* async migration should just abort */
  548. if (cc->mode == MIGRATE_ASYNC)
  549. return 0;
  550. congestion_wait(BLK_RW_ASYNC, HZ/10);
  551. if (fatal_signal_pending(current))
  552. return 0;
  553. }
  554. if (compact_should_abort(cc))
  555. return 0;
  556. /* Time to isolate some pages for migration */
  557. for (; low_pfn < end_pfn; low_pfn++) {
  558. bool is_lru;
  559. /*
  560. * Periodically drop the lock (if held) regardless of its
  561. * contention, to give chance to IRQs. Abort async compaction
  562. * if contended.
  563. */
  564. if (!(low_pfn % SWAP_CLUSTER_MAX)
  565. && compact_unlock_should_abort(&zone->lru_lock, flags,
  566. &locked, cc))
  567. break;
  568. if (!pfn_valid_within(low_pfn))
  569. continue;
  570. nr_scanned++;
  571. page = pfn_to_page(low_pfn);
  572. if (!valid_page)
  573. valid_page = page;
  574. /*
  575. * Skip if free. We read page order here without zone lock
  576. * which is generally unsafe, but the race window is small and
  577. * the worst thing that can happen is that we skip some
  578. * potential isolation targets.
  579. */
  580. if (PageBuddy(page)) {
  581. unsigned long freepage_order = page_order_unsafe(page);
  582. /*
  583. * Without lock, we cannot be sure that what we got is
  584. * a valid page order. Consider only values in the
  585. * valid order range to prevent low_pfn overflow.
  586. */
  587. if (freepage_order > 0 && freepage_order < MAX_ORDER)
  588. low_pfn += (1UL << freepage_order) - 1;
  589. continue;
  590. }
  591. /*
  592. * Check may be lockless but that's ok as we recheck later.
  593. * It's possible to migrate LRU pages and balloon pages
  594. * Skip any other type of page
  595. */
  596. is_lru = PageLRU(page);
  597. if (!is_lru) {
  598. if (unlikely(balloon_page_movable(page))) {
  599. if (balloon_page_isolate(page)) {
  600. /* Successfully isolated */
  601. goto isolate_success;
  602. }
  603. }
  604. }
  605. /*
  606. * Regardless of being on LRU, compound pages such as THP and
  607. * hugetlbfs are not to be compacted. We can potentially save
  608. * a lot of iterations if we skip them at once. The check is
  609. * racy, but we can consider only valid values and the only
  610. * danger is skipping too much.
  611. */
  612. if (PageCompound(page)) {
  613. unsigned int comp_order = compound_order(page);
  614. if (likely(comp_order < MAX_ORDER))
  615. low_pfn += (1UL << comp_order) - 1;
  616. continue;
  617. }
  618. if (!is_lru)
  619. continue;
  620. /*
  621. * Migration will fail if an anonymous page is pinned in memory,
  622. * so avoid taking lru_lock and isolating it unnecessarily in an
  623. * admittedly racy check.
  624. */
  625. if (!page_mapping(page) &&
  626. page_count(page) > page_mapcount(page))
  627. continue;
  628. /* If we already hold the lock, we can skip some rechecking */
  629. if (!locked) {
  630. locked = compact_trylock_irqsave(&zone->lru_lock,
  631. &flags, cc);
  632. if (!locked)
  633. break;
  634. /* Recheck PageLRU and PageCompound under lock */
  635. if (!PageLRU(page))
  636. continue;
  637. /*
  638. * Page become compound since the non-locked check,
  639. * and it's on LRU. It can only be a THP so the order
  640. * is safe to read and it's 0 for tail pages.
  641. */
  642. if (unlikely(PageCompound(page))) {
  643. low_pfn += (1UL << compound_order(page)) - 1;
  644. continue;
  645. }
  646. }
  647. lruvec = mem_cgroup_page_lruvec(page, zone);
  648. /* Try isolate the page */
  649. if (__isolate_lru_page(page, isolate_mode) != 0)
  650. continue;
  651. VM_BUG_ON_PAGE(PageCompound(page), page);
  652. /* Successfully isolated */
  653. del_page_from_lru_list(page, lruvec, page_lru(page));
  654. isolate_success:
  655. list_add(&page->lru, migratelist);
  656. cc->nr_migratepages++;
  657. nr_isolated++;
  658. /* Avoid isolating too much */
  659. if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
  660. ++low_pfn;
  661. break;
  662. }
  663. }
  664. /*
  665. * The PageBuddy() check could have potentially brought us outside
  666. * the range to be scanned.
  667. */
  668. if (unlikely(low_pfn > end_pfn))
  669. low_pfn = end_pfn;
  670. if (locked)
  671. spin_unlock_irqrestore(&zone->lru_lock, flags);
  672. /*
  673. * Update the pageblock-skip information and cached scanner pfn,
  674. * if the whole pageblock was scanned without isolating any page.
  675. */
  676. if (low_pfn == end_pfn)
  677. update_pageblock_skip(cc, valid_page, nr_isolated, true);
  678. trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn,
  679. nr_scanned, nr_isolated);
  680. count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned);
  681. if (nr_isolated)
  682. count_compact_events(COMPACTISOLATED, nr_isolated);
  683. return low_pfn;
  684. }
  685. /**
  686. * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
  687. * @cc: Compaction control structure.
  688. * @start_pfn: The first PFN to start isolating.
  689. * @end_pfn: The one-past-last PFN.
  690. *
  691. * Returns zero if isolation fails fatally due to e.g. pending signal.
  692. * Otherwise, function returns one-past-the-last PFN of isolated page
  693. * (which may be greater than end_pfn if end fell in a middle of a THP page).
  694. */
  695. unsigned long
  696. isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
  697. unsigned long end_pfn)
  698. {
  699. unsigned long pfn, block_start_pfn, block_end_pfn;
  700. /* Scan block by block. First and last block may be incomplete */
  701. pfn = start_pfn;
  702. block_start_pfn = pfn & ~(pageblock_nr_pages - 1);
  703. if (block_start_pfn < cc->zone->zone_start_pfn)
  704. block_start_pfn = cc->zone->zone_start_pfn;
  705. block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
  706. for (; pfn < end_pfn; pfn = block_end_pfn,
  707. block_start_pfn = block_end_pfn,
  708. block_end_pfn += pageblock_nr_pages) {
  709. block_end_pfn = min(block_end_pfn, end_pfn);
  710. if (!pageblock_pfn_to_page(block_start_pfn,
  711. block_end_pfn, cc->zone))
  712. continue;
  713. pfn = isolate_migratepages_block(cc, pfn, block_end_pfn,
  714. ISOLATE_UNEVICTABLE);
  715. /*
  716. * In case of fatal failure, release everything that might
  717. * have been isolated in the previous iteration, and signal
  718. * the failure back to caller.
  719. */
  720. if (!pfn) {
  721. putback_movable_pages(&cc->migratepages);
  722. cc->nr_migratepages = 0;
  723. break;
  724. }
  725. if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
  726. break;
  727. }
  728. acct_isolated(cc->zone, cc);
  729. return pfn;
  730. }
  731. #endif /* CONFIG_COMPACTION || CONFIG_CMA */
  732. #ifdef CONFIG_COMPACTION
  733. /* Returns true if the page is within a block suitable for migration to */
  734. static bool suitable_migration_target(struct page *page)
  735. {
  736. /* If the page is a large free page, then disallow migration */
  737. if (PageBuddy(page)) {
  738. /*
  739. * We are checking page_order without zone->lock taken. But
  740. * the only small danger is that we skip a potentially suitable
  741. * pageblock, so it's not worth to check order for valid range.
  742. */
  743. if (page_order_unsafe(page) >= pageblock_order)
  744. return false;
  745. }
  746. /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
  747. if (migrate_async_suitable(get_pageblock_migratetype(page)))
  748. return true;
  749. /* Otherwise skip the block */
  750. return false;
  751. }
  752. /*
  753. * Test whether the free scanner has reached the same or lower pageblock than
  754. * the migration scanner, and compaction should thus terminate.
  755. */
  756. static inline bool compact_scanners_met(struct compact_control *cc)
  757. {
  758. return (cc->free_pfn >> pageblock_order)
  759. <= (cc->migrate_pfn >> pageblock_order);
  760. }
  761. /*
  762. * Based on information in the current compact_control, find blocks
  763. * suitable for isolating free pages from and then isolate them.
  764. */
  765. static void isolate_freepages(struct compact_control *cc)
  766. {
  767. struct zone *zone = cc->zone;
  768. struct page *page;
  769. unsigned long block_start_pfn; /* start of current pageblock */
  770. unsigned long isolate_start_pfn; /* exact pfn we start at */
  771. unsigned long block_end_pfn; /* end of current pageblock */
  772. unsigned long low_pfn; /* lowest pfn scanner is able to scan */
  773. struct list_head *freelist = &cc->freepages;
  774. /*
  775. * Initialise the free scanner. The starting point is where we last
  776. * successfully isolated from, zone-cached value, or the end of the
  777. * zone when isolating for the first time. For looping we also need
  778. * this pfn aligned down to the pageblock boundary, because we do
  779. * block_start_pfn -= pageblock_nr_pages in the for loop.
  780. * For ending point, take care when isolating in last pageblock of a
  781. * a zone which ends in the middle of a pageblock.
  782. * The low boundary is the end of the pageblock the migration scanner
  783. * is using.
  784. */
  785. isolate_start_pfn = cc->free_pfn;
  786. block_start_pfn = cc->free_pfn & ~(pageblock_nr_pages-1);
  787. block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
  788. zone_end_pfn(zone));
  789. low_pfn = ALIGN(cc->migrate_pfn + 1, pageblock_nr_pages);
  790. /*
  791. * Isolate free pages until enough are available to migrate the
  792. * pages on cc->migratepages. We stop searching if the migrate
  793. * and free page scanners meet or enough free pages are isolated.
  794. */
  795. for (; block_start_pfn >= low_pfn;
  796. block_end_pfn = block_start_pfn,
  797. block_start_pfn -= pageblock_nr_pages,
  798. isolate_start_pfn = block_start_pfn) {
  799. /*
  800. * This can iterate a massively long zone without finding any
  801. * suitable migration targets, so periodically check if we need
  802. * to schedule, or even abort async compaction.
  803. */
  804. if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
  805. && compact_should_abort(cc))
  806. break;
  807. page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
  808. zone);
  809. if (!page)
  810. continue;
  811. /* Check the block is suitable for migration */
  812. if (!suitable_migration_target(page))
  813. continue;
  814. /* If isolation recently failed, do not retry */
  815. if (!isolation_suitable(cc, page))
  816. continue;
  817. /* Found a block suitable for isolating free pages from. */
  818. isolate_freepages_block(cc, &isolate_start_pfn,
  819. block_end_pfn, freelist, false);
  820. /*
  821. * If we isolated enough freepages, or aborted due to async
  822. * compaction being contended, terminate the loop.
  823. * Remember where the free scanner should restart next time,
  824. * which is where isolate_freepages_block() left off.
  825. * But if it scanned the whole pageblock, isolate_start_pfn
  826. * now points at block_end_pfn, which is the start of the next
  827. * pageblock.
  828. * In that case we will however want to restart at the start
  829. * of the previous pageblock.
  830. */
  831. if ((cc->nr_freepages >= cc->nr_migratepages)
  832. || cc->contended) {
  833. if (isolate_start_pfn >= block_end_pfn)
  834. isolate_start_pfn =
  835. block_start_pfn - pageblock_nr_pages;
  836. break;
  837. } else {
  838. /*
  839. * isolate_freepages_block() should not terminate
  840. * prematurely unless contended, or isolated enough
  841. */
  842. VM_BUG_ON(isolate_start_pfn < block_end_pfn);
  843. }
  844. }
  845. /* split_free_page does not map the pages */
  846. map_pages(freelist);
  847. /*
  848. * Record where the free scanner will restart next time. Either we
  849. * broke from the loop and set isolate_start_pfn based on the last
  850. * call to isolate_freepages_block(), or we met the migration scanner
  851. * and the loop terminated due to isolate_start_pfn < low_pfn
  852. */
  853. cc->free_pfn = isolate_start_pfn;
  854. }
  855. /*
  856. * This is a migrate-callback that "allocates" freepages by taking pages
  857. * from the isolated freelists in the block we are migrating to.
  858. */
  859. static struct page *compaction_alloc(struct page *migratepage,
  860. unsigned long data,
  861. int **result)
  862. {
  863. struct compact_control *cc = (struct compact_control *)data;
  864. struct page *freepage;
  865. /*
  866. * Isolate free pages if necessary, and if we are not aborting due to
  867. * contention.
  868. */
  869. if (list_empty(&cc->freepages)) {
  870. if (!cc->contended)
  871. isolate_freepages(cc);
  872. if (list_empty(&cc->freepages))
  873. return NULL;
  874. }
  875. freepage = list_entry(cc->freepages.next, struct page, lru);
  876. list_del(&freepage->lru);
  877. cc->nr_freepages--;
  878. return freepage;
  879. }
  880. /*
  881. * This is a migrate-callback that "frees" freepages back to the isolated
  882. * freelist. All pages on the freelist are from the same zone, so there is no
  883. * special handling needed for NUMA.
  884. */
  885. static void compaction_free(struct page *page, unsigned long data)
  886. {
  887. struct compact_control *cc = (struct compact_control *)data;
  888. list_add(&page->lru, &cc->freepages);
  889. cc->nr_freepages++;
  890. }
  891. /* possible outcome of isolate_migratepages */
  892. typedef enum {
  893. ISOLATE_ABORT, /* Abort compaction now */
  894. ISOLATE_NONE, /* No pages isolated, continue scanning */
  895. ISOLATE_SUCCESS, /* Pages isolated, migrate */
  896. } isolate_migrate_t;
  897. /*
  898. * Allow userspace to control policy on scanning the unevictable LRU for
  899. * compactable pages.
  900. */
  901. int sysctl_compact_unevictable_allowed __read_mostly = 1;
  902. /*
  903. * Isolate all pages that can be migrated from the first suitable block,
  904. * starting at the block pointed to by the migrate scanner pfn within
  905. * compact_control.
  906. */
  907. static isolate_migrate_t isolate_migratepages(struct zone *zone,
  908. struct compact_control *cc)
  909. {
  910. unsigned long block_start_pfn;
  911. unsigned long block_end_pfn;
  912. unsigned long low_pfn;
  913. unsigned long isolate_start_pfn;
  914. struct page *page;
  915. const isolate_mode_t isolate_mode =
  916. (sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) |
  917. (cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0);
  918. /*
  919. * Start at where we last stopped, or beginning of the zone as
  920. * initialized by compact_zone()
  921. */
  922. low_pfn = cc->migrate_pfn;
  923. block_start_pfn = cc->migrate_pfn & ~(pageblock_nr_pages - 1);
  924. if (block_start_pfn < zone->zone_start_pfn)
  925. block_start_pfn = zone->zone_start_pfn;
  926. /* Only scan within a pageblock boundary */
  927. block_end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages);
  928. /*
  929. * Iterate over whole pageblocks until we find the first suitable.
  930. * Do not cross the free scanner.
  931. */
  932. for (; block_end_pfn <= cc->free_pfn;
  933. low_pfn = block_end_pfn,
  934. block_start_pfn = block_end_pfn,
  935. block_end_pfn += pageblock_nr_pages) {
  936. /*
  937. * This can potentially iterate a massively long zone with
  938. * many pageblocks unsuitable, so periodically check if we
  939. * need to schedule, or even abort async compaction.
  940. */
  941. if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
  942. && compact_should_abort(cc))
  943. break;
  944. page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
  945. zone);
  946. if (!page)
  947. continue;
  948. /* If isolation recently failed, do not retry */
  949. if (!isolation_suitable(cc, page))
  950. continue;
  951. /*
  952. * For async compaction, also only scan in MOVABLE blocks.
  953. * Async compaction is optimistic to see if the minimum amount
  954. * of work satisfies the allocation.
  955. */
  956. if (cc->mode == MIGRATE_ASYNC &&
  957. !migrate_async_suitable(get_pageblock_migratetype(page)))
  958. continue;
  959. /* Perform the isolation */
  960. isolate_start_pfn = low_pfn;
  961. low_pfn = isolate_migratepages_block(cc, low_pfn,
  962. block_end_pfn, isolate_mode);
  963. if (!low_pfn || cc->contended) {
  964. acct_isolated(zone, cc);
  965. return ISOLATE_ABORT;
  966. }
  967. /*
  968. * Record where we could have freed pages by migration and not
  969. * yet flushed them to buddy allocator.
  970. * - this is the lowest page that could have been isolated and
  971. * then freed by migration.
  972. */
  973. if (cc->nr_migratepages && !cc->last_migrated_pfn)
  974. cc->last_migrated_pfn = isolate_start_pfn;
  975. /*
  976. * Either we isolated something and proceed with migration. Or
  977. * we failed and compact_zone should decide if we should
  978. * continue or not.
  979. */
  980. break;
  981. }
  982. acct_isolated(zone, cc);
  983. /* Record where migration scanner will be restarted. */
  984. cc->migrate_pfn = low_pfn;
  985. return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
  986. }
  987. /*
  988. * order == -1 is expected when compacting via
  989. * /proc/sys/vm/compact_memory
  990. */
  991. static inline bool is_via_compact_memory(int order)
  992. {
  993. return order == -1;
  994. }
  995. static int __compact_finished(struct zone *zone, struct compact_control *cc,
  996. const int migratetype)
  997. {
  998. unsigned int order;
  999. unsigned long watermark;
  1000. if (cc->contended || fatal_signal_pending(current))
  1001. return COMPACT_CONTENDED;
  1002. /* Compaction run completes if the migrate and free scanner meet */
  1003. if (compact_scanners_met(cc)) {
  1004. /* Let the next compaction start anew. */
  1005. reset_cached_positions(zone);
  1006. /*
  1007. * Mark that the PG_migrate_skip information should be cleared
  1008. * by kswapd when it goes to sleep. kcompactd does not set the
  1009. * flag itself as the decision to be clear should be directly
  1010. * based on an allocation request.
  1011. */
  1012. if (cc->direct_compaction)
  1013. zone->compact_blockskip_flush = true;
  1014. return COMPACT_COMPLETE;
  1015. }
  1016. if (is_via_compact_memory(cc->order))
  1017. return COMPACT_CONTINUE;
  1018. /* Compaction run is not finished if the watermark is not met */
  1019. watermark = low_wmark_pages(zone);
  1020. if (!zone_watermark_ok(zone, cc->order, watermark, cc->classzone_idx,
  1021. cc->alloc_flags))
  1022. return COMPACT_CONTINUE;
  1023. /* Direct compactor: Is a suitable page free? */
  1024. for (order = cc->order; order < MAX_ORDER; order++) {
  1025. struct free_area *area = &zone->free_area[order];
  1026. bool can_steal;
  1027. /* Job done if page is free of the right migratetype */
  1028. if (!list_empty(&area->free_list[migratetype]))
  1029. return COMPACT_PARTIAL;
  1030. #ifdef CONFIG_CMA
  1031. /* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */
  1032. if (migratetype == MIGRATE_MOVABLE &&
  1033. !list_empty(&area->free_list[MIGRATE_CMA]))
  1034. return COMPACT_PARTIAL;
  1035. #endif
  1036. /*
  1037. * Job done if allocation would steal freepages from
  1038. * other migratetype buddy lists.
  1039. */
  1040. if (find_suitable_fallback(area, order, migratetype,
  1041. true, &can_steal) != -1)
  1042. return COMPACT_PARTIAL;
  1043. }
  1044. return COMPACT_NO_SUITABLE_PAGE;
  1045. }
  1046. static int compact_finished(struct zone *zone, struct compact_control *cc,
  1047. const int migratetype)
  1048. {
  1049. int ret;
  1050. ret = __compact_finished(zone, cc, migratetype);
  1051. trace_mm_compaction_finished(zone, cc->order, ret);
  1052. if (ret == COMPACT_NO_SUITABLE_PAGE)
  1053. ret = COMPACT_CONTINUE;
  1054. return ret;
  1055. }
  1056. /*
  1057. * compaction_suitable: Is this suitable to run compaction on this zone now?
  1058. * Returns
  1059. * COMPACT_SKIPPED - If there are too few free pages for compaction
  1060. * COMPACT_PARTIAL - If the allocation would succeed without compaction
  1061. * COMPACT_CONTINUE - If compaction should run now
  1062. */
  1063. static unsigned long __compaction_suitable(struct zone *zone, int order,
  1064. int alloc_flags, int classzone_idx)
  1065. {
  1066. int fragindex;
  1067. unsigned long watermark;
  1068. if (is_via_compact_memory(order))
  1069. return COMPACT_CONTINUE;
  1070. watermark = low_wmark_pages(zone);
  1071. /*
  1072. * If watermarks for high-order allocation are already met, there
  1073. * should be no need for compaction at all.
  1074. */
  1075. if (zone_watermark_ok(zone, order, watermark, classzone_idx,
  1076. alloc_flags))
  1077. return COMPACT_PARTIAL;
  1078. /*
  1079. * Watermarks for order-0 must be met for compaction. Note the 2UL.
  1080. * This is because during migration, copies of pages need to be
  1081. * allocated and for a short time, the footprint is higher
  1082. */
  1083. watermark += (2UL << order);
  1084. if (!zone_watermark_ok(zone, 0, watermark, classzone_idx, alloc_flags))
  1085. return COMPACT_SKIPPED;
  1086. /*
  1087. * fragmentation index determines if allocation failures are due to
  1088. * low memory or external fragmentation
  1089. *
  1090. * index of -1000 would imply allocations might succeed depending on
  1091. * watermarks, but we already failed the high-order watermark check
  1092. * index towards 0 implies failure is due to lack of memory
  1093. * index towards 1000 implies failure is due to fragmentation
  1094. *
  1095. * Only compact if a failure would be due to fragmentation.
  1096. */
  1097. fragindex = fragmentation_index(zone, order);
  1098. if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
  1099. return COMPACT_NOT_SUITABLE_ZONE;
  1100. return COMPACT_CONTINUE;
  1101. }
  1102. unsigned long compaction_suitable(struct zone *zone, int order,
  1103. int alloc_flags, int classzone_idx)
  1104. {
  1105. unsigned long ret;
  1106. ret = __compaction_suitable(zone, order, alloc_flags, classzone_idx);
  1107. trace_mm_compaction_suitable(zone, order, ret);
  1108. if (ret == COMPACT_NOT_SUITABLE_ZONE)
  1109. ret = COMPACT_SKIPPED;
  1110. return ret;
  1111. }
  1112. static int compact_zone(struct zone *zone, struct compact_control *cc)
  1113. {
  1114. int ret;
  1115. unsigned long start_pfn = zone->zone_start_pfn;
  1116. unsigned long end_pfn = zone_end_pfn(zone);
  1117. const int migratetype = gfpflags_to_migratetype(cc->gfp_mask);
  1118. const bool sync = cc->mode != MIGRATE_ASYNC;
  1119. ret = compaction_suitable(zone, cc->order, cc->alloc_flags,
  1120. cc->classzone_idx);
  1121. switch (ret) {
  1122. case COMPACT_PARTIAL:
  1123. case COMPACT_SKIPPED:
  1124. /* Compaction is likely to fail */
  1125. return ret;
  1126. case COMPACT_CONTINUE:
  1127. /* Fall through to compaction */
  1128. ;
  1129. }
  1130. /*
  1131. * Clear pageblock skip if there were failures recently and compaction
  1132. * is about to be retried after being deferred.
  1133. */
  1134. if (compaction_restarting(zone, cc->order))
  1135. __reset_isolation_suitable(zone);
  1136. /*
  1137. * Setup to move all movable pages to the end of the zone. Used cached
  1138. * information on where the scanners should start but check that it
  1139. * is initialised by ensuring the values are within zone boundaries.
  1140. */
  1141. cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync];
  1142. cc->free_pfn = zone->compact_cached_free_pfn;
  1143. if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) {
  1144. cc->free_pfn = round_down(end_pfn - 1, pageblock_nr_pages);
  1145. zone->compact_cached_free_pfn = cc->free_pfn;
  1146. }
  1147. if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) {
  1148. cc->migrate_pfn = start_pfn;
  1149. zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
  1150. zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
  1151. }
  1152. cc->last_migrated_pfn = 0;
  1153. trace_mm_compaction_begin(start_pfn, cc->migrate_pfn,
  1154. cc->free_pfn, end_pfn, sync);
  1155. migrate_prep_local();
  1156. while ((ret = compact_finished(zone, cc, migratetype)) ==
  1157. COMPACT_CONTINUE) {
  1158. int err;
  1159. switch (isolate_migratepages(zone, cc)) {
  1160. case ISOLATE_ABORT:
  1161. ret = COMPACT_CONTENDED;
  1162. putback_movable_pages(&cc->migratepages);
  1163. cc->nr_migratepages = 0;
  1164. goto out;
  1165. case ISOLATE_NONE:
  1166. /*
  1167. * We haven't isolated and migrated anything, but
  1168. * there might still be unflushed migrations from
  1169. * previous cc->order aligned block.
  1170. */
  1171. goto check_drain;
  1172. case ISOLATE_SUCCESS:
  1173. ;
  1174. }
  1175. err = migrate_pages(&cc->migratepages, compaction_alloc,
  1176. compaction_free, (unsigned long)cc, cc->mode,
  1177. MR_COMPACTION);
  1178. trace_mm_compaction_migratepages(cc->nr_migratepages, err,
  1179. &cc->migratepages);
  1180. /* All pages were either migrated or will be released */
  1181. cc->nr_migratepages = 0;
  1182. if (err) {
  1183. putback_movable_pages(&cc->migratepages);
  1184. /*
  1185. * migrate_pages() may return -ENOMEM when scanners meet
  1186. * and we want compact_finished() to detect it
  1187. */
  1188. if (err == -ENOMEM && !compact_scanners_met(cc)) {
  1189. ret = COMPACT_CONTENDED;
  1190. goto out;
  1191. }
  1192. }
  1193. check_drain:
  1194. /*
  1195. * Has the migration scanner moved away from the previous
  1196. * cc->order aligned block where we migrated from? If yes,
  1197. * flush the pages that were freed, so that they can merge and
  1198. * compact_finished() can detect immediately if allocation
  1199. * would succeed.
  1200. */
  1201. if (cc->order > 0 && cc->last_migrated_pfn) {
  1202. int cpu;
  1203. unsigned long current_block_start =
  1204. cc->migrate_pfn & ~((1UL << cc->order) - 1);
  1205. if (cc->last_migrated_pfn < current_block_start) {
  1206. cpu = get_cpu();
  1207. lru_add_drain_cpu(cpu);
  1208. drain_local_pages(zone);
  1209. put_cpu();
  1210. /* No more flushing until we migrate again */
  1211. cc->last_migrated_pfn = 0;
  1212. }
  1213. }
  1214. }
  1215. out:
  1216. /*
  1217. * Release free pages and update where the free scanner should restart,
  1218. * so we don't leave any returned pages behind in the next attempt.
  1219. */
  1220. if (cc->nr_freepages > 0) {
  1221. unsigned long free_pfn = release_freepages(&cc->freepages);
  1222. cc->nr_freepages = 0;
  1223. VM_BUG_ON(free_pfn == 0);
  1224. /* The cached pfn is always the first in a pageblock */
  1225. free_pfn &= ~(pageblock_nr_pages-1);
  1226. /*
  1227. * Only go back, not forward. The cached pfn might have been
  1228. * already reset to zone end in compact_finished()
  1229. */
  1230. if (free_pfn > zone->compact_cached_free_pfn)
  1231. zone->compact_cached_free_pfn = free_pfn;
  1232. }
  1233. trace_mm_compaction_end(start_pfn, cc->migrate_pfn,
  1234. cc->free_pfn, end_pfn, sync, ret);
  1235. if (ret == COMPACT_CONTENDED)
  1236. ret = COMPACT_PARTIAL;
  1237. return ret;
  1238. }
  1239. static unsigned long compact_zone_order(struct zone *zone, int order,
  1240. gfp_t gfp_mask, enum migrate_mode mode, int *contended,
  1241. int alloc_flags, int classzone_idx)
  1242. {
  1243. unsigned long ret;
  1244. struct compact_control cc = {
  1245. .nr_freepages = 0,
  1246. .nr_migratepages = 0,
  1247. .order = order,
  1248. .gfp_mask = gfp_mask,
  1249. .zone = zone,
  1250. .mode = mode,
  1251. .alloc_flags = alloc_flags,
  1252. .classzone_idx = classzone_idx,
  1253. .direct_compaction = true,
  1254. };
  1255. INIT_LIST_HEAD(&cc.freepages);
  1256. INIT_LIST_HEAD(&cc.migratepages);
  1257. ret = compact_zone(zone, &cc);
  1258. VM_BUG_ON(!list_empty(&cc.freepages));
  1259. VM_BUG_ON(!list_empty(&cc.migratepages));
  1260. *contended = cc.contended;
  1261. return ret;
  1262. }
  1263. int sysctl_extfrag_threshold = 500;
  1264. /**
  1265. * try_to_compact_pages - Direct compact to satisfy a high-order allocation
  1266. * @gfp_mask: The GFP mask of the current allocation
  1267. * @order: The order of the current allocation
  1268. * @alloc_flags: The allocation flags of the current allocation
  1269. * @ac: The context of current allocation
  1270. * @mode: The migration mode for async, sync light, or sync migration
  1271. * @contended: Return value that determines if compaction was aborted due to
  1272. * need_resched() or lock contention
  1273. *
  1274. * This is the main entry point for direct page compaction.
  1275. */
  1276. unsigned long try_to_compact_pages(gfp_t gfp_mask, unsigned int order,
  1277. int alloc_flags, const struct alloc_context *ac,
  1278. enum migrate_mode mode, int *contended)
  1279. {
  1280. int may_enter_fs = gfp_mask & __GFP_FS;
  1281. int may_perform_io = gfp_mask & __GFP_IO;
  1282. struct zoneref *z;
  1283. struct zone *zone;
  1284. int rc = COMPACT_DEFERRED;
  1285. int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */
  1286. *contended = COMPACT_CONTENDED_NONE;
  1287. /* Check if the GFP flags allow compaction */
  1288. if (!order || !may_enter_fs || !may_perform_io)
  1289. return COMPACT_SKIPPED;
  1290. trace_mm_compaction_try_to_compact_pages(order, gfp_mask, mode);
  1291. /* Compact each zone in the list */
  1292. for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
  1293. ac->nodemask) {
  1294. int status;
  1295. int zone_contended;
  1296. if (compaction_deferred(zone, order))
  1297. continue;
  1298. status = compact_zone_order(zone, order, gfp_mask, mode,
  1299. &zone_contended, alloc_flags,
  1300. ac->classzone_idx);
  1301. rc = max(status, rc);
  1302. /*
  1303. * It takes at least one zone that wasn't lock contended
  1304. * to clear all_zones_contended.
  1305. */
  1306. all_zones_contended &= zone_contended;
  1307. /* If a normal allocation would succeed, stop compacting */
  1308. if (zone_watermark_ok(zone, order, low_wmark_pages(zone),
  1309. ac->classzone_idx, alloc_flags)) {
  1310. /*
  1311. * We think the allocation will succeed in this zone,
  1312. * but it is not certain, hence the false. The caller
  1313. * will repeat this with true if allocation indeed
  1314. * succeeds in this zone.
  1315. */
  1316. compaction_defer_reset(zone, order, false);
  1317. /*
  1318. * It is possible that async compaction aborted due to
  1319. * need_resched() and the watermarks were ok thanks to
  1320. * somebody else freeing memory. The allocation can
  1321. * however still fail so we better signal the
  1322. * need_resched() contention anyway (this will not
  1323. * prevent the allocation attempt).
  1324. */
  1325. if (zone_contended == COMPACT_CONTENDED_SCHED)
  1326. *contended = COMPACT_CONTENDED_SCHED;
  1327. goto break_loop;
  1328. }
  1329. if (mode != MIGRATE_ASYNC && status == COMPACT_COMPLETE) {
  1330. /*
  1331. * We think that allocation won't succeed in this zone
  1332. * so we defer compaction there. If it ends up
  1333. * succeeding after all, it will be reset.
  1334. */
  1335. defer_compaction(zone, order);
  1336. }
  1337. /*
  1338. * We might have stopped compacting due to need_resched() in
  1339. * async compaction, or due to a fatal signal detected. In that
  1340. * case do not try further zones and signal need_resched()
  1341. * contention.
  1342. */
  1343. if ((zone_contended == COMPACT_CONTENDED_SCHED)
  1344. || fatal_signal_pending(current)) {
  1345. *contended = COMPACT_CONTENDED_SCHED;
  1346. goto break_loop;
  1347. }
  1348. continue;
  1349. break_loop:
  1350. /*
  1351. * We might not have tried all the zones, so be conservative
  1352. * and assume they are not all lock contended.
  1353. */
  1354. all_zones_contended = 0;
  1355. break;
  1356. }
  1357. /*
  1358. * If at least one zone wasn't deferred or skipped, we report if all
  1359. * zones that were tried were lock contended.
  1360. */
  1361. if (rc > COMPACT_SKIPPED && all_zones_contended)
  1362. *contended = COMPACT_CONTENDED_LOCK;
  1363. return rc;
  1364. }
  1365. /* Compact all zones within a node */
  1366. static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
  1367. {
  1368. int zoneid;
  1369. struct zone *zone;
  1370. for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
  1371. zone = &pgdat->node_zones[zoneid];
  1372. if (!populated_zone(zone))
  1373. continue;
  1374. cc->nr_freepages = 0;
  1375. cc->nr_migratepages = 0;
  1376. cc->zone = zone;
  1377. INIT_LIST_HEAD(&cc->freepages);
  1378. INIT_LIST_HEAD(&cc->migratepages);
  1379. /*
  1380. * When called via /proc/sys/vm/compact_memory
  1381. * this makes sure we compact the whole zone regardless of
  1382. * cached scanner positions.
  1383. */
  1384. if (is_via_compact_memory(cc->order))
  1385. __reset_isolation_suitable(zone);
  1386. if (is_via_compact_memory(cc->order) ||
  1387. !compaction_deferred(zone, cc->order))
  1388. compact_zone(zone, cc);
  1389. VM_BUG_ON(!list_empty(&cc->freepages));
  1390. VM_BUG_ON(!list_empty(&cc->migratepages));
  1391. if (is_via_compact_memory(cc->order))
  1392. continue;
  1393. if (zone_watermark_ok(zone, cc->order,
  1394. low_wmark_pages(zone), 0, 0))
  1395. compaction_defer_reset(zone, cc->order, false);
  1396. }
  1397. }
  1398. void compact_pgdat(pg_data_t *pgdat, int order)
  1399. {
  1400. struct compact_control cc = {
  1401. .order = order,
  1402. .mode = MIGRATE_ASYNC,
  1403. };
  1404. if (!order)
  1405. return;
  1406. __compact_pgdat(pgdat, &cc);
  1407. }
  1408. static void compact_node(int nid)
  1409. {
  1410. struct compact_control cc = {
  1411. .order = -1,
  1412. .mode = MIGRATE_SYNC,
  1413. .ignore_skip_hint = true,
  1414. };
  1415. __compact_pgdat(NODE_DATA(nid), &cc);
  1416. }
  1417. /* Compact all nodes in the system */
  1418. static void compact_nodes(void)
  1419. {
  1420. int nid;
  1421. /* Flush pending updates to the LRU lists */
  1422. lru_add_drain_all();
  1423. for_each_online_node(nid)
  1424. compact_node(nid);
  1425. }
  1426. /* The written value is actually unused, all memory is compacted */
  1427. int sysctl_compact_memory;
  1428. /*
  1429. * This is the entry point for compacting all nodes via
  1430. * /proc/sys/vm/compact_memory
  1431. */
  1432. int sysctl_compaction_handler(struct ctl_table *table, int write,
  1433. void __user *buffer, size_t *length, loff_t *ppos)
  1434. {
  1435. if (write)
  1436. compact_nodes();
  1437. return 0;
  1438. }
  1439. int sysctl_extfrag_handler(struct ctl_table *table, int write,
  1440. void __user *buffer, size_t *length, loff_t *ppos)
  1441. {
  1442. proc_dointvec_minmax(table, write, buffer, length, ppos);
  1443. return 0;
  1444. }
  1445. #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
  1446. static ssize_t sysfs_compact_node(struct device *dev,
  1447. struct device_attribute *attr,
  1448. const char *buf, size_t count)
  1449. {
  1450. int nid = dev->id;
  1451. if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
  1452. /* Flush pending updates to the LRU lists */
  1453. lru_add_drain_all();
  1454. compact_node(nid);
  1455. }
  1456. return count;
  1457. }
  1458. static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
  1459. int compaction_register_node(struct node *node)
  1460. {
  1461. return device_create_file(&node->dev, &dev_attr_compact);
  1462. }
  1463. void compaction_unregister_node(struct node *node)
  1464. {
  1465. return device_remove_file(&node->dev, &dev_attr_compact);
  1466. }
  1467. #endif /* CONFIG_SYSFS && CONFIG_NUMA */
  1468. static inline bool kcompactd_work_requested(pg_data_t *pgdat)
  1469. {
  1470. return pgdat->kcompactd_max_order > 0;
  1471. }
  1472. static bool kcompactd_node_suitable(pg_data_t *pgdat)
  1473. {
  1474. int zoneid;
  1475. struct zone *zone;
  1476. enum zone_type classzone_idx = pgdat->kcompactd_classzone_idx;
  1477. for (zoneid = 0; zoneid < classzone_idx; zoneid++) {
  1478. zone = &pgdat->node_zones[zoneid];
  1479. if (!populated_zone(zone))
  1480. continue;
  1481. if (compaction_suitable(zone, pgdat->kcompactd_max_order, 0,
  1482. classzone_idx) == COMPACT_CONTINUE)
  1483. return true;
  1484. }
  1485. return false;
  1486. }
  1487. static void kcompactd_do_work(pg_data_t *pgdat)
  1488. {
  1489. /*
  1490. * With no special task, compact all zones so that a page of requested
  1491. * order is allocatable.
  1492. */
  1493. int zoneid;
  1494. struct zone *zone;
  1495. struct compact_control cc = {
  1496. .order = pgdat->kcompactd_max_order,
  1497. .classzone_idx = pgdat->kcompactd_classzone_idx,
  1498. .mode = MIGRATE_SYNC_LIGHT,
  1499. .ignore_skip_hint = true,
  1500. };
  1501. bool success = false;
  1502. trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order,
  1503. cc.classzone_idx);
  1504. count_vm_event(KCOMPACTD_WAKE);
  1505. for (zoneid = 0; zoneid < cc.classzone_idx; zoneid++) {
  1506. int status;
  1507. zone = &pgdat->node_zones[zoneid];
  1508. if (!populated_zone(zone))
  1509. continue;
  1510. if (compaction_deferred(zone, cc.order))
  1511. continue;
  1512. if (compaction_suitable(zone, cc.order, 0, zoneid) !=
  1513. COMPACT_CONTINUE)
  1514. continue;
  1515. cc.nr_freepages = 0;
  1516. cc.nr_migratepages = 0;
  1517. cc.zone = zone;
  1518. INIT_LIST_HEAD(&cc.freepages);
  1519. INIT_LIST_HEAD(&cc.migratepages);
  1520. status = compact_zone(zone, &cc);
  1521. if (zone_watermark_ok(zone, cc.order, low_wmark_pages(zone),
  1522. cc.classzone_idx, 0)) {
  1523. success = true;
  1524. compaction_defer_reset(zone, cc.order, false);
  1525. } else if (status == COMPACT_COMPLETE) {
  1526. /*
  1527. * We use sync migration mode here, so we defer like
  1528. * sync direct compaction does.
  1529. */
  1530. defer_compaction(zone, cc.order);
  1531. }
  1532. VM_BUG_ON(!list_empty(&cc.freepages));
  1533. VM_BUG_ON(!list_empty(&cc.migratepages));
  1534. }
  1535. /*
  1536. * Regardless of success, we are done until woken up next. But remember
  1537. * the requested order/classzone_idx in case it was higher/tighter than
  1538. * our current ones
  1539. */
  1540. if (pgdat->kcompactd_max_order <= cc.order)
  1541. pgdat->kcompactd_max_order = 0;
  1542. if (pgdat->kcompactd_classzone_idx >= cc.classzone_idx)
  1543. pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1;
  1544. }
  1545. void wakeup_kcompactd(pg_data_t *pgdat, int order, int classzone_idx)
  1546. {
  1547. if (!order)
  1548. return;
  1549. if (pgdat->kcompactd_max_order < order)
  1550. pgdat->kcompactd_max_order = order;
  1551. if (pgdat->kcompactd_classzone_idx > classzone_idx)
  1552. pgdat->kcompactd_classzone_idx = classzone_idx;
  1553. if (!waitqueue_active(&pgdat->kcompactd_wait))
  1554. return;
  1555. if (!kcompactd_node_suitable(pgdat))
  1556. return;
  1557. trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order,
  1558. classzone_idx);
  1559. wake_up_interruptible(&pgdat->kcompactd_wait);
  1560. }
  1561. /*
  1562. * The background compaction daemon, started as a kernel thread
  1563. * from the init process.
  1564. */
  1565. static int kcompactd(void *p)
  1566. {
  1567. pg_data_t *pgdat = (pg_data_t*)p;
  1568. struct task_struct *tsk = current;
  1569. const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
  1570. if (!cpumask_empty(cpumask))
  1571. set_cpus_allowed_ptr(tsk, cpumask);
  1572. set_freezable();
  1573. pgdat->kcompactd_max_order = 0;
  1574. pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1;
  1575. while (!kthread_should_stop()) {
  1576. trace_mm_compaction_kcompactd_sleep(pgdat->node_id);
  1577. wait_event_freezable(pgdat->kcompactd_wait,
  1578. kcompactd_work_requested(pgdat));
  1579. kcompactd_do_work(pgdat);
  1580. }
  1581. return 0;
  1582. }
  1583. /*
  1584. * This kcompactd start function will be called by init and node-hot-add.
  1585. * On node-hot-add, kcompactd will moved to proper cpus if cpus are hot-added.
  1586. */
  1587. int kcompactd_run(int nid)
  1588. {
  1589. pg_data_t *pgdat = NODE_DATA(nid);
  1590. int ret = 0;
  1591. if (pgdat->kcompactd)
  1592. return 0;
  1593. pgdat->kcompactd = kthread_run(kcompactd, pgdat, "kcompactd%d", nid);
  1594. if (IS_ERR(pgdat->kcompactd)) {
  1595. pr_err("Failed to start kcompactd on node %d\n", nid);
  1596. ret = PTR_ERR(pgdat->kcompactd);
  1597. pgdat->kcompactd = NULL;
  1598. }
  1599. return ret;
  1600. }
  1601. /*
  1602. * Called by memory hotplug when all memory in a node is offlined. Caller must
  1603. * hold mem_hotplug_begin/end().
  1604. */
  1605. void kcompactd_stop(int nid)
  1606. {
  1607. struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd;
  1608. if (kcompactd) {
  1609. kthread_stop(kcompactd);
  1610. NODE_DATA(nid)->kcompactd = NULL;
  1611. }
  1612. }
  1613. /*
  1614. * It's optimal to keep kcompactd on the same CPUs as their memory, but
  1615. * not required for correctness. So if the last cpu in a node goes
  1616. * away, we get changed to run anywhere: as the first one comes back,
  1617. * restore their cpu bindings.
  1618. */
  1619. static int cpu_callback(struct notifier_block *nfb, unsigned long action,
  1620. void *hcpu)
  1621. {
  1622. int nid;
  1623. if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
  1624. for_each_node_state(nid, N_MEMORY) {
  1625. pg_data_t *pgdat = NODE_DATA(nid);
  1626. const struct cpumask *mask;
  1627. mask = cpumask_of_node(pgdat->node_id);
  1628. if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
  1629. /* One of our CPUs online: restore mask */
  1630. set_cpus_allowed_ptr(pgdat->kcompactd, mask);
  1631. }
  1632. }
  1633. return NOTIFY_OK;
  1634. }
  1635. static int __init kcompactd_init(void)
  1636. {
  1637. int nid;
  1638. for_each_node_state(nid, N_MEMORY)
  1639. kcompactd_run(nid);
  1640. hotcpu_notifier(cpu_callback, 0);
  1641. return 0;
  1642. }
  1643. subsys_initcall(kcompactd_init)
  1644. #endif /* CONFIG_COMPACTION */