compaction.c 57 KB

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