compaction.c 56 KB

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