vmscan.c 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577
  1. /*
  2. * linux/mm/vmscan.c
  3. *
  4. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  5. *
  6. * Swap reorganised 29.12.95, Stephen Tweedie.
  7. * kswapd added: 7.1.96 sct
  8. * Removed kswapd_ctl limits, and swap out as many pages as needed
  9. * to bring the system back to freepages.high: 2.4.97, Rik van Riel.
  10. * Zone aware kswapd started 02/00, Kanoj Sarcar (kanoj@sgi.com).
  11. * Multiqueue VM started 5.8.00, Rik van Riel.
  12. */
  13. #include <linux/mm.h>
  14. #include <linux/module.h>
  15. #include <linux/gfp.h>
  16. #include <linux/kernel_stat.h>
  17. #include <linux/swap.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/init.h>
  20. #include <linux/highmem.h>
  21. #include <linux/vmstat.h>
  22. #include <linux/file.h>
  23. #include <linux/writeback.h>
  24. #include <linux/blkdev.h>
  25. #include <linux/buffer_head.h> /* for try_to_release_page(),
  26. buffer_heads_over_limit */
  27. #include <linux/mm_inline.h>
  28. #include <linux/pagevec.h>
  29. #include <linux/backing-dev.h>
  30. #include <linux/rmap.h>
  31. #include <linux/topology.h>
  32. #include <linux/cpu.h>
  33. #include <linux/cpuset.h>
  34. #include <linux/compaction.h>
  35. #include <linux/notifier.h>
  36. #include <linux/rwsem.h>
  37. #include <linux/delay.h>
  38. #include <linux/kthread.h>
  39. #include <linux/freezer.h>
  40. #include <linux/memcontrol.h>
  41. #include <linux/delayacct.h>
  42. #include <linux/sysctl.h>
  43. #include <linux/oom.h>
  44. #include <linux/prefetch.h>
  45. #include <asm/tlbflush.h>
  46. #include <asm/div64.h>
  47. #include <linux/swapops.h>
  48. #include "internal.h"
  49. #define CREATE_TRACE_POINTS
  50. #include <trace/events/vmscan.h>
  51. /*
  52. * reclaim_mode determines how the inactive list is shrunk
  53. * RECLAIM_MODE_SINGLE: Reclaim only order-0 pages
  54. * RECLAIM_MODE_ASYNC: Do not block
  55. * RECLAIM_MODE_SYNC: Allow blocking e.g. call wait_on_page_writeback
  56. * RECLAIM_MODE_LUMPYRECLAIM: For high-order allocations, take a reference
  57. * page from the LRU and reclaim all pages within a
  58. * naturally aligned range
  59. * RECLAIM_MODE_COMPACTION: For high-order allocations, reclaim a number of
  60. * order-0 pages and then compact the zone
  61. */
  62. typedef unsigned __bitwise__ reclaim_mode_t;
  63. #define RECLAIM_MODE_SINGLE ((__force reclaim_mode_t)0x01u)
  64. #define RECLAIM_MODE_ASYNC ((__force reclaim_mode_t)0x02u)
  65. #define RECLAIM_MODE_SYNC ((__force reclaim_mode_t)0x04u)
  66. #define RECLAIM_MODE_LUMPYRECLAIM ((__force reclaim_mode_t)0x08u)
  67. #define RECLAIM_MODE_COMPACTION ((__force reclaim_mode_t)0x10u)
  68. struct scan_control {
  69. /* Incremented by the number of inactive pages that were scanned */
  70. unsigned long nr_scanned;
  71. /* Number of pages freed so far during a call to shrink_zones() */
  72. unsigned long nr_reclaimed;
  73. /* How many pages shrink_list() should reclaim */
  74. unsigned long nr_to_reclaim;
  75. unsigned long hibernation_mode;
  76. /* This context's GFP mask */
  77. gfp_t gfp_mask;
  78. int may_writepage;
  79. /* Can mapped pages be reclaimed? */
  80. int may_unmap;
  81. /* Can pages be swapped as part of reclaim? */
  82. int may_swap;
  83. int order;
  84. /*
  85. * Intend to reclaim enough continuous memory rather than reclaim
  86. * enough amount of memory. i.e, mode for high order allocation.
  87. */
  88. reclaim_mode_t reclaim_mode;
  89. /*
  90. * The memory cgroup that hit its limit and as a result is the
  91. * primary target of this reclaim invocation.
  92. */
  93. struct mem_cgroup *target_mem_cgroup;
  94. /*
  95. * Nodemask of nodes allowed by the caller. If NULL, all nodes
  96. * are scanned.
  97. */
  98. nodemask_t *nodemask;
  99. };
  100. struct mem_cgroup_zone {
  101. struct mem_cgroup *mem_cgroup;
  102. struct zone *zone;
  103. };
  104. #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
  105. #ifdef ARCH_HAS_PREFETCH
  106. #define prefetch_prev_lru_page(_page, _base, _field) \
  107. do { \
  108. if ((_page)->lru.prev != _base) { \
  109. struct page *prev; \
  110. \
  111. prev = lru_to_page(&(_page->lru)); \
  112. prefetch(&prev->_field); \
  113. } \
  114. } while (0)
  115. #else
  116. #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
  117. #endif
  118. #ifdef ARCH_HAS_PREFETCHW
  119. #define prefetchw_prev_lru_page(_page, _base, _field) \
  120. do { \
  121. if ((_page)->lru.prev != _base) { \
  122. struct page *prev; \
  123. \
  124. prev = lru_to_page(&(_page->lru)); \
  125. prefetchw(&prev->_field); \
  126. } \
  127. } while (0)
  128. #else
  129. #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
  130. #endif
  131. /*
  132. * From 0 .. 100. Higher means more swappy.
  133. */
  134. int vm_swappiness = 60;
  135. long vm_total_pages; /* The total number of pages which the VM controls */
  136. static LIST_HEAD(shrinker_list);
  137. static DECLARE_RWSEM(shrinker_rwsem);
  138. #ifdef CONFIG_CGROUP_MEM_RES_CTLR
  139. static bool global_reclaim(struct scan_control *sc)
  140. {
  141. return !sc->target_mem_cgroup;
  142. }
  143. static bool scanning_global_lru(struct mem_cgroup_zone *mz)
  144. {
  145. return !mz->mem_cgroup;
  146. }
  147. #else
  148. static bool global_reclaim(struct scan_control *sc)
  149. {
  150. return true;
  151. }
  152. static bool scanning_global_lru(struct mem_cgroup_zone *mz)
  153. {
  154. return true;
  155. }
  156. #endif
  157. static struct zone_reclaim_stat *get_reclaim_stat(struct mem_cgroup_zone *mz)
  158. {
  159. if (!scanning_global_lru(mz))
  160. return mem_cgroup_get_reclaim_stat(mz->mem_cgroup, mz->zone);
  161. return &mz->zone->reclaim_stat;
  162. }
  163. static unsigned long zone_nr_lru_pages(struct mem_cgroup_zone *mz,
  164. enum lru_list lru)
  165. {
  166. if (!scanning_global_lru(mz))
  167. return mem_cgroup_zone_nr_lru_pages(mz->mem_cgroup,
  168. zone_to_nid(mz->zone),
  169. zone_idx(mz->zone),
  170. BIT(lru));
  171. return zone_page_state(mz->zone, NR_LRU_BASE + lru);
  172. }
  173. /*
  174. * Add a shrinker callback to be called from the vm
  175. */
  176. void register_shrinker(struct shrinker *shrinker)
  177. {
  178. atomic_long_set(&shrinker->nr_in_batch, 0);
  179. down_write(&shrinker_rwsem);
  180. list_add_tail(&shrinker->list, &shrinker_list);
  181. up_write(&shrinker_rwsem);
  182. }
  183. EXPORT_SYMBOL(register_shrinker);
  184. /*
  185. * Remove one
  186. */
  187. void unregister_shrinker(struct shrinker *shrinker)
  188. {
  189. down_write(&shrinker_rwsem);
  190. list_del(&shrinker->list);
  191. up_write(&shrinker_rwsem);
  192. }
  193. EXPORT_SYMBOL(unregister_shrinker);
  194. static inline int do_shrinker_shrink(struct shrinker *shrinker,
  195. struct shrink_control *sc,
  196. unsigned long nr_to_scan)
  197. {
  198. sc->nr_to_scan = nr_to_scan;
  199. return (*shrinker->shrink)(shrinker, sc);
  200. }
  201. #define SHRINK_BATCH 128
  202. /*
  203. * Call the shrink functions to age shrinkable caches
  204. *
  205. * Here we assume it costs one seek to replace a lru page and that it also
  206. * takes a seek to recreate a cache object. With this in mind we age equal
  207. * percentages of the lru and ageable caches. This should balance the seeks
  208. * generated by these structures.
  209. *
  210. * If the vm encountered mapped pages on the LRU it increase the pressure on
  211. * slab to avoid swapping.
  212. *
  213. * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
  214. *
  215. * `lru_pages' represents the number of on-LRU pages in all the zones which
  216. * are eligible for the caller's allocation attempt. It is used for balancing
  217. * slab reclaim versus page reclaim.
  218. *
  219. * Returns the number of slab objects which we shrunk.
  220. */
  221. unsigned long shrink_slab(struct shrink_control *shrink,
  222. unsigned long nr_pages_scanned,
  223. unsigned long lru_pages)
  224. {
  225. struct shrinker *shrinker;
  226. unsigned long ret = 0;
  227. if (nr_pages_scanned == 0)
  228. nr_pages_scanned = SWAP_CLUSTER_MAX;
  229. if (!down_read_trylock(&shrinker_rwsem)) {
  230. /* Assume we'll be able to shrink next time */
  231. ret = 1;
  232. goto out;
  233. }
  234. list_for_each_entry(shrinker, &shrinker_list, list) {
  235. unsigned long long delta;
  236. long total_scan;
  237. long max_pass;
  238. int shrink_ret = 0;
  239. long nr;
  240. long new_nr;
  241. long batch_size = shrinker->batch ? shrinker->batch
  242. : SHRINK_BATCH;
  243. max_pass = do_shrinker_shrink(shrinker, shrink, 0);
  244. if (max_pass <= 0)
  245. continue;
  246. /*
  247. * copy the current shrinker scan count into a local variable
  248. * and zero it so that other concurrent shrinker invocations
  249. * don't also do this scanning work.
  250. */
  251. nr = atomic_long_xchg(&shrinker->nr_in_batch, 0);
  252. total_scan = nr;
  253. delta = (4 * nr_pages_scanned) / shrinker->seeks;
  254. delta *= max_pass;
  255. do_div(delta, lru_pages + 1);
  256. total_scan += delta;
  257. if (total_scan < 0) {
  258. printk(KERN_ERR "shrink_slab: %pF negative objects to "
  259. "delete nr=%ld\n",
  260. shrinker->shrink, total_scan);
  261. total_scan = max_pass;
  262. }
  263. /*
  264. * We need to avoid excessive windup on filesystem shrinkers
  265. * due to large numbers of GFP_NOFS allocations causing the
  266. * shrinkers to return -1 all the time. This results in a large
  267. * nr being built up so when a shrink that can do some work
  268. * comes along it empties the entire cache due to nr >>>
  269. * max_pass. This is bad for sustaining a working set in
  270. * memory.
  271. *
  272. * Hence only allow the shrinker to scan the entire cache when
  273. * a large delta change is calculated directly.
  274. */
  275. if (delta < max_pass / 4)
  276. total_scan = min(total_scan, max_pass / 2);
  277. /*
  278. * Avoid risking looping forever due to too large nr value:
  279. * never try to free more than twice the estimate number of
  280. * freeable entries.
  281. */
  282. if (total_scan > max_pass * 2)
  283. total_scan = max_pass * 2;
  284. trace_mm_shrink_slab_start(shrinker, shrink, nr,
  285. nr_pages_scanned, lru_pages,
  286. max_pass, delta, total_scan);
  287. while (total_scan >= batch_size) {
  288. int nr_before;
  289. nr_before = do_shrinker_shrink(shrinker, shrink, 0);
  290. shrink_ret = do_shrinker_shrink(shrinker, shrink,
  291. batch_size);
  292. if (shrink_ret == -1)
  293. break;
  294. if (shrink_ret < nr_before)
  295. ret += nr_before - shrink_ret;
  296. count_vm_events(SLABS_SCANNED, batch_size);
  297. total_scan -= batch_size;
  298. cond_resched();
  299. }
  300. /*
  301. * move the unused scan count back into the shrinker in a
  302. * manner that handles concurrent updates. If we exhausted the
  303. * scan, there is no need to do an update.
  304. */
  305. if (total_scan > 0)
  306. new_nr = atomic_long_add_return(total_scan,
  307. &shrinker->nr_in_batch);
  308. else
  309. new_nr = atomic_long_read(&shrinker->nr_in_batch);
  310. trace_mm_shrink_slab_end(shrinker, shrink_ret, nr, new_nr);
  311. }
  312. up_read(&shrinker_rwsem);
  313. out:
  314. cond_resched();
  315. return ret;
  316. }
  317. static void set_reclaim_mode(int priority, struct scan_control *sc,
  318. bool sync)
  319. {
  320. reclaim_mode_t syncmode = sync ? RECLAIM_MODE_SYNC : RECLAIM_MODE_ASYNC;
  321. /*
  322. * Initially assume we are entering either lumpy reclaim or
  323. * reclaim/compaction.Depending on the order, we will either set the
  324. * sync mode or just reclaim order-0 pages later.
  325. */
  326. if (COMPACTION_BUILD)
  327. sc->reclaim_mode = RECLAIM_MODE_COMPACTION;
  328. else
  329. sc->reclaim_mode = RECLAIM_MODE_LUMPYRECLAIM;
  330. /*
  331. * Avoid using lumpy reclaim or reclaim/compaction if possible by
  332. * restricting when its set to either costly allocations or when
  333. * under memory pressure
  334. */
  335. if (sc->order > PAGE_ALLOC_COSTLY_ORDER)
  336. sc->reclaim_mode |= syncmode;
  337. else if (sc->order && priority < DEF_PRIORITY - 2)
  338. sc->reclaim_mode |= syncmode;
  339. else
  340. sc->reclaim_mode = RECLAIM_MODE_SINGLE | RECLAIM_MODE_ASYNC;
  341. }
  342. static void reset_reclaim_mode(struct scan_control *sc)
  343. {
  344. sc->reclaim_mode = RECLAIM_MODE_SINGLE | RECLAIM_MODE_ASYNC;
  345. }
  346. static inline int is_page_cache_freeable(struct page *page)
  347. {
  348. /*
  349. * A freeable page cache page is referenced only by the caller
  350. * that isolated the page, the page cache radix tree and
  351. * optional buffer heads at page->private.
  352. */
  353. return page_count(page) - page_has_private(page) == 2;
  354. }
  355. static int may_write_to_queue(struct backing_dev_info *bdi,
  356. struct scan_control *sc)
  357. {
  358. if (current->flags & PF_SWAPWRITE)
  359. return 1;
  360. if (!bdi_write_congested(bdi))
  361. return 1;
  362. if (bdi == current->backing_dev_info)
  363. return 1;
  364. /* lumpy reclaim for hugepage often need a lot of write */
  365. if (sc->order > PAGE_ALLOC_COSTLY_ORDER)
  366. return 1;
  367. return 0;
  368. }
  369. /*
  370. * We detected a synchronous write error writing a page out. Probably
  371. * -ENOSPC. We need to propagate that into the address_space for a subsequent
  372. * fsync(), msync() or close().
  373. *
  374. * The tricky part is that after writepage we cannot touch the mapping: nothing
  375. * prevents it from being freed up. But we have a ref on the page and once
  376. * that page is locked, the mapping is pinned.
  377. *
  378. * We're allowed to run sleeping lock_page() here because we know the caller has
  379. * __GFP_FS.
  380. */
  381. static void handle_write_error(struct address_space *mapping,
  382. struct page *page, int error)
  383. {
  384. lock_page(page);
  385. if (page_mapping(page) == mapping)
  386. mapping_set_error(mapping, error);
  387. unlock_page(page);
  388. }
  389. /* possible outcome of pageout() */
  390. typedef enum {
  391. /* failed to write page out, page is locked */
  392. PAGE_KEEP,
  393. /* move page to the active list, page is locked */
  394. PAGE_ACTIVATE,
  395. /* page has been sent to the disk successfully, page is unlocked */
  396. PAGE_SUCCESS,
  397. /* page is clean and locked */
  398. PAGE_CLEAN,
  399. } pageout_t;
  400. /*
  401. * pageout is called by shrink_page_list() for each dirty page.
  402. * Calls ->writepage().
  403. */
  404. static pageout_t pageout(struct page *page, struct address_space *mapping,
  405. struct scan_control *sc)
  406. {
  407. /*
  408. * If the page is dirty, only perform writeback if that write
  409. * will be non-blocking. To prevent this allocation from being
  410. * stalled by pagecache activity. But note that there may be
  411. * stalls if we need to run get_block(). We could test
  412. * PagePrivate for that.
  413. *
  414. * If this process is currently in __generic_file_aio_write() against
  415. * this page's queue, we can perform writeback even if that
  416. * will block.
  417. *
  418. * If the page is swapcache, write it back even if that would
  419. * block, for some throttling. This happens by accident, because
  420. * swap_backing_dev_info is bust: it doesn't reflect the
  421. * congestion state of the swapdevs. Easy to fix, if needed.
  422. */
  423. if (!is_page_cache_freeable(page))
  424. return PAGE_KEEP;
  425. if (!mapping) {
  426. /*
  427. * Some data journaling orphaned pages can have
  428. * page->mapping == NULL while being dirty with clean buffers.
  429. */
  430. if (page_has_private(page)) {
  431. if (try_to_free_buffers(page)) {
  432. ClearPageDirty(page);
  433. printk("%s: orphaned page\n", __func__);
  434. return PAGE_CLEAN;
  435. }
  436. }
  437. return PAGE_KEEP;
  438. }
  439. if (mapping->a_ops->writepage == NULL)
  440. return PAGE_ACTIVATE;
  441. if (!may_write_to_queue(mapping->backing_dev_info, sc))
  442. return PAGE_KEEP;
  443. if (clear_page_dirty_for_io(page)) {
  444. int res;
  445. struct writeback_control wbc = {
  446. .sync_mode = WB_SYNC_NONE,
  447. .nr_to_write = SWAP_CLUSTER_MAX,
  448. .range_start = 0,
  449. .range_end = LLONG_MAX,
  450. .for_reclaim = 1,
  451. };
  452. SetPageReclaim(page);
  453. res = mapping->a_ops->writepage(page, &wbc);
  454. if (res < 0)
  455. handle_write_error(mapping, page, res);
  456. if (res == AOP_WRITEPAGE_ACTIVATE) {
  457. ClearPageReclaim(page);
  458. return PAGE_ACTIVATE;
  459. }
  460. if (!PageWriteback(page)) {
  461. /* synchronous write or broken a_ops? */
  462. ClearPageReclaim(page);
  463. }
  464. trace_mm_vmscan_writepage(page,
  465. trace_reclaim_flags(page, sc->reclaim_mode));
  466. inc_zone_page_state(page, NR_VMSCAN_WRITE);
  467. return PAGE_SUCCESS;
  468. }
  469. return PAGE_CLEAN;
  470. }
  471. /*
  472. * Same as remove_mapping, but if the page is removed from the mapping, it
  473. * gets returned with a refcount of 0.
  474. */
  475. static int __remove_mapping(struct address_space *mapping, struct page *page)
  476. {
  477. BUG_ON(!PageLocked(page));
  478. BUG_ON(mapping != page_mapping(page));
  479. spin_lock_irq(&mapping->tree_lock);
  480. /*
  481. * The non racy check for a busy page.
  482. *
  483. * Must be careful with the order of the tests. When someone has
  484. * a ref to the page, it may be possible that they dirty it then
  485. * drop the reference. So if PageDirty is tested before page_count
  486. * here, then the following race may occur:
  487. *
  488. * get_user_pages(&page);
  489. * [user mapping goes away]
  490. * write_to(page);
  491. * !PageDirty(page) [good]
  492. * SetPageDirty(page);
  493. * put_page(page);
  494. * !page_count(page) [good, discard it]
  495. *
  496. * [oops, our write_to data is lost]
  497. *
  498. * Reversing the order of the tests ensures such a situation cannot
  499. * escape unnoticed. The smp_rmb is needed to ensure the page->flags
  500. * load is not satisfied before that of page->_count.
  501. *
  502. * Note that if SetPageDirty is always performed via set_page_dirty,
  503. * and thus under tree_lock, then this ordering is not required.
  504. */
  505. if (!page_freeze_refs(page, 2))
  506. goto cannot_free;
  507. /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */
  508. if (unlikely(PageDirty(page))) {
  509. page_unfreeze_refs(page, 2);
  510. goto cannot_free;
  511. }
  512. if (PageSwapCache(page)) {
  513. swp_entry_t swap = { .val = page_private(page) };
  514. __delete_from_swap_cache(page);
  515. spin_unlock_irq(&mapping->tree_lock);
  516. swapcache_free(swap, page);
  517. } else {
  518. void (*freepage)(struct page *);
  519. freepage = mapping->a_ops->freepage;
  520. __delete_from_page_cache(page);
  521. spin_unlock_irq(&mapping->tree_lock);
  522. mem_cgroup_uncharge_cache_page(page);
  523. if (freepage != NULL)
  524. freepage(page);
  525. }
  526. return 1;
  527. cannot_free:
  528. spin_unlock_irq(&mapping->tree_lock);
  529. return 0;
  530. }
  531. /*
  532. * Attempt to detach a locked page from its ->mapping. If it is dirty or if
  533. * someone else has a ref on the page, abort and return 0. If it was
  534. * successfully detached, return 1. Assumes the caller has a single ref on
  535. * this page.
  536. */
  537. int remove_mapping(struct address_space *mapping, struct page *page)
  538. {
  539. if (__remove_mapping(mapping, page)) {
  540. /*
  541. * Unfreezing the refcount with 1 rather than 2 effectively
  542. * drops the pagecache ref for us without requiring another
  543. * atomic operation.
  544. */
  545. page_unfreeze_refs(page, 1);
  546. return 1;
  547. }
  548. return 0;
  549. }
  550. /**
  551. * putback_lru_page - put previously isolated page onto appropriate LRU list
  552. * @page: page to be put back to appropriate lru list
  553. *
  554. * Add previously isolated @page to appropriate LRU list.
  555. * Page may still be unevictable for other reasons.
  556. *
  557. * lru_lock must not be held, interrupts must be enabled.
  558. */
  559. void putback_lru_page(struct page *page)
  560. {
  561. int lru;
  562. int active = !!TestClearPageActive(page);
  563. int was_unevictable = PageUnevictable(page);
  564. VM_BUG_ON(PageLRU(page));
  565. redo:
  566. ClearPageUnevictable(page);
  567. if (page_evictable(page, NULL)) {
  568. /*
  569. * For evictable pages, we can use the cache.
  570. * In event of a race, worst case is we end up with an
  571. * unevictable page on [in]active list.
  572. * We know how to handle that.
  573. */
  574. lru = active + page_lru_base_type(page);
  575. lru_cache_add_lru(page, lru);
  576. } else {
  577. /*
  578. * Put unevictable pages directly on zone's unevictable
  579. * list.
  580. */
  581. lru = LRU_UNEVICTABLE;
  582. add_page_to_unevictable_list(page);
  583. /*
  584. * When racing with an mlock or AS_UNEVICTABLE clearing
  585. * (page is unlocked) make sure that if the other thread
  586. * does not observe our setting of PG_lru and fails
  587. * isolation/check_move_unevictable_page,
  588. * we see PG_mlocked/AS_UNEVICTABLE cleared below and move
  589. * the page back to the evictable list.
  590. *
  591. * The other side is TestClearPageMlocked() or shmem_lock().
  592. */
  593. smp_mb();
  594. }
  595. /*
  596. * page's status can change while we move it among lru. If an evictable
  597. * page is on unevictable list, it never be freed. To avoid that,
  598. * check after we added it to the list, again.
  599. */
  600. if (lru == LRU_UNEVICTABLE && page_evictable(page, NULL)) {
  601. if (!isolate_lru_page(page)) {
  602. put_page(page);
  603. goto redo;
  604. }
  605. /* This means someone else dropped this page from LRU
  606. * So, it will be freed or putback to LRU again. There is
  607. * nothing to do here.
  608. */
  609. }
  610. if (was_unevictable && lru != LRU_UNEVICTABLE)
  611. count_vm_event(UNEVICTABLE_PGRESCUED);
  612. else if (!was_unevictable && lru == LRU_UNEVICTABLE)
  613. count_vm_event(UNEVICTABLE_PGCULLED);
  614. put_page(page); /* drop ref from isolate */
  615. }
  616. enum page_references {
  617. PAGEREF_RECLAIM,
  618. PAGEREF_RECLAIM_CLEAN,
  619. PAGEREF_KEEP,
  620. PAGEREF_ACTIVATE,
  621. };
  622. static enum page_references page_check_references(struct page *page,
  623. struct mem_cgroup_zone *mz,
  624. struct scan_control *sc)
  625. {
  626. int referenced_ptes, referenced_page;
  627. unsigned long vm_flags;
  628. referenced_ptes = page_referenced(page, 1, mz->mem_cgroup, &vm_flags);
  629. referenced_page = TestClearPageReferenced(page);
  630. /* Lumpy reclaim - ignore references */
  631. if (sc->reclaim_mode & RECLAIM_MODE_LUMPYRECLAIM)
  632. return PAGEREF_RECLAIM;
  633. /*
  634. * Mlock lost the isolation race with us. Let try_to_unmap()
  635. * move the page to the unevictable list.
  636. */
  637. if (vm_flags & VM_LOCKED)
  638. return PAGEREF_RECLAIM;
  639. if (referenced_ptes) {
  640. if (PageAnon(page))
  641. return PAGEREF_ACTIVATE;
  642. /*
  643. * All mapped pages start out with page table
  644. * references from the instantiating fault, so we need
  645. * to look twice if a mapped file page is used more
  646. * than once.
  647. *
  648. * Mark it and spare it for another trip around the
  649. * inactive list. Another page table reference will
  650. * lead to its activation.
  651. *
  652. * Note: the mark is set for activated pages as well
  653. * so that recently deactivated but used pages are
  654. * quickly recovered.
  655. */
  656. SetPageReferenced(page);
  657. if (referenced_page || referenced_ptes > 1)
  658. return PAGEREF_ACTIVATE;
  659. /*
  660. * Activate file-backed executable pages after first usage.
  661. */
  662. if (vm_flags & VM_EXEC)
  663. return PAGEREF_ACTIVATE;
  664. return PAGEREF_KEEP;
  665. }
  666. /* Reclaim if clean, defer dirty pages to writeback */
  667. if (referenced_page && !PageSwapBacked(page))
  668. return PAGEREF_RECLAIM_CLEAN;
  669. return PAGEREF_RECLAIM;
  670. }
  671. /*
  672. * shrink_page_list() returns the number of reclaimed pages
  673. */
  674. static unsigned long shrink_page_list(struct list_head *page_list,
  675. struct mem_cgroup_zone *mz,
  676. struct scan_control *sc,
  677. int priority,
  678. unsigned long *ret_nr_dirty,
  679. unsigned long *ret_nr_writeback)
  680. {
  681. LIST_HEAD(ret_pages);
  682. LIST_HEAD(free_pages);
  683. int pgactivate = 0;
  684. unsigned long nr_dirty = 0;
  685. unsigned long nr_congested = 0;
  686. unsigned long nr_reclaimed = 0;
  687. unsigned long nr_writeback = 0;
  688. cond_resched();
  689. while (!list_empty(page_list)) {
  690. enum page_references references;
  691. struct address_space *mapping;
  692. struct page *page;
  693. int may_enter_fs;
  694. cond_resched();
  695. page = lru_to_page(page_list);
  696. list_del(&page->lru);
  697. if (!trylock_page(page))
  698. goto keep;
  699. VM_BUG_ON(PageActive(page));
  700. VM_BUG_ON(page_zone(page) != mz->zone);
  701. sc->nr_scanned++;
  702. if (unlikely(!page_evictable(page, NULL)))
  703. goto cull_mlocked;
  704. if (!sc->may_unmap && page_mapped(page))
  705. goto keep_locked;
  706. /* Double the slab pressure for mapped and swapcache pages */
  707. if (page_mapped(page) || PageSwapCache(page))
  708. sc->nr_scanned++;
  709. may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
  710. (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
  711. if (PageWriteback(page)) {
  712. nr_writeback++;
  713. /*
  714. * Synchronous reclaim cannot queue pages for
  715. * writeback due to the possibility of stack overflow
  716. * but if it encounters a page under writeback, wait
  717. * for the IO to complete.
  718. */
  719. if ((sc->reclaim_mode & RECLAIM_MODE_SYNC) &&
  720. may_enter_fs)
  721. wait_on_page_writeback(page);
  722. else {
  723. unlock_page(page);
  724. goto keep_lumpy;
  725. }
  726. }
  727. references = page_check_references(page, mz, sc);
  728. switch (references) {
  729. case PAGEREF_ACTIVATE:
  730. goto activate_locked;
  731. case PAGEREF_KEEP:
  732. goto keep_locked;
  733. case PAGEREF_RECLAIM:
  734. case PAGEREF_RECLAIM_CLEAN:
  735. ; /* try to reclaim the page below */
  736. }
  737. /*
  738. * Anonymous process memory has backing store?
  739. * Try to allocate it some swap space here.
  740. */
  741. if (PageAnon(page) && !PageSwapCache(page)) {
  742. if (!(sc->gfp_mask & __GFP_IO))
  743. goto keep_locked;
  744. if (!add_to_swap(page))
  745. goto activate_locked;
  746. may_enter_fs = 1;
  747. }
  748. mapping = page_mapping(page);
  749. /*
  750. * The page is mapped into the page tables of one or more
  751. * processes. Try to unmap it here.
  752. */
  753. if (page_mapped(page) && mapping) {
  754. switch (try_to_unmap(page, TTU_UNMAP)) {
  755. case SWAP_FAIL:
  756. goto activate_locked;
  757. case SWAP_AGAIN:
  758. goto keep_locked;
  759. case SWAP_MLOCK:
  760. goto cull_mlocked;
  761. case SWAP_SUCCESS:
  762. ; /* try to free the page below */
  763. }
  764. }
  765. if (PageDirty(page)) {
  766. nr_dirty++;
  767. /*
  768. * Only kswapd can writeback filesystem pages to
  769. * avoid risk of stack overflow but do not writeback
  770. * unless under significant pressure.
  771. */
  772. if (page_is_file_cache(page) &&
  773. (!current_is_kswapd() || priority >= DEF_PRIORITY - 2)) {
  774. /*
  775. * Immediately reclaim when written back.
  776. * Similar in principal to deactivate_page()
  777. * except we already have the page isolated
  778. * and know it's dirty
  779. */
  780. inc_zone_page_state(page, NR_VMSCAN_IMMEDIATE);
  781. SetPageReclaim(page);
  782. goto keep_locked;
  783. }
  784. if (references == PAGEREF_RECLAIM_CLEAN)
  785. goto keep_locked;
  786. if (!may_enter_fs)
  787. goto keep_locked;
  788. if (!sc->may_writepage)
  789. goto keep_locked;
  790. /* Page is dirty, try to write it out here */
  791. switch (pageout(page, mapping, sc)) {
  792. case PAGE_KEEP:
  793. nr_congested++;
  794. goto keep_locked;
  795. case PAGE_ACTIVATE:
  796. goto activate_locked;
  797. case PAGE_SUCCESS:
  798. if (PageWriteback(page))
  799. goto keep_lumpy;
  800. if (PageDirty(page))
  801. goto keep;
  802. /*
  803. * A synchronous write - probably a ramdisk. Go
  804. * ahead and try to reclaim the page.
  805. */
  806. if (!trylock_page(page))
  807. goto keep;
  808. if (PageDirty(page) || PageWriteback(page))
  809. goto keep_locked;
  810. mapping = page_mapping(page);
  811. case PAGE_CLEAN:
  812. ; /* try to free the page below */
  813. }
  814. }
  815. /*
  816. * If the page has buffers, try to free the buffer mappings
  817. * associated with this page. If we succeed we try to free
  818. * the page as well.
  819. *
  820. * We do this even if the page is PageDirty().
  821. * try_to_release_page() does not perform I/O, but it is
  822. * possible for a page to have PageDirty set, but it is actually
  823. * clean (all its buffers are clean). This happens if the
  824. * buffers were written out directly, with submit_bh(). ext3
  825. * will do this, as well as the blockdev mapping.
  826. * try_to_release_page() will discover that cleanness and will
  827. * drop the buffers and mark the page clean - it can be freed.
  828. *
  829. * Rarely, pages can have buffers and no ->mapping. These are
  830. * the pages which were not successfully invalidated in
  831. * truncate_complete_page(). We try to drop those buffers here
  832. * and if that worked, and the page is no longer mapped into
  833. * process address space (page_count == 1) it can be freed.
  834. * Otherwise, leave the page on the LRU so it is swappable.
  835. */
  836. if (page_has_private(page)) {
  837. if (!try_to_release_page(page, sc->gfp_mask))
  838. goto activate_locked;
  839. if (!mapping && page_count(page) == 1) {
  840. unlock_page(page);
  841. if (put_page_testzero(page))
  842. goto free_it;
  843. else {
  844. /*
  845. * rare race with speculative reference.
  846. * the speculative reference will free
  847. * this page shortly, so we may
  848. * increment nr_reclaimed here (and
  849. * leave it off the LRU).
  850. */
  851. nr_reclaimed++;
  852. continue;
  853. }
  854. }
  855. }
  856. if (!mapping || !__remove_mapping(mapping, page))
  857. goto keep_locked;
  858. /*
  859. * At this point, we have no other references and there is
  860. * no way to pick any more up (removed from LRU, removed
  861. * from pagecache). Can use non-atomic bitops now (and
  862. * we obviously don't have to worry about waking up a process
  863. * waiting on the page lock, because there are no references.
  864. */
  865. __clear_page_locked(page);
  866. free_it:
  867. nr_reclaimed++;
  868. /*
  869. * Is there need to periodically free_page_list? It would
  870. * appear not as the counts should be low
  871. */
  872. list_add(&page->lru, &free_pages);
  873. continue;
  874. cull_mlocked:
  875. if (PageSwapCache(page))
  876. try_to_free_swap(page);
  877. unlock_page(page);
  878. putback_lru_page(page);
  879. reset_reclaim_mode(sc);
  880. continue;
  881. activate_locked:
  882. /* Not a candidate for swapping, so reclaim swap space. */
  883. if (PageSwapCache(page) && vm_swap_full())
  884. try_to_free_swap(page);
  885. VM_BUG_ON(PageActive(page));
  886. SetPageActive(page);
  887. pgactivate++;
  888. keep_locked:
  889. unlock_page(page);
  890. keep:
  891. reset_reclaim_mode(sc);
  892. keep_lumpy:
  893. list_add(&page->lru, &ret_pages);
  894. VM_BUG_ON(PageLRU(page) || PageUnevictable(page));
  895. }
  896. /*
  897. * Tag a zone as congested if all the dirty pages encountered were
  898. * backed by a congested BDI. In this case, reclaimers should just
  899. * back off and wait for congestion to clear because further reclaim
  900. * will encounter the same problem
  901. */
  902. if (nr_dirty && nr_dirty == nr_congested && global_reclaim(sc))
  903. zone_set_flag(mz->zone, ZONE_CONGESTED);
  904. free_hot_cold_page_list(&free_pages, 1);
  905. list_splice(&ret_pages, page_list);
  906. count_vm_events(PGACTIVATE, pgactivate);
  907. *ret_nr_dirty += nr_dirty;
  908. *ret_nr_writeback += nr_writeback;
  909. return nr_reclaimed;
  910. }
  911. /*
  912. * Attempt to remove the specified page from its LRU. Only take this page
  913. * if it is of the appropriate PageActive status. Pages which are being
  914. * freed elsewhere are also ignored.
  915. *
  916. * page: page to consider
  917. * mode: one of the LRU isolation modes defined above
  918. *
  919. * returns 0 on success, -ve errno on failure.
  920. */
  921. int __isolate_lru_page(struct page *page, isolate_mode_t mode, int file)
  922. {
  923. bool all_lru_mode;
  924. int ret = -EINVAL;
  925. /* Only take pages on the LRU. */
  926. if (!PageLRU(page))
  927. return ret;
  928. all_lru_mode = (mode & (ISOLATE_ACTIVE|ISOLATE_INACTIVE)) ==
  929. (ISOLATE_ACTIVE|ISOLATE_INACTIVE);
  930. /*
  931. * When checking the active state, we need to be sure we are
  932. * dealing with comparible boolean values. Take the logical not
  933. * of each.
  934. */
  935. if (!all_lru_mode && !PageActive(page) != !(mode & ISOLATE_ACTIVE))
  936. return ret;
  937. if (!all_lru_mode && !!page_is_file_cache(page) != file)
  938. return ret;
  939. /*
  940. * When this function is being called for lumpy reclaim, we
  941. * initially look into all LRU pages, active, inactive and
  942. * unevictable; only give shrink_page_list evictable pages.
  943. */
  944. if (PageUnevictable(page))
  945. return ret;
  946. ret = -EBUSY;
  947. if ((mode & ISOLATE_CLEAN) && (PageDirty(page) || PageWriteback(page)))
  948. return ret;
  949. if ((mode & ISOLATE_UNMAPPED) && page_mapped(page))
  950. return ret;
  951. if (likely(get_page_unless_zero(page))) {
  952. /*
  953. * Be careful not to clear PageLRU until after we're
  954. * sure the page is not being freed elsewhere -- the
  955. * page release code relies on it.
  956. */
  957. ClearPageLRU(page);
  958. ret = 0;
  959. }
  960. return ret;
  961. }
  962. /*
  963. * zone->lru_lock is heavily contended. Some of the functions that
  964. * shrink the lists perform better by taking out a batch of pages
  965. * and working on them outside the LRU lock.
  966. *
  967. * For pagecache intensive workloads, this function is the hottest
  968. * spot in the kernel (apart from copy_*_user functions).
  969. *
  970. * Appropriate locks must be held before calling this function.
  971. *
  972. * @nr_to_scan: The number of pages to look through on the list.
  973. * @src: The LRU list to pull pages off.
  974. * @dst: The temp list to put pages on to.
  975. * @scanned: The number of pages that were scanned.
  976. * @order: The caller's attempted allocation order
  977. * @mode: One of the LRU isolation modes
  978. * @file: True [1] if isolating file [!anon] pages
  979. *
  980. * returns how many pages were moved onto *@dst.
  981. */
  982. static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
  983. struct list_head *src, struct list_head *dst,
  984. unsigned long *scanned, int order, isolate_mode_t mode,
  985. int file)
  986. {
  987. unsigned long nr_taken = 0;
  988. unsigned long nr_lumpy_taken = 0;
  989. unsigned long nr_lumpy_dirty = 0;
  990. unsigned long nr_lumpy_failed = 0;
  991. unsigned long scan;
  992. for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
  993. struct page *page;
  994. unsigned long pfn;
  995. unsigned long end_pfn;
  996. unsigned long page_pfn;
  997. int zone_id;
  998. page = lru_to_page(src);
  999. prefetchw_prev_lru_page(page, src, flags);
  1000. VM_BUG_ON(!PageLRU(page));
  1001. switch (__isolate_lru_page(page, mode, file)) {
  1002. case 0:
  1003. mem_cgroup_lru_del(page);
  1004. list_move(&page->lru, dst);
  1005. nr_taken += hpage_nr_pages(page);
  1006. break;
  1007. case -EBUSY:
  1008. /* else it is being freed elsewhere */
  1009. list_move(&page->lru, src);
  1010. continue;
  1011. default:
  1012. BUG();
  1013. }
  1014. if (!order)
  1015. continue;
  1016. /*
  1017. * Attempt to take all pages in the order aligned region
  1018. * surrounding the tag page. Only take those pages of
  1019. * the same active state as that tag page. We may safely
  1020. * round the target page pfn down to the requested order
  1021. * as the mem_map is guaranteed valid out to MAX_ORDER,
  1022. * where that page is in a different zone we will detect
  1023. * it from its zone id and abort this block scan.
  1024. */
  1025. zone_id = page_zone_id(page);
  1026. page_pfn = page_to_pfn(page);
  1027. pfn = page_pfn & ~((1 << order) - 1);
  1028. end_pfn = pfn + (1 << order);
  1029. for (; pfn < end_pfn; pfn++) {
  1030. struct page *cursor_page;
  1031. /* The target page is in the block, ignore it. */
  1032. if (unlikely(pfn == page_pfn))
  1033. continue;
  1034. /* Avoid holes within the zone. */
  1035. if (unlikely(!pfn_valid_within(pfn)))
  1036. break;
  1037. cursor_page = pfn_to_page(pfn);
  1038. /* Check that we have not crossed a zone boundary. */
  1039. if (unlikely(page_zone_id(cursor_page) != zone_id))
  1040. break;
  1041. /*
  1042. * If we don't have enough swap space, reclaiming of
  1043. * anon page which don't already have a swap slot is
  1044. * pointless.
  1045. */
  1046. if (nr_swap_pages <= 0 && PageSwapBacked(cursor_page) &&
  1047. !PageSwapCache(cursor_page))
  1048. break;
  1049. if (__isolate_lru_page(cursor_page, mode, file) == 0) {
  1050. mem_cgroup_lru_del(cursor_page);
  1051. list_move(&cursor_page->lru, dst);
  1052. nr_taken += hpage_nr_pages(cursor_page);
  1053. nr_lumpy_taken++;
  1054. if (PageDirty(cursor_page))
  1055. nr_lumpy_dirty++;
  1056. scan++;
  1057. } else {
  1058. /*
  1059. * Check if the page is freed already.
  1060. *
  1061. * We can't use page_count() as that
  1062. * requires compound_head and we don't
  1063. * have a pin on the page here. If a
  1064. * page is tail, we may or may not
  1065. * have isolated the head, so assume
  1066. * it's not free, it'd be tricky to
  1067. * track the head status without a
  1068. * page pin.
  1069. */
  1070. if (!PageTail(cursor_page) &&
  1071. !atomic_read(&cursor_page->_count))
  1072. continue;
  1073. break;
  1074. }
  1075. }
  1076. /* If we break out of the loop above, lumpy reclaim failed */
  1077. if (pfn < end_pfn)
  1078. nr_lumpy_failed++;
  1079. }
  1080. *scanned = scan;
  1081. trace_mm_vmscan_lru_isolate(order,
  1082. nr_to_scan, scan,
  1083. nr_taken,
  1084. nr_lumpy_taken, nr_lumpy_dirty, nr_lumpy_failed,
  1085. mode, file);
  1086. return nr_taken;
  1087. }
  1088. static unsigned long isolate_pages(unsigned long nr, struct mem_cgroup_zone *mz,
  1089. struct list_head *dst,
  1090. unsigned long *scanned, int order,
  1091. isolate_mode_t mode, int active, int file)
  1092. {
  1093. struct lruvec *lruvec;
  1094. int lru = LRU_BASE;
  1095. lruvec = mem_cgroup_zone_lruvec(mz->zone, mz->mem_cgroup);
  1096. if (active)
  1097. lru += LRU_ACTIVE;
  1098. if (file)
  1099. lru += LRU_FILE;
  1100. return isolate_lru_pages(nr, &lruvec->lists[lru], dst,
  1101. scanned, order, mode, file);
  1102. }
  1103. /*
  1104. * clear_active_flags() is a helper for shrink_active_list(), clearing
  1105. * any active bits from the pages in the list.
  1106. */
  1107. static unsigned long clear_active_flags(struct list_head *page_list,
  1108. unsigned int *count)
  1109. {
  1110. int nr_active = 0;
  1111. int lru;
  1112. struct page *page;
  1113. list_for_each_entry(page, page_list, lru) {
  1114. int numpages = hpage_nr_pages(page);
  1115. lru = page_lru_base_type(page);
  1116. if (PageActive(page)) {
  1117. lru += LRU_ACTIVE;
  1118. ClearPageActive(page);
  1119. nr_active += numpages;
  1120. }
  1121. if (count)
  1122. count[lru] += numpages;
  1123. }
  1124. return nr_active;
  1125. }
  1126. /**
  1127. * isolate_lru_page - tries to isolate a page from its LRU list
  1128. * @page: page to isolate from its LRU list
  1129. *
  1130. * Isolates a @page from an LRU list, clears PageLRU and adjusts the
  1131. * vmstat statistic corresponding to whatever LRU list the page was on.
  1132. *
  1133. * Returns 0 if the page was removed from an LRU list.
  1134. * Returns -EBUSY if the page was not on an LRU list.
  1135. *
  1136. * The returned page will have PageLRU() cleared. If it was found on
  1137. * the active list, it will have PageActive set. If it was found on
  1138. * the unevictable list, it will have the PageUnevictable bit set. That flag
  1139. * may need to be cleared by the caller before letting the page go.
  1140. *
  1141. * The vmstat statistic corresponding to the list on which the page was
  1142. * found will be decremented.
  1143. *
  1144. * Restrictions:
  1145. * (1) Must be called with an elevated refcount on the page. This is a
  1146. * fundamentnal difference from isolate_lru_pages (which is called
  1147. * without a stable reference).
  1148. * (2) the lru_lock must not be held.
  1149. * (3) interrupts must be enabled.
  1150. */
  1151. int isolate_lru_page(struct page *page)
  1152. {
  1153. int ret = -EBUSY;
  1154. VM_BUG_ON(!page_count(page));
  1155. if (PageLRU(page)) {
  1156. struct zone *zone = page_zone(page);
  1157. spin_lock_irq(&zone->lru_lock);
  1158. if (PageLRU(page)) {
  1159. int lru = page_lru(page);
  1160. ret = 0;
  1161. get_page(page);
  1162. ClearPageLRU(page);
  1163. del_page_from_lru_list(zone, page, lru);
  1164. }
  1165. spin_unlock_irq(&zone->lru_lock);
  1166. }
  1167. return ret;
  1168. }
  1169. /*
  1170. * Are there way too many processes in the direct reclaim path already?
  1171. */
  1172. static int too_many_isolated(struct zone *zone, int file,
  1173. struct scan_control *sc)
  1174. {
  1175. unsigned long inactive, isolated;
  1176. if (current_is_kswapd())
  1177. return 0;
  1178. if (!global_reclaim(sc))
  1179. return 0;
  1180. if (file) {
  1181. inactive = zone_page_state(zone, NR_INACTIVE_FILE);
  1182. isolated = zone_page_state(zone, NR_ISOLATED_FILE);
  1183. } else {
  1184. inactive = zone_page_state(zone, NR_INACTIVE_ANON);
  1185. isolated = zone_page_state(zone, NR_ISOLATED_ANON);
  1186. }
  1187. return isolated > inactive;
  1188. }
  1189. /*
  1190. * TODO: Try merging with migrations version of putback_lru_pages
  1191. */
  1192. static noinline_for_stack void
  1193. putback_lru_pages(struct mem_cgroup_zone *mz, struct scan_control *sc,
  1194. unsigned long nr_anon, unsigned long nr_file,
  1195. struct list_head *page_list)
  1196. {
  1197. struct page *page;
  1198. struct pagevec pvec;
  1199. struct zone *zone = mz->zone;
  1200. struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(mz);
  1201. pagevec_init(&pvec, 1);
  1202. /*
  1203. * Put back any unfreeable pages.
  1204. */
  1205. spin_lock(&zone->lru_lock);
  1206. while (!list_empty(page_list)) {
  1207. int lru;
  1208. page = lru_to_page(page_list);
  1209. VM_BUG_ON(PageLRU(page));
  1210. list_del(&page->lru);
  1211. if (unlikely(!page_evictable(page, NULL))) {
  1212. spin_unlock_irq(&zone->lru_lock);
  1213. putback_lru_page(page);
  1214. spin_lock_irq(&zone->lru_lock);
  1215. continue;
  1216. }
  1217. SetPageLRU(page);
  1218. lru = page_lru(page);
  1219. add_page_to_lru_list(zone, page, lru);
  1220. if (is_active_lru(lru)) {
  1221. int file = is_file_lru(lru);
  1222. int numpages = hpage_nr_pages(page);
  1223. reclaim_stat->recent_rotated[file] += numpages;
  1224. }
  1225. if (!pagevec_add(&pvec, page)) {
  1226. spin_unlock_irq(&zone->lru_lock);
  1227. __pagevec_release(&pvec);
  1228. spin_lock_irq(&zone->lru_lock);
  1229. }
  1230. }
  1231. __mod_zone_page_state(zone, NR_ISOLATED_ANON, -nr_anon);
  1232. __mod_zone_page_state(zone, NR_ISOLATED_FILE, -nr_file);
  1233. spin_unlock_irq(&zone->lru_lock);
  1234. pagevec_release(&pvec);
  1235. }
  1236. static noinline_for_stack void
  1237. update_isolated_counts(struct mem_cgroup_zone *mz,
  1238. struct scan_control *sc,
  1239. unsigned long *nr_anon,
  1240. unsigned long *nr_file,
  1241. struct list_head *isolated_list)
  1242. {
  1243. unsigned long nr_active;
  1244. struct zone *zone = mz->zone;
  1245. unsigned int count[NR_LRU_LISTS] = { 0, };
  1246. struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(mz);
  1247. nr_active = clear_active_flags(isolated_list, count);
  1248. __count_vm_events(PGDEACTIVATE, nr_active);
  1249. __mod_zone_page_state(zone, NR_ACTIVE_FILE,
  1250. -count[LRU_ACTIVE_FILE]);
  1251. __mod_zone_page_state(zone, NR_INACTIVE_FILE,
  1252. -count[LRU_INACTIVE_FILE]);
  1253. __mod_zone_page_state(zone, NR_ACTIVE_ANON,
  1254. -count[LRU_ACTIVE_ANON]);
  1255. __mod_zone_page_state(zone, NR_INACTIVE_ANON,
  1256. -count[LRU_INACTIVE_ANON]);
  1257. *nr_anon = count[LRU_ACTIVE_ANON] + count[LRU_INACTIVE_ANON];
  1258. *nr_file = count[LRU_ACTIVE_FILE] + count[LRU_INACTIVE_FILE];
  1259. __mod_zone_page_state(zone, NR_ISOLATED_ANON, *nr_anon);
  1260. __mod_zone_page_state(zone, NR_ISOLATED_FILE, *nr_file);
  1261. reclaim_stat->recent_scanned[0] += *nr_anon;
  1262. reclaim_stat->recent_scanned[1] += *nr_file;
  1263. }
  1264. /*
  1265. * Returns true if a direct reclaim should wait on pages under writeback.
  1266. *
  1267. * If we are direct reclaiming for contiguous pages and we do not reclaim
  1268. * everything in the list, try again and wait for writeback IO to complete.
  1269. * This will stall high-order allocations noticeably. Only do that when really
  1270. * need to free the pages under high memory pressure.
  1271. */
  1272. static inline bool should_reclaim_stall(unsigned long nr_taken,
  1273. unsigned long nr_freed,
  1274. int priority,
  1275. struct scan_control *sc)
  1276. {
  1277. int lumpy_stall_priority;
  1278. /* kswapd should not stall on sync IO */
  1279. if (current_is_kswapd())
  1280. return false;
  1281. /* Only stall on lumpy reclaim */
  1282. if (sc->reclaim_mode & RECLAIM_MODE_SINGLE)
  1283. return false;
  1284. /* If we have reclaimed everything on the isolated list, no stall */
  1285. if (nr_freed == nr_taken)
  1286. return false;
  1287. /*
  1288. * For high-order allocations, there are two stall thresholds.
  1289. * High-cost allocations stall immediately where as lower
  1290. * order allocations such as stacks require the scanning
  1291. * priority to be much higher before stalling.
  1292. */
  1293. if (sc->order > PAGE_ALLOC_COSTLY_ORDER)
  1294. lumpy_stall_priority = DEF_PRIORITY;
  1295. else
  1296. lumpy_stall_priority = DEF_PRIORITY / 3;
  1297. return priority <= lumpy_stall_priority;
  1298. }
  1299. /*
  1300. * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
  1301. * of reclaimed pages
  1302. */
  1303. static noinline_for_stack unsigned long
  1304. shrink_inactive_list(unsigned long nr_to_scan, struct mem_cgroup_zone *mz,
  1305. struct scan_control *sc, int priority, int file)
  1306. {
  1307. LIST_HEAD(page_list);
  1308. unsigned long nr_scanned;
  1309. unsigned long nr_reclaimed = 0;
  1310. unsigned long nr_taken;
  1311. unsigned long nr_anon;
  1312. unsigned long nr_file;
  1313. unsigned long nr_dirty = 0;
  1314. unsigned long nr_writeback = 0;
  1315. isolate_mode_t reclaim_mode = ISOLATE_INACTIVE;
  1316. struct zone *zone = mz->zone;
  1317. while (unlikely(too_many_isolated(zone, file, sc))) {
  1318. congestion_wait(BLK_RW_ASYNC, HZ/10);
  1319. /* We are about to die and free our memory. Return now. */
  1320. if (fatal_signal_pending(current))
  1321. return SWAP_CLUSTER_MAX;
  1322. }
  1323. set_reclaim_mode(priority, sc, false);
  1324. if (sc->reclaim_mode & RECLAIM_MODE_LUMPYRECLAIM)
  1325. reclaim_mode |= ISOLATE_ACTIVE;
  1326. lru_add_drain();
  1327. if (!sc->may_unmap)
  1328. reclaim_mode |= ISOLATE_UNMAPPED;
  1329. if (!sc->may_writepage)
  1330. reclaim_mode |= ISOLATE_CLEAN;
  1331. spin_lock_irq(&zone->lru_lock);
  1332. nr_taken = isolate_pages(nr_to_scan, mz, &page_list,
  1333. &nr_scanned, sc->order,
  1334. reclaim_mode, 0, file);
  1335. if (global_reclaim(sc)) {
  1336. zone->pages_scanned += nr_scanned;
  1337. if (current_is_kswapd())
  1338. __count_zone_vm_events(PGSCAN_KSWAPD, zone,
  1339. nr_scanned);
  1340. else
  1341. __count_zone_vm_events(PGSCAN_DIRECT, zone,
  1342. nr_scanned);
  1343. }
  1344. if (nr_taken == 0) {
  1345. spin_unlock_irq(&zone->lru_lock);
  1346. return 0;
  1347. }
  1348. update_isolated_counts(mz, sc, &nr_anon, &nr_file, &page_list);
  1349. spin_unlock_irq(&zone->lru_lock);
  1350. nr_reclaimed = shrink_page_list(&page_list, mz, sc, priority,
  1351. &nr_dirty, &nr_writeback);
  1352. /* Check if we should syncronously wait for writeback */
  1353. if (should_reclaim_stall(nr_taken, nr_reclaimed, priority, sc)) {
  1354. set_reclaim_mode(priority, sc, true);
  1355. nr_reclaimed += shrink_page_list(&page_list, mz, sc,
  1356. priority, &nr_dirty, &nr_writeback);
  1357. }
  1358. local_irq_disable();
  1359. if (current_is_kswapd())
  1360. __count_vm_events(KSWAPD_STEAL, nr_reclaimed);
  1361. __count_zone_vm_events(PGSTEAL, zone, nr_reclaimed);
  1362. putback_lru_pages(mz, sc, nr_anon, nr_file, &page_list);
  1363. /*
  1364. * If reclaim is isolating dirty pages under writeback, it implies
  1365. * that the long-lived page allocation rate is exceeding the page
  1366. * laundering rate. Either the global limits are not being effective
  1367. * at throttling processes due to the page distribution throughout
  1368. * zones or there is heavy usage of a slow backing device. The
  1369. * only option is to throttle from reclaim context which is not ideal
  1370. * as there is no guarantee the dirtying process is throttled in the
  1371. * same way balance_dirty_pages() manages.
  1372. *
  1373. * This scales the number of dirty pages that must be under writeback
  1374. * before throttling depending on priority. It is a simple backoff
  1375. * function that has the most effect in the range DEF_PRIORITY to
  1376. * DEF_PRIORITY-2 which is the priority reclaim is considered to be
  1377. * in trouble and reclaim is considered to be in trouble.
  1378. *
  1379. * DEF_PRIORITY 100% isolated pages must be PageWriteback to throttle
  1380. * DEF_PRIORITY-1 50% must be PageWriteback
  1381. * DEF_PRIORITY-2 25% must be PageWriteback, kswapd in trouble
  1382. * ...
  1383. * DEF_PRIORITY-6 For SWAP_CLUSTER_MAX isolated pages, throttle if any
  1384. * isolated page is PageWriteback
  1385. */
  1386. if (nr_writeback && nr_writeback >= (nr_taken >> (DEF_PRIORITY-priority)))
  1387. wait_iff_congested(zone, BLK_RW_ASYNC, HZ/10);
  1388. trace_mm_vmscan_lru_shrink_inactive(zone->zone_pgdat->node_id,
  1389. zone_idx(zone),
  1390. nr_scanned, nr_reclaimed,
  1391. priority,
  1392. trace_shrink_flags(file, sc->reclaim_mode));
  1393. return nr_reclaimed;
  1394. }
  1395. /*
  1396. * This moves pages from the active list to the inactive list.
  1397. *
  1398. * We move them the other way if the page is referenced by one or more
  1399. * processes, from rmap.
  1400. *
  1401. * If the pages are mostly unmapped, the processing is fast and it is
  1402. * appropriate to hold zone->lru_lock across the whole operation. But if
  1403. * the pages are mapped, the processing is slow (page_referenced()) so we
  1404. * should drop zone->lru_lock around each page. It's impossible to balance
  1405. * this, so instead we remove the pages from the LRU while processing them.
  1406. * It is safe to rely on PG_active against the non-LRU pages in here because
  1407. * nobody will play with that bit on a non-LRU page.
  1408. *
  1409. * The downside is that we have to touch page->_count against each page.
  1410. * But we had to alter page->flags anyway.
  1411. */
  1412. static void move_active_pages_to_lru(struct zone *zone,
  1413. struct list_head *list,
  1414. enum lru_list lru)
  1415. {
  1416. unsigned long pgmoved = 0;
  1417. struct pagevec pvec;
  1418. struct page *page;
  1419. pagevec_init(&pvec, 1);
  1420. while (!list_empty(list)) {
  1421. struct lruvec *lruvec;
  1422. page = lru_to_page(list);
  1423. VM_BUG_ON(PageLRU(page));
  1424. SetPageLRU(page);
  1425. lruvec = mem_cgroup_lru_add_list(zone, page, lru);
  1426. list_move(&page->lru, &lruvec->lists[lru]);
  1427. pgmoved += hpage_nr_pages(page);
  1428. if (!pagevec_add(&pvec, page) || list_empty(list)) {
  1429. spin_unlock_irq(&zone->lru_lock);
  1430. if (buffer_heads_over_limit)
  1431. pagevec_strip(&pvec);
  1432. __pagevec_release(&pvec);
  1433. spin_lock_irq(&zone->lru_lock);
  1434. }
  1435. }
  1436. __mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved);
  1437. if (!is_active_lru(lru))
  1438. __count_vm_events(PGDEACTIVATE, pgmoved);
  1439. }
  1440. static void shrink_active_list(unsigned long nr_pages,
  1441. struct mem_cgroup_zone *mz,
  1442. struct scan_control *sc,
  1443. int priority, int file)
  1444. {
  1445. unsigned long nr_taken;
  1446. unsigned long pgscanned;
  1447. unsigned long vm_flags;
  1448. LIST_HEAD(l_hold); /* The pages which were snipped off */
  1449. LIST_HEAD(l_active);
  1450. LIST_HEAD(l_inactive);
  1451. struct page *page;
  1452. struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(mz);
  1453. unsigned long nr_rotated = 0;
  1454. isolate_mode_t reclaim_mode = ISOLATE_ACTIVE;
  1455. struct zone *zone = mz->zone;
  1456. lru_add_drain();
  1457. if (!sc->may_unmap)
  1458. reclaim_mode |= ISOLATE_UNMAPPED;
  1459. if (!sc->may_writepage)
  1460. reclaim_mode |= ISOLATE_CLEAN;
  1461. spin_lock_irq(&zone->lru_lock);
  1462. nr_taken = isolate_pages(nr_pages, mz, &l_hold,
  1463. &pgscanned, sc->order,
  1464. reclaim_mode, 1, file);
  1465. if (global_reclaim(sc))
  1466. zone->pages_scanned += pgscanned;
  1467. reclaim_stat->recent_scanned[file] += nr_taken;
  1468. __count_zone_vm_events(PGREFILL, zone, pgscanned);
  1469. if (file)
  1470. __mod_zone_page_state(zone, NR_ACTIVE_FILE, -nr_taken);
  1471. else
  1472. __mod_zone_page_state(zone, NR_ACTIVE_ANON, -nr_taken);
  1473. __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken);
  1474. spin_unlock_irq(&zone->lru_lock);
  1475. while (!list_empty(&l_hold)) {
  1476. cond_resched();
  1477. page = lru_to_page(&l_hold);
  1478. list_del(&page->lru);
  1479. if (unlikely(!page_evictable(page, NULL))) {
  1480. putback_lru_page(page);
  1481. continue;
  1482. }
  1483. if (page_referenced(page, 0, mz->mem_cgroup, &vm_flags)) {
  1484. nr_rotated += hpage_nr_pages(page);
  1485. /*
  1486. * Identify referenced, file-backed active pages and
  1487. * give them one more trip around the active list. So
  1488. * that executable code get better chances to stay in
  1489. * memory under moderate memory pressure. Anon pages
  1490. * are not likely to be evicted by use-once streaming
  1491. * IO, plus JVM can create lots of anon VM_EXEC pages,
  1492. * so we ignore them here.
  1493. */
  1494. if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) {
  1495. list_add(&page->lru, &l_active);
  1496. continue;
  1497. }
  1498. }
  1499. ClearPageActive(page); /* we are de-activating */
  1500. list_add(&page->lru, &l_inactive);
  1501. }
  1502. /*
  1503. * Move pages back to the lru list.
  1504. */
  1505. spin_lock_irq(&zone->lru_lock);
  1506. /*
  1507. * Count referenced pages from currently used mappings as rotated,
  1508. * even though only some of them are actually re-activated. This
  1509. * helps balance scan pressure between file and anonymous pages in
  1510. * get_scan_ratio.
  1511. */
  1512. reclaim_stat->recent_rotated[file] += nr_rotated;
  1513. move_active_pages_to_lru(zone, &l_active,
  1514. LRU_ACTIVE + file * LRU_FILE);
  1515. move_active_pages_to_lru(zone, &l_inactive,
  1516. LRU_BASE + file * LRU_FILE);
  1517. __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken);
  1518. spin_unlock_irq(&zone->lru_lock);
  1519. }
  1520. #ifdef CONFIG_SWAP
  1521. static int inactive_anon_is_low_global(struct zone *zone)
  1522. {
  1523. unsigned long active, inactive;
  1524. active = zone_page_state(zone, NR_ACTIVE_ANON);
  1525. inactive = zone_page_state(zone, NR_INACTIVE_ANON);
  1526. if (inactive * zone->inactive_ratio < active)
  1527. return 1;
  1528. return 0;
  1529. }
  1530. /**
  1531. * inactive_anon_is_low - check if anonymous pages need to be deactivated
  1532. * @zone: zone to check
  1533. * @sc: scan control of this context
  1534. *
  1535. * Returns true if the zone does not have enough inactive anon pages,
  1536. * meaning some active anon pages need to be deactivated.
  1537. */
  1538. static int inactive_anon_is_low(struct mem_cgroup_zone *mz)
  1539. {
  1540. /*
  1541. * If we don't have swap space, anonymous page deactivation
  1542. * is pointless.
  1543. */
  1544. if (!total_swap_pages)
  1545. return 0;
  1546. if (!scanning_global_lru(mz))
  1547. return mem_cgroup_inactive_anon_is_low(mz->mem_cgroup,
  1548. mz->zone);
  1549. return inactive_anon_is_low_global(mz->zone);
  1550. }
  1551. #else
  1552. static inline int inactive_anon_is_low(struct mem_cgroup_zone *mz)
  1553. {
  1554. return 0;
  1555. }
  1556. #endif
  1557. static int inactive_file_is_low_global(struct zone *zone)
  1558. {
  1559. unsigned long active, inactive;
  1560. active = zone_page_state(zone, NR_ACTIVE_FILE);
  1561. inactive = zone_page_state(zone, NR_INACTIVE_FILE);
  1562. return (active > inactive);
  1563. }
  1564. /**
  1565. * inactive_file_is_low - check if file pages need to be deactivated
  1566. * @mz: memory cgroup and zone to check
  1567. *
  1568. * When the system is doing streaming IO, memory pressure here
  1569. * ensures that active file pages get deactivated, until more
  1570. * than half of the file pages are on the inactive list.
  1571. *
  1572. * Once we get to that situation, protect the system's working
  1573. * set from being evicted by disabling active file page aging.
  1574. *
  1575. * This uses a different ratio than the anonymous pages, because
  1576. * the page cache uses a use-once replacement algorithm.
  1577. */
  1578. static int inactive_file_is_low(struct mem_cgroup_zone *mz)
  1579. {
  1580. if (!scanning_global_lru(mz))
  1581. return mem_cgroup_inactive_file_is_low(mz->mem_cgroup,
  1582. mz->zone);
  1583. return inactive_file_is_low_global(mz->zone);
  1584. }
  1585. static int inactive_list_is_low(struct mem_cgroup_zone *mz, int file)
  1586. {
  1587. if (file)
  1588. return inactive_file_is_low(mz);
  1589. else
  1590. return inactive_anon_is_low(mz);
  1591. }
  1592. static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
  1593. struct mem_cgroup_zone *mz,
  1594. struct scan_control *sc, int priority)
  1595. {
  1596. int file = is_file_lru(lru);
  1597. if (is_active_lru(lru)) {
  1598. if (inactive_list_is_low(mz, file))
  1599. shrink_active_list(nr_to_scan, mz, sc, priority, file);
  1600. return 0;
  1601. }
  1602. return shrink_inactive_list(nr_to_scan, mz, sc, priority, file);
  1603. }
  1604. static int vmscan_swappiness(struct mem_cgroup_zone *mz,
  1605. struct scan_control *sc)
  1606. {
  1607. if (global_reclaim(sc))
  1608. return vm_swappiness;
  1609. return mem_cgroup_swappiness(mz->mem_cgroup);
  1610. }
  1611. /*
  1612. * Determine how aggressively the anon and file LRU lists should be
  1613. * scanned. The relative value of each set of LRU lists is determined
  1614. * by looking at the fraction of the pages scanned we did rotate back
  1615. * onto the active list instead of evict.
  1616. *
  1617. * nr[0] = anon pages to scan; nr[1] = file pages to scan
  1618. */
  1619. static void get_scan_count(struct mem_cgroup_zone *mz, struct scan_control *sc,
  1620. unsigned long *nr, int priority)
  1621. {
  1622. unsigned long anon, file, free;
  1623. unsigned long anon_prio, file_prio;
  1624. unsigned long ap, fp;
  1625. struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(mz);
  1626. u64 fraction[2], denominator;
  1627. enum lru_list l;
  1628. int noswap = 0;
  1629. bool force_scan = false;
  1630. /*
  1631. * If the zone or memcg is small, nr[l] can be 0. This
  1632. * results in no scanning on this priority and a potential
  1633. * priority drop. Global direct reclaim can go to the next
  1634. * zone and tends to have no problems. Global kswapd is for
  1635. * zone balancing and it needs to scan a minimum amount. When
  1636. * reclaiming for a memcg, a priority drop can cause high
  1637. * latencies, so it's better to scan a minimum amount there as
  1638. * well.
  1639. */
  1640. if (current_is_kswapd() && mz->zone->all_unreclaimable)
  1641. force_scan = true;
  1642. if (!global_reclaim(sc))
  1643. force_scan = true;
  1644. /* If we have no swap space, do not bother scanning anon pages. */
  1645. if (!sc->may_swap || (nr_swap_pages <= 0)) {
  1646. noswap = 1;
  1647. fraction[0] = 0;
  1648. fraction[1] = 1;
  1649. denominator = 1;
  1650. goto out;
  1651. }
  1652. anon = zone_nr_lru_pages(mz, LRU_ACTIVE_ANON) +
  1653. zone_nr_lru_pages(mz, LRU_INACTIVE_ANON);
  1654. file = zone_nr_lru_pages(mz, LRU_ACTIVE_FILE) +
  1655. zone_nr_lru_pages(mz, LRU_INACTIVE_FILE);
  1656. if (global_reclaim(sc)) {
  1657. free = zone_page_state(mz->zone, NR_FREE_PAGES);
  1658. /* If we have very few page cache pages,
  1659. force-scan anon pages. */
  1660. if (unlikely(file + free <= high_wmark_pages(mz->zone))) {
  1661. fraction[0] = 1;
  1662. fraction[1] = 0;
  1663. denominator = 1;
  1664. goto out;
  1665. }
  1666. }
  1667. /*
  1668. * With swappiness at 100, anonymous and file have the same priority.
  1669. * This scanning priority is essentially the inverse of IO cost.
  1670. */
  1671. anon_prio = vmscan_swappiness(mz, sc);
  1672. file_prio = 200 - vmscan_swappiness(mz, sc);
  1673. /*
  1674. * OK, so we have swap space and a fair amount of page cache
  1675. * pages. We use the recently rotated / recently scanned
  1676. * ratios to determine how valuable each cache is.
  1677. *
  1678. * Because workloads change over time (and to avoid overflow)
  1679. * we keep these statistics as a floating average, which ends
  1680. * up weighing recent references more than old ones.
  1681. *
  1682. * anon in [0], file in [1]
  1683. */
  1684. spin_lock_irq(&mz->zone->lru_lock);
  1685. if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) {
  1686. reclaim_stat->recent_scanned[0] /= 2;
  1687. reclaim_stat->recent_rotated[0] /= 2;
  1688. }
  1689. if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) {
  1690. reclaim_stat->recent_scanned[1] /= 2;
  1691. reclaim_stat->recent_rotated[1] /= 2;
  1692. }
  1693. /*
  1694. * The amount of pressure on anon vs file pages is inversely
  1695. * proportional to the fraction of recently scanned pages on
  1696. * each list that were recently referenced and in active use.
  1697. */
  1698. ap = (anon_prio + 1) * (reclaim_stat->recent_scanned[0] + 1);
  1699. ap /= reclaim_stat->recent_rotated[0] + 1;
  1700. fp = (file_prio + 1) * (reclaim_stat->recent_scanned[1] + 1);
  1701. fp /= reclaim_stat->recent_rotated[1] + 1;
  1702. spin_unlock_irq(&mz->zone->lru_lock);
  1703. fraction[0] = ap;
  1704. fraction[1] = fp;
  1705. denominator = ap + fp + 1;
  1706. out:
  1707. for_each_evictable_lru(l) {
  1708. int file = is_file_lru(l);
  1709. unsigned long scan;
  1710. scan = zone_nr_lru_pages(mz, l);
  1711. if (priority || noswap) {
  1712. scan >>= priority;
  1713. if (!scan && force_scan)
  1714. scan = SWAP_CLUSTER_MAX;
  1715. scan = div64_u64(scan * fraction[file], denominator);
  1716. }
  1717. nr[l] = scan;
  1718. }
  1719. }
  1720. /*
  1721. * Reclaim/compaction depends on a number of pages being freed. To avoid
  1722. * disruption to the system, a small number of order-0 pages continue to be
  1723. * rotated and reclaimed in the normal fashion. However, by the time we get
  1724. * back to the allocator and call try_to_compact_zone(), we ensure that
  1725. * there are enough free pages for it to be likely successful
  1726. */
  1727. static inline bool should_continue_reclaim(struct mem_cgroup_zone *mz,
  1728. unsigned long nr_reclaimed,
  1729. unsigned long nr_scanned,
  1730. struct scan_control *sc)
  1731. {
  1732. unsigned long pages_for_compaction;
  1733. unsigned long inactive_lru_pages;
  1734. /* If not in reclaim/compaction mode, stop */
  1735. if (!(sc->reclaim_mode & RECLAIM_MODE_COMPACTION))
  1736. return false;
  1737. /* Consider stopping depending on scan and reclaim activity */
  1738. if (sc->gfp_mask & __GFP_REPEAT) {
  1739. /*
  1740. * For __GFP_REPEAT allocations, stop reclaiming if the
  1741. * full LRU list has been scanned and we are still failing
  1742. * to reclaim pages. This full LRU scan is potentially
  1743. * expensive but a __GFP_REPEAT caller really wants to succeed
  1744. */
  1745. if (!nr_reclaimed && !nr_scanned)
  1746. return false;
  1747. } else {
  1748. /*
  1749. * For non-__GFP_REPEAT allocations which can presumably
  1750. * fail without consequence, stop if we failed to reclaim
  1751. * any pages from the last SWAP_CLUSTER_MAX number of
  1752. * pages that were scanned. This will return to the
  1753. * caller faster at the risk reclaim/compaction and
  1754. * the resulting allocation attempt fails
  1755. */
  1756. if (!nr_reclaimed)
  1757. return false;
  1758. }
  1759. /*
  1760. * If we have not reclaimed enough pages for compaction and the
  1761. * inactive lists are large enough, continue reclaiming
  1762. */
  1763. pages_for_compaction = (2UL << sc->order);
  1764. inactive_lru_pages = zone_nr_lru_pages(mz, LRU_INACTIVE_FILE);
  1765. if (nr_swap_pages > 0)
  1766. inactive_lru_pages += zone_nr_lru_pages(mz, LRU_INACTIVE_ANON);
  1767. if (sc->nr_reclaimed < pages_for_compaction &&
  1768. inactive_lru_pages > pages_for_compaction)
  1769. return true;
  1770. /* If compaction would go ahead or the allocation would succeed, stop */
  1771. switch (compaction_suitable(mz->zone, sc->order)) {
  1772. case COMPACT_PARTIAL:
  1773. case COMPACT_CONTINUE:
  1774. return false;
  1775. default:
  1776. return true;
  1777. }
  1778. }
  1779. /*
  1780. * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
  1781. */
  1782. static void shrink_mem_cgroup_zone(int priority, struct mem_cgroup_zone *mz,
  1783. struct scan_control *sc)
  1784. {
  1785. unsigned long nr[NR_LRU_LISTS];
  1786. unsigned long nr_to_scan;
  1787. enum lru_list l;
  1788. unsigned long nr_reclaimed, nr_scanned;
  1789. unsigned long nr_to_reclaim = sc->nr_to_reclaim;
  1790. struct blk_plug plug;
  1791. restart:
  1792. nr_reclaimed = 0;
  1793. nr_scanned = sc->nr_scanned;
  1794. get_scan_count(mz, sc, nr, priority);
  1795. blk_start_plug(&plug);
  1796. while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] ||
  1797. nr[LRU_INACTIVE_FILE]) {
  1798. for_each_evictable_lru(l) {
  1799. if (nr[l]) {
  1800. nr_to_scan = min_t(unsigned long,
  1801. nr[l], SWAP_CLUSTER_MAX);
  1802. nr[l] -= nr_to_scan;
  1803. nr_reclaimed += shrink_list(l, nr_to_scan,
  1804. mz, sc, priority);
  1805. }
  1806. }
  1807. /*
  1808. * On large memory systems, scan >> priority can become
  1809. * really large. This is fine for the starting priority;
  1810. * we want to put equal scanning pressure on each zone.
  1811. * However, if the VM has a harder time of freeing pages,
  1812. * with multiple processes reclaiming pages, the total
  1813. * freeing target can get unreasonably large.
  1814. */
  1815. if (nr_reclaimed >= nr_to_reclaim && priority < DEF_PRIORITY)
  1816. break;
  1817. }
  1818. blk_finish_plug(&plug);
  1819. sc->nr_reclaimed += nr_reclaimed;
  1820. /*
  1821. * Even if we did not try to evict anon pages at all, we want to
  1822. * rebalance the anon lru active/inactive ratio.
  1823. */
  1824. if (inactive_anon_is_low(mz))
  1825. shrink_active_list(SWAP_CLUSTER_MAX, mz, sc, priority, 0);
  1826. /* reclaim/compaction might need reclaim to continue */
  1827. if (should_continue_reclaim(mz, nr_reclaimed,
  1828. sc->nr_scanned - nr_scanned, sc))
  1829. goto restart;
  1830. throttle_vm_writeout(sc->gfp_mask);
  1831. }
  1832. static void shrink_zone(int priority, struct zone *zone,
  1833. struct scan_control *sc)
  1834. {
  1835. struct mem_cgroup *root = sc->target_mem_cgroup;
  1836. struct mem_cgroup_reclaim_cookie reclaim = {
  1837. .zone = zone,
  1838. .priority = priority,
  1839. };
  1840. struct mem_cgroup *memcg;
  1841. memcg = mem_cgroup_iter(root, NULL, &reclaim);
  1842. do {
  1843. struct mem_cgroup_zone mz = {
  1844. .mem_cgroup = memcg,
  1845. .zone = zone,
  1846. };
  1847. shrink_mem_cgroup_zone(priority, &mz, sc);
  1848. /*
  1849. * Limit reclaim has historically picked one memcg and
  1850. * scanned it with decreasing priority levels until
  1851. * nr_to_reclaim had been reclaimed. This priority
  1852. * cycle is thus over after a single memcg.
  1853. *
  1854. * Direct reclaim and kswapd, on the other hand, have
  1855. * to scan all memory cgroups to fulfill the overall
  1856. * scan target for the zone.
  1857. */
  1858. if (!global_reclaim(sc)) {
  1859. mem_cgroup_iter_break(root, memcg);
  1860. break;
  1861. }
  1862. memcg = mem_cgroup_iter(root, memcg, &reclaim);
  1863. } while (memcg);
  1864. }
  1865. /*
  1866. * This is the direct reclaim path, for page-allocating processes. We only
  1867. * try to reclaim pages from zones which will satisfy the caller's allocation
  1868. * request.
  1869. *
  1870. * We reclaim from a zone even if that zone is over high_wmark_pages(zone).
  1871. * Because:
  1872. * a) The caller may be trying to free *extra* pages to satisfy a higher-order
  1873. * allocation or
  1874. * b) The target zone may be at high_wmark_pages(zone) but the lower zones
  1875. * must go *over* high_wmark_pages(zone) to satisfy the `incremental min'
  1876. * zone defense algorithm.
  1877. *
  1878. * If a zone is deemed to be full of pinned pages then just give it a light
  1879. * scan then give up on it.
  1880. *
  1881. * This function returns true if a zone is being reclaimed for a costly
  1882. * high-order allocation and compaction is either ready to begin or deferred.
  1883. * This indicates to the caller that it should retry the allocation or fail.
  1884. */
  1885. static bool shrink_zones(int priority, struct zonelist *zonelist,
  1886. struct scan_control *sc)
  1887. {
  1888. struct zoneref *z;
  1889. struct zone *zone;
  1890. unsigned long nr_soft_reclaimed;
  1891. unsigned long nr_soft_scanned;
  1892. bool should_abort_reclaim = false;
  1893. for_each_zone_zonelist_nodemask(zone, z, zonelist,
  1894. gfp_zone(sc->gfp_mask), sc->nodemask) {
  1895. if (!populated_zone(zone))
  1896. continue;
  1897. /*
  1898. * Take care memory controller reclaiming has small influence
  1899. * to global LRU.
  1900. */
  1901. if (global_reclaim(sc)) {
  1902. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1903. continue;
  1904. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  1905. continue; /* Let kswapd poll it */
  1906. if (COMPACTION_BUILD) {
  1907. /*
  1908. * If we already have plenty of memory free for
  1909. * compaction in this zone, don't free any more.
  1910. * Even though compaction is invoked for any
  1911. * non-zero order, only frequent costly order
  1912. * reclamation is disruptive enough to become a
  1913. * noticable problem, like transparent huge page
  1914. * allocations.
  1915. */
  1916. if (sc->order > PAGE_ALLOC_COSTLY_ORDER &&
  1917. (compaction_suitable(zone, sc->order) ||
  1918. compaction_deferred(zone))) {
  1919. should_abort_reclaim = true;
  1920. continue;
  1921. }
  1922. }
  1923. /*
  1924. * This steals pages from memory cgroups over softlimit
  1925. * and returns the number of reclaimed pages and
  1926. * scanned pages. This works for global memory pressure
  1927. * and balancing, not for a memcg's limit.
  1928. */
  1929. nr_soft_scanned = 0;
  1930. nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone,
  1931. sc->order, sc->gfp_mask,
  1932. &nr_soft_scanned);
  1933. sc->nr_reclaimed += nr_soft_reclaimed;
  1934. sc->nr_scanned += nr_soft_scanned;
  1935. /* need some check for avoid more shrink_zone() */
  1936. }
  1937. shrink_zone(priority, zone, sc);
  1938. }
  1939. return should_abort_reclaim;
  1940. }
  1941. static bool zone_reclaimable(struct zone *zone)
  1942. {
  1943. return zone->pages_scanned < zone_reclaimable_pages(zone) * 6;
  1944. }
  1945. /* All zones in zonelist are unreclaimable? */
  1946. static bool all_unreclaimable(struct zonelist *zonelist,
  1947. struct scan_control *sc)
  1948. {
  1949. struct zoneref *z;
  1950. struct zone *zone;
  1951. for_each_zone_zonelist_nodemask(zone, z, zonelist,
  1952. gfp_zone(sc->gfp_mask), sc->nodemask) {
  1953. if (!populated_zone(zone))
  1954. continue;
  1955. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1956. continue;
  1957. if (!zone->all_unreclaimable)
  1958. return false;
  1959. }
  1960. return true;
  1961. }
  1962. /*
  1963. * This is the main entry point to direct page reclaim.
  1964. *
  1965. * If a full scan of the inactive list fails to free enough memory then we
  1966. * are "out of memory" and something needs to be killed.
  1967. *
  1968. * If the caller is !__GFP_FS then the probability of a failure is reasonably
  1969. * high - the zone may be full of dirty or under-writeback pages, which this
  1970. * caller can't do much about. We kick the writeback threads and take explicit
  1971. * naps in the hope that some of these pages can be written. But if the
  1972. * allocating task holds filesystem locks which prevent writeout this might not
  1973. * work, and the allocation attempt will fail.
  1974. *
  1975. * returns: 0, if no pages reclaimed
  1976. * else, the number of pages reclaimed
  1977. */
  1978. static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
  1979. struct scan_control *sc,
  1980. struct shrink_control *shrink)
  1981. {
  1982. int priority;
  1983. unsigned long total_scanned = 0;
  1984. struct reclaim_state *reclaim_state = current->reclaim_state;
  1985. struct zoneref *z;
  1986. struct zone *zone;
  1987. unsigned long writeback_threshold;
  1988. get_mems_allowed();
  1989. delayacct_freepages_start();
  1990. if (global_reclaim(sc))
  1991. count_vm_event(ALLOCSTALL);
  1992. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  1993. sc->nr_scanned = 0;
  1994. if (!priority)
  1995. disable_swap_token(sc->target_mem_cgroup);
  1996. if (shrink_zones(priority, zonelist, sc))
  1997. break;
  1998. /*
  1999. * Don't shrink slabs when reclaiming memory from
  2000. * over limit cgroups
  2001. */
  2002. if (global_reclaim(sc)) {
  2003. unsigned long lru_pages = 0;
  2004. for_each_zone_zonelist(zone, z, zonelist,
  2005. gfp_zone(sc->gfp_mask)) {
  2006. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  2007. continue;
  2008. lru_pages += zone_reclaimable_pages(zone);
  2009. }
  2010. shrink_slab(shrink, sc->nr_scanned, lru_pages);
  2011. if (reclaim_state) {
  2012. sc->nr_reclaimed += reclaim_state->reclaimed_slab;
  2013. reclaim_state->reclaimed_slab = 0;
  2014. }
  2015. }
  2016. total_scanned += sc->nr_scanned;
  2017. if (sc->nr_reclaimed >= sc->nr_to_reclaim)
  2018. goto out;
  2019. /*
  2020. * Try to write back as many pages as we just scanned. This
  2021. * tends to cause slow streaming writers to write data to the
  2022. * disk smoothly, at the dirtying rate, which is nice. But
  2023. * that's undesirable in laptop mode, where we *want* lumpy
  2024. * writeout. So in laptop mode, write out the whole world.
  2025. */
  2026. writeback_threshold = sc->nr_to_reclaim + sc->nr_to_reclaim / 2;
  2027. if (total_scanned > writeback_threshold) {
  2028. wakeup_flusher_threads(laptop_mode ? 0 : total_scanned,
  2029. WB_REASON_TRY_TO_FREE_PAGES);
  2030. sc->may_writepage = 1;
  2031. }
  2032. /* Take a nap, wait for some writeback to complete */
  2033. if (!sc->hibernation_mode && sc->nr_scanned &&
  2034. priority < DEF_PRIORITY - 2) {
  2035. struct zone *preferred_zone;
  2036. first_zones_zonelist(zonelist, gfp_zone(sc->gfp_mask),
  2037. &cpuset_current_mems_allowed,
  2038. &preferred_zone);
  2039. wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/10);
  2040. }
  2041. }
  2042. out:
  2043. delayacct_freepages_end();
  2044. put_mems_allowed();
  2045. if (sc->nr_reclaimed)
  2046. return sc->nr_reclaimed;
  2047. /*
  2048. * As hibernation is going on, kswapd is freezed so that it can't mark
  2049. * the zone into all_unreclaimable. Thus bypassing all_unreclaimable
  2050. * check.
  2051. */
  2052. if (oom_killer_disabled)
  2053. return 0;
  2054. /* top priority shrink_zones still had more to do? don't OOM, then */
  2055. if (global_reclaim(sc) && !all_unreclaimable(zonelist, sc))
  2056. return 1;
  2057. return 0;
  2058. }
  2059. unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
  2060. gfp_t gfp_mask, nodemask_t *nodemask)
  2061. {
  2062. unsigned long nr_reclaimed;
  2063. struct scan_control sc = {
  2064. .gfp_mask = gfp_mask,
  2065. .may_writepage = !laptop_mode,
  2066. .nr_to_reclaim = SWAP_CLUSTER_MAX,
  2067. .may_unmap = 1,
  2068. .may_swap = 1,
  2069. .order = order,
  2070. .target_mem_cgroup = NULL,
  2071. .nodemask = nodemask,
  2072. };
  2073. struct shrink_control shrink = {
  2074. .gfp_mask = sc.gfp_mask,
  2075. };
  2076. trace_mm_vmscan_direct_reclaim_begin(order,
  2077. sc.may_writepage,
  2078. gfp_mask);
  2079. nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink);
  2080. trace_mm_vmscan_direct_reclaim_end(nr_reclaimed);
  2081. return nr_reclaimed;
  2082. }
  2083. #ifdef CONFIG_CGROUP_MEM_RES_CTLR
  2084. unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup *memcg,
  2085. gfp_t gfp_mask, bool noswap,
  2086. struct zone *zone,
  2087. unsigned long *nr_scanned)
  2088. {
  2089. struct scan_control sc = {
  2090. .nr_scanned = 0,
  2091. .nr_to_reclaim = SWAP_CLUSTER_MAX,
  2092. .may_writepage = !laptop_mode,
  2093. .may_unmap = 1,
  2094. .may_swap = !noswap,
  2095. .order = 0,
  2096. .target_mem_cgroup = memcg,
  2097. };
  2098. struct mem_cgroup_zone mz = {
  2099. .mem_cgroup = memcg,
  2100. .zone = zone,
  2101. };
  2102. sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
  2103. (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
  2104. trace_mm_vmscan_memcg_softlimit_reclaim_begin(0,
  2105. sc.may_writepage,
  2106. sc.gfp_mask);
  2107. /*
  2108. * NOTE: Although we can get the priority field, using it
  2109. * here is not a good idea, since it limits the pages we can scan.
  2110. * if we don't reclaim here, the shrink_zone from balance_pgdat
  2111. * will pick up pages from other mem cgroup's as well. We hack
  2112. * the priority and make it zero.
  2113. */
  2114. shrink_mem_cgroup_zone(0, &mz, &sc);
  2115. trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed);
  2116. *nr_scanned = sc.nr_scanned;
  2117. return sc.nr_reclaimed;
  2118. }
  2119. unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
  2120. gfp_t gfp_mask,
  2121. bool noswap)
  2122. {
  2123. struct zonelist *zonelist;
  2124. unsigned long nr_reclaimed;
  2125. int nid;
  2126. struct scan_control sc = {
  2127. .may_writepage = !laptop_mode,
  2128. .may_unmap = 1,
  2129. .may_swap = !noswap,
  2130. .nr_to_reclaim = SWAP_CLUSTER_MAX,
  2131. .order = 0,
  2132. .target_mem_cgroup = memcg,
  2133. .nodemask = NULL, /* we don't care the placement */
  2134. .gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
  2135. (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK),
  2136. };
  2137. struct shrink_control shrink = {
  2138. .gfp_mask = sc.gfp_mask,
  2139. };
  2140. /*
  2141. * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't
  2142. * take care of from where we get pages. So the node where we start the
  2143. * scan does not need to be the current node.
  2144. */
  2145. nid = mem_cgroup_select_victim_node(memcg);
  2146. zonelist = NODE_DATA(nid)->node_zonelists;
  2147. trace_mm_vmscan_memcg_reclaim_begin(0,
  2148. sc.may_writepage,
  2149. sc.gfp_mask);
  2150. nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink);
  2151. trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed);
  2152. return nr_reclaimed;
  2153. }
  2154. #endif
  2155. static void age_active_anon(struct zone *zone, struct scan_control *sc,
  2156. int priority)
  2157. {
  2158. struct mem_cgroup *memcg;
  2159. if (!total_swap_pages)
  2160. return;
  2161. memcg = mem_cgroup_iter(NULL, NULL, NULL);
  2162. do {
  2163. struct mem_cgroup_zone mz = {
  2164. .mem_cgroup = memcg,
  2165. .zone = zone,
  2166. };
  2167. if (inactive_anon_is_low(&mz))
  2168. shrink_active_list(SWAP_CLUSTER_MAX, &mz,
  2169. sc, priority, 0);
  2170. memcg = mem_cgroup_iter(NULL, memcg, NULL);
  2171. } while (memcg);
  2172. }
  2173. /*
  2174. * pgdat_balanced is used when checking if a node is balanced for high-order
  2175. * allocations. Only zones that meet watermarks and are in a zone allowed
  2176. * by the callers classzone_idx are added to balanced_pages. The total of
  2177. * balanced pages must be at least 25% of the zones allowed by classzone_idx
  2178. * for the node to be considered balanced. Forcing all zones to be balanced
  2179. * for high orders can cause excessive reclaim when there are imbalanced zones.
  2180. * The choice of 25% is due to
  2181. * o a 16M DMA zone that is balanced will not balance a zone on any
  2182. * reasonable sized machine
  2183. * o On all other machines, the top zone must be at least a reasonable
  2184. * percentage of the middle zones. For example, on 32-bit x86, highmem
  2185. * would need to be at least 256M for it to be balance a whole node.
  2186. * Similarly, on x86-64 the Normal zone would need to be at least 1G
  2187. * to balance a node on its own. These seemed like reasonable ratios.
  2188. */
  2189. static bool pgdat_balanced(pg_data_t *pgdat, unsigned long balanced_pages,
  2190. int classzone_idx)
  2191. {
  2192. unsigned long present_pages = 0;
  2193. int i;
  2194. for (i = 0; i <= classzone_idx; i++)
  2195. present_pages += pgdat->node_zones[i].present_pages;
  2196. /* A special case here: if zone has no page, we think it's balanced */
  2197. return balanced_pages >= (present_pages >> 2);
  2198. }
  2199. /* is kswapd sleeping prematurely? */
  2200. static bool sleeping_prematurely(pg_data_t *pgdat, int order, long remaining,
  2201. int classzone_idx)
  2202. {
  2203. int i;
  2204. unsigned long balanced = 0;
  2205. bool all_zones_ok = true;
  2206. /* If a direct reclaimer woke kswapd within HZ/10, it's premature */
  2207. if (remaining)
  2208. return true;
  2209. /* Check the watermark levels */
  2210. for (i = 0; i <= classzone_idx; i++) {
  2211. struct zone *zone = pgdat->node_zones + i;
  2212. if (!populated_zone(zone))
  2213. continue;
  2214. /*
  2215. * balance_pgdat() skips over all_unreclaimable after
  2216. * DEF_PRIORITY. Effectively, it considers them balanced so
  2217. * they must be considered balanced here as well if kswapd
  2218. * is to sleep
  2219. */
  2220. if (zone->all_unreclaimable) {
  2221. balanced += zone->present_pages;
  2222. continue;
  2223. }
  2224. if (!zone_watermark_ok_safe(zone, order, high_wmark_pages(zone),
  2225. i, 0))
  2226. all_zones_ok = false;
  2227. else
  2228. balanced += zone->present_pages;
  2229. }
  2230. /*
  2231. * For high-order requests, the balanced zones must contain at least
  2232. * 25% of the nodes pages for kswapd to sleep. For order-0, all zones
  2233. * must be balanced
  2234. */
  2235. if (order)
  2236. return !pgdat_balanced(pgdat, balanced, classzone_idx);
  2237. else
  2238. return !all_zones_ok;
  2239. }
  2240. /*
  2241. * For kswapd, balance_pgdat() will work across all this node's zones until
  2242. * they are all at high_wmark_pages(zone).
  2243. *
  2244. * Returns the final order kswapd was reclaiming at
  2245. *
  2246. * There is special handling here for zones which are full of pinned pages.
  2247. * This can happen if the pages are all mlocked, or if they are all used by
  2248. * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
  2249. * What we do is to detect the case where all pages in the zone have been
  2250. * scanned twice and there has been zero successful reclaim. Mark the zone as
  2251. * dead and from now on, only perform a short scan. Basically we're polling
  2252. * the zone for when the problem goes away.
  2253. *
  2254. * kswapd scans the zones in the highmem->normal->dma direction. It skips
  2255. * zones which have free_pages > high_wmark_pages(zone), but once a zone is
  2256. * found to have free_pages <= high_wmark_pages(zone), we scan that zone and the
  2257. * lower zones regardless of the number of free pages in the lower zones. This
  2258. * interoperates with the page allocator fallback scheme to ensure that aging
  2259. * of pages is balanced across the zones.
  2260. */
  2261. static unsigned long balance_pgdat(pg_data_t *pgdat, int order,
  2262. int *classzone_idx)
  2263. {
  2264. int all_zones_ok;
  2265. unsigned long balanced;
  2266. int priority;
  2267. int i;
  2268. int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
  2269. unsigned long total_scanned;
  2270. struct reclaim_state *reclaim_state = current->reclaim_state;
  2271. unsigned long nr_soft_reclaimed;
  2272. unsigned long nr_soft_scanned;
  2273. struct scan_control sc = {
  2274. .gfp_mask = GFP_KERNEL,
  2275. .may_unmap = 1,
  2276. .may_swap = 1,
  2277. /*
  2278. * kswapd doesn't want to be bailed out while reclaim. because
  2279. * we want to put equal scanning pressure on each zone.
  2280. */
  2281. .nr_to_reclaim = ULONG_MAX,
  2282. .order = order,
  2283. .target_mem_cgroup = NULL,
  2284. };
  2285. struct shrink_control shrink = {
  2286. .gfp_mask = sc.gfp_mask,
  2287. };
  2288. loop_again:
  2289. total_scanned = 0;
  2290. sc.nr_reclaimed = 0;
  2291. sc.may_writepage = !laptop_mode;
  2292. count_vm_event(PAGEOUTRUN);
  2293. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  2294. unsigned long lru_pages = 0;
  2295. int has_under_min_watermark_zone = 0;
  2296. /* The swap token gets in the way of swapout... */
  2297. if (!priority)
  2298. disable_swap_token(NULL);
  2299. all_zones_ok = 1;
  2300. balanced = 0;
  2301. /*
  2302. * Scan in the highmem->dma direction for the highest
  2303. * zone which needs scanning
  2304. */
  2305. for (i = pgdat->nr_zones - 1; i >= 0; i--) {
  2306. struct zone *zone = pgdat->node_zones + i;
  2307. if (!populated_zone(zone))
  2308. continue;
  2309. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  2310. continue;
  2311. /*
  2312. * Do some background aging of the anon list, to give
  2313. * pages a chance to be referenced before reclaiming.
  2314. */
  2315. age_active_anon(zone, &sc, priority);
  2316. if (!zone_watermark_ok_safe(zone, order,
  2317. high_wmark_pages(zone), 0, 0)) {
  2318. end_zone = i;
  2319. break;
  2320. } else {
  2321. /* If balanced, clear the congested flag */
  2322. zone_clear_flag(zone, ZONE_CONGESTED);
  2323. }
  2324. }
  2325. if (i < 0)
  2326. goto out;
  2327. for (i = 0; i <= end_zone; i++) {
  2328. struct zone *zone = pgdat->node_zones + i;
  2329. lru_pages += zone_reclaimable_pages(zone);
  2330. }
  2331. /*
  2332. * Now scan the zone in the dma->highmem direction, stopping
  2333. * at the last zone which needs scanning.
  2334. *
  2335. * We do this because the page allocator works in the opposite
  2336. * direction. This prevents the page allocator from allocating
  2337. * pages behind kswapd's direction of progress, which would
  2338. * cause too much scanning of the lower zones.
  2339. */
  2340. for (i = 0; i <= end_zone; i++) {
  2341. struct zone *zone = pgdat->node_zones + i;
  2342. int nr_slab;
  2343. unsigned long balance_gap;
  2344. if (!populated_zone(zone))
  2345. continue;
  2346. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  2347. continue;
  2348. sc.nr_scanned = 0;
  2349. nr_soft_scanned = 0;
  2350. /*
  2351. * Call soft limit reclaim before calling shrink_zone.
  2352. */
  2353. nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone,
  2354. order, sc.gfp_mask,
  2355. &nr_soft_scanned);
  2356. sc.nr_reclaimed += nr_soft_reclaimed;
  2357. total_scanned += nr_soft_scanned;
  2358. /*
  2359. * We put equal pressure on every zone, unless
  2360. * one zone has way too many pages free
  2361. * already. The "too many pages" is defined
  2362. * as the high wmark plus a "gap" where the
  2363. * gap is either the low watermark or 1%
  2364. * of the zone, whichever is smaller.
  2365. */
  2366. balance_gap = min(low_wmark_pages(zone),
  2367. (zone->present_pages +
  2368. KSWAPD_ZONE_BALANCE_GAP_RATIO-1) /
  2369. KSWAPD_ZONE_BALANCE_GAP_RATIO);
  2370. if (!zone_watermark_ok_safe(zone, order,
  2371. high_wmark_pages(zone) + balance_gap,
  2372. end_zone, 0)) {
  2373. shrink_zone(priority, zone, &sc);
  2374. reclaim_state->reclaimed_slab = 0;
  2375. nr_slab = shrink_slab(&shrink, sc.nr_scanned, lru_pages);
  2376. sc.nr_reclaimed += reclaim_state->reclaimed_slab;
  2377. total_scanned += sc.nr_scanned;
  2378. if (nr_slab == 0 && !zone_reclaimable(zone))
  2379. zone->all_unreclaimable = 1;
  2380. }
  2381. /*
  2382. * If we've done a decent amount of scanning and
  2383. * the reclaim ratio is low, start doing writepage
  2384. * even in laptop mode
  2385. */
  2386. if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
  2387. total_scanned > sc.nr_reclaimed + sc.nr_reclaimed / 2)
  2388. sc.may_writepage = 1;
  2389. if (zone->all_unreclaimable) {
  2390. if (end_zone && end_zone == i)
  2391. end_zone--;
  2392. continue;
  2393. }
  2394. if (!zone_watermark_ok_safe(zone, order,
  2395. high_wmark_pages(zone), end_zone, 0)) {
  2396. all_zones_ok = 0;
  2397. /*
  2398. * We are still under min water mark. This
  2399. * means that we have a GFP_ATOMIC allocation
  2400. * failure risk. Hurry up!
  2401. */
  2402. if (!zone_watermark_ok_safe(zone, order,
  2403. min_wmark_pages(zone), end_zone, 0))
  2404. has_under_min_watermark_zone = 1;
  2405. } else {
  2406. /*
  2407. * If a zone reaches its high watermark,
  2408. * consider it to be no longer congested. It's
  2409. * possible there are dirty pages backed by
  2410. * congested BDIs but as pressure is relieved,
  2411. * spectulatively avoid congestion waits
  2412. */
  2413. zone_clear_flag(zone, ZONE_CONGESTED);
  2414. if (i <= *classzone_idx)
  2415. balanced += zone->present_pages;
  2416. }
  2417. }
  2418. if (all_zones_ok || (order && pgdat_balanced(pgdat, balanced, *classzone_idx)))
  2419. break; /* kswapd: all done */
  2420. /*
  2421. * OK, kswapd is getting into trouble. Take a nap, then take
  2422. * another pass across the zones.
  2423. */
  2424. if (total_scanned && (priority < DEF_PRIORITY - 2)) {
  2425. if (has_under_min_watermark_zone)
  2426. count_vm_event(KSWAPD_SKIP_CONGESTION_WAIT);
  2427. else
  2428. congestion_wait(BLK_RW_ASYNC, HZ/10);
  2429. }
  2430. /*
  2431. * We do this so kswapd doesn't build up large priorities for
  2432. * example when it is freeing in parallel with allocators. It
  2433. * matches the direct reclaim path behaviour in terms of impact
  2434. * on zone->*_priority.
  2435. */
  2436. if (sc.nr_reclaimed >= SWAP_CLUSTER_MAX)
  2437. break;
  2438. }
  2439. out:
  2440. /*
  2441. * order-0: All zones must meet high watermark for a balanced node
  2442. * high-order: Balanced zones must make up at least 25% of the node
  2443. * for the node to be balanced
  2444. */
  2445. if (!(all_zones_ok || (order && pgdat_balanced(pgdat, balanced, *classzone_idx)))) {
  2446. cond_resched();
  2447. try_to_freeze();
  2448. /*
  2449. * Fragmentation may mean that the system cannot be
  2450. * rebalanced for high-order allocations in all zones.
  2451. * At this point, if nr_reclaimed < SWAP_CLUSTER_MAX,
  2452. * it means the zones have been fully scanned and are still
  2453. * not balanced. For high-order allocations, there is
  2454. * little point trying all over again as kswapd may
  2455. * infinite loop.
  2456. *
  2457. * Instead, recheck all watermarks at order-0 as they
  2458. * are the most important. If watermarks are ok, kswapd will go
  2459. * back to sleep. High-order users can still perform direct
  2460. * reclaim if they wish.
  2461. */
  2462. if (sc.nr_reclaimed < SWAP_CLUSTER_MAX)
  2463. order = sc.order = 0;
  2464. goto loop_again;
  2465. }
  2466. /*
  2467. * If kswapd was reclaiming at a higher order, it has the option of
  2468. * sleeping without all zones being balanced. Before it does, it must
  2469. * ensure that the watermarks for order-0 on *all* zones are met and
  2470. * that the congestion flags are cleared. The congestion flag must
  2471. * be cleared as kswapd is the only mechanism that clears the flag
  2472. * and it is potentially going to sleep here.
  2473. */
  2474. if (order) {
  2475. for (i = 0; i <= end_zone; i++) {
  2476. struct zone *zone = pgdat->node_zones + i;
  2477. if (!populated_zone(zone))
  2478. continue;
  2479. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  2480. continue;
  2481. /* Confirm the zone is balanced for order-0 */
  2482. if (!zone_watermark_ok(zone, 0,
  2483. high_wmark_pages(zone), 0, 0)) {
  2484. order = sc.order = 0;
  2485. goto loop_again;
  2486. }
  2487. /* If balanced, clear the congested flag */
  2488. zone_clear_flag(zone, ZONE_CONGESTED);
  2489. if (i <= *classzone_idx)
  2490. balanced += zone->present_pages;
  2491. }
  2492. }
  2493. /*
  2494. * Return the order we were reclaiming at so sleeping_prematurely()
  2495. * makes a decision on the order we were last reclaiming at. However,
  2496. * if another caller entered the allocator slow path while kswapd
  2497. * was awake, order will remain at the higher level
  2498. */
  2499. *classzone_idx = end_zone;
  2500. return order;
  2501. }
  2502. static void kswapd_try_to_sleep(pg_data_t *pgdat, int order, int classzone_idx)
  2503. {
  2504. long remaining = 0;
  2505. DEFINE_WAIT(wait);
  2506. if (freezing(current) || kthread_should_stop())
  2507. return;
  2508. prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
  2509. /* Try to sleep for a short interval */
  2510. if (!sleeping_prematurely(pgdat, order, remaining, classzone_idx)) {
  2511. remaining = schedule_timeout(HZ/10);
  2512. finish_wait(&pgdat->kswapd_wait, &wait);
  2513. prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
  2514. }
  2515. /*
  2516. * After a short sleep, check if it was a premature sleep. If not, then
  2517. * go fully to sleep until explicitly woken up.
  2518. */
  2519. if (!sleeping_prematurely(pgdat, order, remaining, classzone_idx)) {
  2520. trace_mm_vmscan_kswapd_sleep(pgdat->node_id);
  2521. /*
  2522. * vmstat counters are not perfectly accurate and the estimated
  2523. * value for counters such as NR_FREE_PAGES can deviate from the
  2524. * true value by nr_online_cpus * threshold. To avoid the zone
  2525. * watermarks being breached while under pressure, we reduce the
  2526. * per-cpu vmstat threshold while kswapd is awake and restore
  2527. * them before going back to sleep.
  2528. */
  2529. set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold);
  2530. schedule();
  2531. set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold);
  2532. } else {
  2533. if (remaining)
  2534. count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY);
  2535. else
  2536. count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY);
  2537. }
  2538. finish_wait(&pgdat->kswapd_wait, &wait);
  2539. }
  2540. /*
  2541. * The background pageout daemon, started as a kernel thread
  2542. * from the init process.
  2543. *
  2544. * This basically trickles out pages so that we have _some_
  2545. * free memory available even if there is no other activity
  2546. * that frees anything up. This is needed for things like routing
  2547. * etc, where we otherwise might have all activity going on in
  2548. * asynchronous contexts that cannot page things out.
  2549. *
  2550. * If there are applications that are active memory-allocators
  2551. * (most normal use), this basically shouldn't matter.
  2552. */
  2553. static int kswapd(void *p)
  2554. {
  2555. unsigned long order, new_order;
  2556. unsigned balanced_order;
  2557. int classzone_idx, new_classzone_idx;
  2558. int balanced_classzone_idx;
  2559. pg_data_t *pgdat = (pg_data_t*)p;
  2560. struct task_struct *tsk = current;
  2561. struct reclaim_state reclaim_state = {
  2562. .reclaimed_slab = 0,
  2563. };
  2564. const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
  2565. lockdep_set_current_reclaim_state(GFP_KERNEL);
  2566. if (!cpumask_empty(cpumask))
  2567. set_cpus_allowed_ptr(tsk, cpumask);
  2568. current->reclaim_state = &reclaim_state;
  2569. /*
  2570. * Tell the memory management that we're a "memory allocator",
  2571. * and that if we need more memory we should get access to it
  2572. * regardless (see "__alloc_pages()"). "kswapd" should
  2573. * never get caught in the normal page freeing logic.
  2574. *
  2575. * (Kswapd normally doesn't need memory anyway, but sometimes
  2576. * you need a small amount of memory in order to be able to
  2577. * page out something else, and this flag essentially protects
  2578. * us from recursively trying to free more memory as we're
  2579. * trying to free the first piece of memory in the first place).
  2580. */
  2581. tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
  2582. set_freezable();
  2583. order = new_order = 0;
  2584. balanced_order = 0;
  2585. classzone_idx = new_classzone_idx = pgdat->nr_zones - 1;
  2586. balanced_classzone_idx = classzone_idx;
  2587. for ( ; ; ) {
  2588. int ret;
  2589. /*
  2590. * If the last balance_pgdat was unsuccessful it's unlikely a
  2591. * new request of a similar or harder type will succeed soon
  2592. * so consider going to sleep on the basis we reclaimed at
  2593. */
  2594. if (balanced_classzone_idx >= new_classzone_idx &&
  2595. balanced_order == new_order) {
  2596. new_order = pgdat->kswapd_max_order;
  2597. new_classzone_idx = pgdat->classzone_idx;
  2598. pgdat->kswapd_max_order = 0;
  2599. pgdat->classzone_idx = pgdat->nr_zones - 1;
  2600. }
  2601. if (order < new_order || classzone_idx > new_classzone_idx) {
  2602. /*
  2603. * Don't sleep if someone wants a larger 'order'
  2604. * allocation or has tigher zone constraints
  2605. */
  2606. order = new_order;
  2607. classzone_idx = new_classzone_idx;
  2608. } else {
  2609. kswapd_try_to_sleep(pgdat, balanced_order,
  2610. balanced_classzone_idx);
  2611. order = pgdat->kswapd_max_order;
  2612. classzone_idx = pgdat->classzone_idx;
  2613. new_order = order;
  2614. new_classzone_idx = classzone_idx;
  2615. pgdat->kswapd_max_order = 0;
  2616. pgdat->classzone_idx = pgdat->nr_zones - 1;
  2617. }
  2618. ret = try_to_freeze();
  2619. if (kthread_should_stop())
  2620. break;
  2621. /*
  2622. * We can speed up thawing tasks if we don't call balance_pgdat
  2623. * after returning from the refrigerator
  2624. */
  2625. if (!ret) {
  2626. trace_mm_vmscan_kswapd_wake(pgdat->node_id, order);
  2627. balanced_classzone_idx = classzone_idx;
  2628. balanced_order = balance_pgdat(pgdat, order,
  2629. &balanced_classzone_idx);
  2630. }
  2631. }
  2632. return 0;
  2633. }
  2634. /*
  2635. * A zone is low on free memory, so wake its kswapd task to service it.
  2636. */
  2637. void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx)
  2638. {
  2639. pg_data_t *pgdat;
  2640. if (!populated_zone(zone))
  2641. return;
  2642. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  2643. return;
  2644. pgdat = zone->zone_pgdat;
  2645. if (pgdat->kswapd_max_order < order) {
  2646. pgdat->kswapd_max_order = order;
  2647. pgdat->classzone_idx = min(pgdat->classzone_idx, classzone_idx);
  2648. }
  2649. if (!waitqueue_active(&pgdat->kswapd_wait))
  2650. return;
  2651. if (zone_watermark_ok_safe(zone, order, low_wmark_pages(zone), 0, 0))
  2652. return;
  2653. trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, zone_idx(zone), order);
  2654. wake_up_interruptible(&pgdat->kswapd_wait);
  2655. }
  2656. /*
  2657. * The reclaimable count would be mostly accurate.
  2658. * The less reclaimable pages may be
  2659. * - mlocked pages, which will be moved to unevictable list when encountered
  2660. * - mapped pages, which may require several travels to be reclaimed
  2661. * - dirty pages, which is not "instantly" reclaimable
  2662. */
  2663. unsigned long global_reclaimable_pages(void)
  2664. {
  2665. int nr;
  2666. nr = global_page_state(NR_ACTIVE_FILE) +
  2667. global_page_state(NR_INACTIVE_FILE);
  2668. if (nr_swap_pages > 0)
  2669. nr += global_page_state(NR_ACTIVE_ANON) +
  2670. global_page_state(NR_INACTIVE_ANON);
  2671. return nr;
  2672. }
  2673. unsigned long zone_reclaimable_pages(struct zone *zone)
  2674. {
  2675. int nr;
  2676. nr = zone_page_state(zone, NR_ACTIVE_FILE) +
  2677. zone_page_state(zone, NR_INACTIVE_FILE);
  2678. if (nr_swap_pages > 0)
  2679. nr += zone_page_state(zone, NR_ACTIVE_ANON) +
  2680. zone_page_state(zone, NR_INACTIVE_ANON);
  2681. return nr;
  2682. }
  2683. #ifdef CONFIG_HIBERNATION
  2684. /*
  2685. * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of
  2686. * freed pages.
  2687. *
  2688. * Rather than trying to age LRUs the aim is to preserve the overall
  2689. * LRU order by reclaiming preferentially
  2690. * inactive > active > active referenced > active mapped
  2691. */
  2692. unsigned long shrink_all_memory(unsigned long nr_to_reclaim)
  2693. {
  2694. struct reclaim_state reclaim_state;
  2695. struct scan_control sc = {
  2696. .gfp_mask = GFP_HIGHUSER_MOVABLE,
  2697. .may_swap = 1,
  2698. .may_unmap = 1,
  2699. .may_writepage = 1,
  2700. .nr_to_reclaim = nr_to_reclaim,
  2701. .hibernation_mode = 1,
  2702. .order = 0,
  2703. };
  2704. struct shrink_control shrink = {
  2705. .gfp_mask = sc.gfp_mask,
  2706. };
  2707. struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
  2708. struct task_struct *p = current;
  2709. unsigned long nr_reclaimed;
  2710. p->flags |= PF_MEMALLOC;
  2711. lockdep_set_current_reclaim_state(sc.gfp_mask);
  2712. reclaim_state.reclaimed_slab = 0;
  2713. p->reclaim_state = &reclaim_state;
  2714. nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink);
  2715. p->reclaim_state = NULL;
  2716. lockdep_clear_current_reclaim_state();
  2717. p->flags &= ~PF_MEMALLOC;
  2718. return nr_reclaimed;
  2719. }
  2720. #endif /* CONFIG_HIBERNATION */
  2721. /* It's optimal to keep kswapds on the same CPUs as their memory, but
  2722. not required for correctness. So if the last cpu in a node goes
  2723. away, we get changed to run anywhere: as the first one comes back,
  2724. restore their cpu bindings. */
  2725. static int __devinit cpu_callback(struct notifier_block *nfb,
  2726. unsigned long action, void *hcpu)
  2727. {
  2728. int nid;
  2729. if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
  2730. for_each_node_state(nid, N_HIGH_MEMORY) {
  2731. pg_data_t *pgdat = NODE_DATA(nid);
  2732. const struct cpumask *mask;
  2733. mask = cpumask_of_node(pgdat->node_id);
  2734. if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
  2735. /* One of our CPUs online: restore mask */
  2736. set_cpus_allowed_ptr(pgdat->kswapd, mask);
  2737. }
  2738. }
  2739. return NOTIFY_OK;
  2740. }
  2741. /*
  2742. * This kswapd start function will be called by init and node-hot-add.
  2743. * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
  2744. */
  2745. int kswapd_run(int nid)
  2746. {
  2747. pg_data_t *pgdat = NODE_DATA(nid);
  2748. int ret = 0;
  2749. if (pgdat->kswapd)
  2750. return 0;
  2751. pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
  2752. if (IS_ERR(pgdat->kswapd)) {
  2753. /* failure at boot is fatal */
  2754. BUG_ON(system_state == SYSTEM_BOOTING);
  2755. printk("Failed to start kswapd on node %d\n",nid);
  2756. ret = -1;
  2757. }
  2758. return ret;
  2759. }
  2760. /*
  2761. * Called by memory hotplug when all memory in a node is offlined.
  2762. */
  2763. void kswapd_stop(int nid)
  2764. {
  2765. struct task_struct *kswapd = NODE_DATA(nid)->kswapd;
  2766. if (kswapd)
  2767. kthread_stop(kswapd);
  2768. }
  2769. static int __init kswapd_init(void)
  2770. {
  2771. int nid;
  2772. swap_setup();
  2773. for_each_node_state(nid, N_HIGH_MEMORY)
  2774. kswapd_run(nid);
  2775. hotcpu_notifier(cpu_callback, 0);
  2776. return 0;
  2777. }
  2778. module_init(kswapd_init)
  2779. #ifdef CONFIG_NUMA
  2780. /*
  2781. * Zone reclaim mode
  2782. *
  2783. * If non-zero call zone_reclaim when the number of free pages falls below
  2784. * the watermarks.
  2785. */
  2786. int zone_reclaim_mode __read_mostly;
  2787. #define RECLAIM_OFF 0
  2788. #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */
  2789. #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
  2790. #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
  2791. /*
  2792. * Priority for ZONE_RECLAIM. This determines the fraction of pages
  2793. * of a node considered for each zone_reclaim. 4 scans 1/16th of
  2794. * a zone.
  2795. */
  2796. #define ZONE_RECLAIM_PRIORITY 4
  2797. /*
  2798. * Percentage of pages in a zone that must be unmapped for zone_reclaim to
  2799. * occur.
  2800. */
  2801. int sysctl_min_unmapped_ratio = 1;
  2802. /*
  2803. * If the number of slab pages in a zone grows beyond this percentage then
  2804. * slab reclaim needs to occur.
  2805. */
  2806. int sysctl_min_slab_ratio = 5;
  2807. static inline unsigned long zone_unmapped_file_pages(struct zone *zone)
  2808. {
  2809. unsigned long file_mapped = zone_page_state(zone, NR_FILE_MAPPED);
  2810. unsigned long file_lru = zone_page_state(zone, NR_INACTIVE_FILE) +
  2811. zone_page_state(zone, NR_ACTIVE_FILE);
  2812. /*
  2813. * It's possible for there to be more file mapped pages than
  2814. * accounted for by the pages on the file LRU lists because
  2815. * tmpfs pages accounted for as ANON can also be FILE_MAPPED
  2816. */
  2817. return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0;
  2818. }
  2819. /* Work out how many page cache pages we can reclaim in this reclaim_mode */
  2820. static long zone_pagecache_reclaimable(struct zone *zone)
  2821. {
  2822. long nr_pagecache_reclaimable;
  2823. long delta = 0;
  2824. /*
  2825. * If RECLAIM_SWAP is set, then all file pages are considered
  2826. * potentially reclaimable. Otherwise, we have to worry about
  2827. * pages like swapcache and zone_unmapped_file_pages() provides
  2828. * a better estimate
  2829. */
  2830. if (zone_reclaim_mode & RECLAIM_SWAP)
  2831. nr_pagecache_reclaimable = zone_page_state(zone, NR_FILE_PAGES);
  2832. else
  2833. nr_pagecache_reclaimable = zone_unmapped_file_pages(zone);
  2834. /* If we can't clean pages, remove dirty pages from consideration */
  2835. if (!(zone_reclaim_mode & RECLAIM_WRITE))
  2836. delta += zone_page_state(zone, NR_FILE_DIRTY);
  2837. /* Watch for any possible underflows due to delta */
  2838. if (unlikely(delta > nr_pagecache_reclaimable))
  2839. delta = nr_pagecache_reclaimable;
  2840. return nr_pagecache_reclaimable - delta;
  2841. }
  2842. /*
  2843. * Try to free up some pages from this zone through reclaim.
  2844. */
  2845. static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  2846. {
  2847. /* Minimum pages needed in order to stay on node */
  2848. const unsigned long nr_pages = 1 << order;
  2849. struct task_struct *p = current;
  2850. struct reclaim_state reclaim_state;
  2851. int priority;
  2852. struct scan_control sc = {
  2853. .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
  2854. .may_unmap = !!(zone_reclaim_mode & RECLAIM_SWAP),
  2855. .may_swap = 1,
  2856. .nr_to_reclaim = max_t(unsigned long, nr_pages,
  2857. SWAP_CLUSTER_MAX),
  2858. .gfp_mask = gfp_mask,
  2859. .order = order,
  2860. };
  2861. struct shrink_control shrink = {
  2862. .gfp_mask = sc.gfp_mask,
  2863. };
  2864. unsigned long nr_slab_pages0, nr_slab_pages1;
  2865. cond_resched();
  2866. /*
  2867. * We need to be able to allocate from the reserves for RECLAIM_SWAP
  2868. * and we also need to be able to write out pages for RECLAIM_WRITE
  2869. * and RECLAIM_SWAP.
  2870. */
  2871. p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
  2872. lockdep_set_current_reclaim_state(gfp_mask);
  2873. reclaim_state.reclaimed_slab = 0;
  2874. p->reclaim_state = &reclaim_state;
  2875. if (zone_pagecache_reclaimable(zone) > zone->min_unmapped_pages) {
  2876. /*
  2877. * Free memory by calling shrink zone with increasing
  2878. * priorities until we have enough memory freed.
  2879. */
  2880. priority = ZONE_RECLAIM_PRIORITY;
  2881. do {
  2882. shrink_zone(priority, zone, &sc);
  2883. priority--;
  2884. } while (priority >= 0 && sc.nr_reclaimed < nr_pages);
  2885. }
  2886. nr_slab_pages0 = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
  2887. if (nr_slab_pages0 > zone->min_slab_pages) {
  2888. /*
  2889. * shrink_slab() does not currently allow us to determine how
  2890. * many pages were freed in this zone. So we take the current
  2891. * number of slab pages and shake the slab until it is reduced
  2892. * by the same nr_pages that we used for reclaiming unmapped
  2893. * pages.
  2894. *
  2895. * Note that shrink_slab will free memory on all zones and may
  2896. * take a long time.
  2897. */
  2898. for (;;) {
  2899. unsigned long lru_pages = zone_reclaimable_pages(zone);
  2900. /* No reclaimable slab or very low memory pressure */
  2901. if (!shrink_slab(&shrink, sc.nr_scanned, lru_pages))
  2902. break;
  2903. /* Freed enough memory */
  2904. nr_slab_pages1 = zone_page_state(zone,
  2905. NR_SLAB_RECLAIMABLE);
  2906. if (nr_slab_pages1 + nr_pages <= nr_slab_pages0)
  2907. break;
  2908. }
  2909. /*
  2910. * Update nr_reclaimed by the number of slab pages we
  2911. * reclaimed from this zone.
  2912. */
  2913. nr_slab_pages1 = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
  2914. if (nr_slab_pages1 < nr_slab_pages0)
  2915. sc.nr_reclaimed += nr_slab_pages0 - nr_slab_pages1;
  2916. }
  2917. p->reclaim_state = NULL;
  2918. current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
  2919. lockdep_clear_current_reclaim_state();
  2920. return sc.nr_reclaimed >= nr_pages;
  2921. }
  2922. int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  2923. {
  2924. int node_id;
  2925. int ret;
  2926. /*
  2927. * Zone reclaim reclaims unmapped file backed pages and
  2928. * slab pages if we are over the defined limits.
  2929. *
  2930. * A small portion of unmapped file backed pages is needed for
  2931. * file I/O otherwise pages read by file I/O will be immediately
  2932. * thrown out if the zone is overallocated. So we do not reclaim
  2933. * if less than a specified percentage of the zone is used by
  2934. * unmapped file backed pages.
  2935. */
  2936. if (zone_pagecache_reclaimable(zone) <= zone->min_unmapped_pages &&
  2937. zone_page_state(zone, NR_SLAB_RECLAIMABLE) <= zone->min_slab_pages)
  2938. return ZONE_RECLAIM_FULL;
  2939. if (zone->all_unreclaimable)
  2940. return ZONE_RECLAIM_FULL;
  2941. /*
  2942. * Do not scan if the allocation should not be delayed.
  2943. */
  2944. if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC))
  2945. return ZONE_RECLAIM_NOSCAN;
  2946. /*
  2947. * Only run zone reclaim on the local zone or on zones that do not
  2948. * have associated processors. This will favor the local processor
  2949. * over remote processors and spread off node memory allocations
  2950. * as wide as possible.
  2951. */
  2952. node_id = zone_to_nid(zone);
  2953. if (node_state(node_id, N_CPU) && node_id != numa_node_id())
  2954. return ZONE_RECLAIM_NOSCAN;
  2955. if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED))
  2956. return ZONE_RECLAIM_NOSCAN;
  2957. ret = __zone_reclaim(zone, gfp_mask, order);
  2958. zone_clear_flag(zone, ZONE_RECLAIM_LOCKED);
  2959. if (!ret)
  2960. count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED);
  2961. return ret;
  2962. }
  2963. #endif
  2964. /*
  2965. * page_evictable - test whether a page is evictable
  2966. * @page: the page to test
  2967. * @vma: the VMA in which the page is or will be mapped, may be NULL
  2968. *
  2969. * Test whether page is evictable--i.e., should be placed on active/inactive
  2970. * lists vs unevictable list. The vma argument is !NULL when called from the
  2971. * fault path to determine how to instantate a new page.
  2972. *
  2973. * Reasons page might not be evictable:
  2974. * (1) page's mapping marked unevictable
  2975. * (2) page is part of an mlocked VMA
  2976. *
  2977. */
  2978. int page_evictable(struct page *page, struct vm_area_struct *vma)
  2979. {
  2980. if (mapping_unevictable(page_mapping(page)))
  2981. return 0;
  2982. if (PageMlocked(page) || (vma && is_mlocked_vma(vma, page)))
  2983. return 0;
  2984. return 1;
  2985. }
  2986. /**
  2987. * check_move_unevictable_page - check page for evictability and move to appropriate zone lru list
  2988. * @page: page to check evictability and move to appropriate lru list
  2989. * @zone: zone page is in
  2990. *
  2991. * Checks a page for evictability and moves the page to the appropriate
  2992. * zone lru list.
  2993. *
  2994. * Restrictions: zone->lru_lock must be held, page must be on LRU and must
  2995. * have PageUnevictable set.
  2996. */
  2997. static void check_move_unevictable_page(struct page *page, struct zone *zone)
  2998. {
  2999. struct lruvec *lruvec;
  3000. VM_BUG_ON(PageActive(page));
  3001. retry:
  3002. ClearPageUnevictable(page);
  3003. if (page_evictable(page, NULL)) {
  3004. enum lru_list l = page_lru_base_type(page);
  3005. __dec_zone_state(zone, NR_UNEVICTABLE);
  3006. lruvec = mem_cgroup_lru_move_lists(zone, page,
  3007. LRU_UNEVICTABLE, l);
  3008. list_move(&page->lru, &lruvec->lists[l]);
  3009. __inc_zone_state(zone, NR_INACTIVE_ANON + l);
  3010. __count_vm_event(UNEVICTABLE_PGRESCUED);
  3011. } else {
  3012. /*
  3013. * rotate unevictable list
  3014. */
  3015. SetPageUnevictable(page);
  3016. lruvec = mem_cgroup_lru_move_lists(zone, page, LRU_UNEVICTABLE,
  3017. LRU_UNEVICTABLE);
  3018. list_move(&page->lru, &lruvec->lists[LRU_UNEVICTABLE]);
  3019. if (page_evictable(page, NULL))
  3020. goto retry;
  3021. }
  3022. }
  3023. /**
  3024. * scan_mapping_unevictable_pages - scan an address space for evictable pages
  3025. * @mapping: struct address_space to scan for evictable pages
  3026. *
  3027. * Scan all pages in mapping. Check unevictable pages for
  3028. * evictability and move them to the appropriate zone lru list.
  3029. */
  3030. void scan_mapping_unevictable_pages(struct address_space *mapping)
  3031. {
  3032. pgoff_t next = 0;
  3033. pgoff_t end = (i_size_read(mapping->host) + PAGE_CACHE_SIZE - 1) >>
  3034. PAGE_CACHE_SHIFT;
  3035. struct zone *zone;
  3036. struct pagevec pvec;
  3037. if (mapping->nrpages == 0)
  3038. return;
  3039. pagevec_init(&pvec, 0);
  3040. while (next < end &&
  3041. pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  3042. int i;
  3043. int pg_scanned = 0;
  3044. zone = NULL;
  3045. for (i = 0; i < pagevec_count(&pvec); i++) {
  3046. struct page *page = pvec.pages[i];
  3047. pgoff_t page_index = page->index;
  3048. struct zone *pagezone = page_zone(page);
  3049. pg_scanned++;
  3050. if (page_index > next)
  3051. next = page_index;
  3052. next++;
  3053. if (pagezone != zone) {
  3054. if (zone)
  3055. spin_unlock_irq(&zone->lru_lock);
  3056. zone = pagezone;
  3057. spin_lock_irq(&zone->lru_lock);
  3058. }
  3059. if (PageLRU(page) && PageUnevictable(page))
  3060. check_move_unevictable_page(page, zone);
  3061. }
  3062. if (zone)
  3063. spin_unlock_irq(&zone->lru_lock);
  3064. pagevec_release(&pvec);
  3065. count_vm_events(UNEVICTABLE_PGSCANNED, pg_scanned);
  3066. }
  3067. }
  3068. static void warn_scan_unevictable_pages(void)
  3069. {
  3070. printk_once(KERN_WARNING
  3071. "%s: The scan_unevictable_pages sysctl/node-interface has been "
  3072. "disabled for lack of a legitimate use case. If you have "
  3073. "one, please send an email to linux-mm@kvack.org.\n",
  3074. current->comm);
  3075. }
  3076. /*
  3077. * scan_unevictable_pages [vm] sysctl handler. On demand re-scan of
  3078. * all nodes' unevictable lists for evictable pages
  3079. */
  3080. unsigned long scan_unevictable_pages;
  3081. int scan_unevictable_handler(struct ctl_table *table, int write,
  3082. void __user *buffer,
  3083. size_t *length, loff_t *ppos)
  3084. {
  3085. warn_scan_unevictable_pages();
  3086. proc_doulongvec_minmax(table, write, buffer, length, ppos);
  3087. scan_unevictable_pages = 0;
  3088. return 0;
  3089. }
  3090. #ifdef CONFIG_NUMA
  3091. /*
  3092. * per node 'scan_unevictable_pages' attribute. On demand re-scan of
  3093. * a specified node's per zone unevictable lists for evictable pages.
  3094. */
  3095. static ssize_t read_scan_unevictable_node(struct device *dev,
  3096. struct device_attribute *attr,
  3097. char *buf)
  3098. {
  3099. warn_scan_unevictable_pages();
  3100. return sprintf(buf, "0\n"); /* always zero; should fit... */
  3101. }
  3102. static ssize_t write_scan_unevictable_node(struct device *dev,
  3103. struct device_attribute *attr,
  3104. const char *buf, size_t count)
  3105. {
  3106. warn_scan_unevictable_pages();
  3107. return 1;
  3108. }
  3109. static DEVICE_ATTR(scan_unevictable_pages, S_IRUGO | S_IWUSR,
  3110. read_scan_unevictable_node,
  3111. write_scan_unevictable_node);
  3112. int scan_unevictable_register_node(struct node *node)
  3113. {
  3114. return device_create_file(&node->dev, &dev_attr_scan_unevictable_pages);
  3115. }
  3116. void scan_unevictable_unregister_node(struct node *node)
  3117. {
  3118. device_remove_file(&node->dev, &dev_attr_scan_unevictable_pages);
  3119. }
  3120. #endif