vmscan.c 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658
  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. /*
  948. * To minimise LRU disruption, the caller can indicate that it only
  949. * wants to isolate pages it will be able to operate on without
  950. * blocking - clean pages for the most part.
  951. *
  952. * ISOLATE_CLEAN means that only clean pages should be isolated. This
  953. * is used by reclaim when it is cannot write to backing storage
  954. *
  955. * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages
  956. * that it is possible to migrate without blocking
  957. */
  958. if (mode & (ISOLATE_CLEAN|ISOLATE_ASYNC_MIGRATE)) {
  959. /* All the caller can do on PageWriteback is block */
  960. if (PageWriteback(page))
  961. return ret;
  962. if (PageDirty(page)) {
  963. struct address_space *mapping;
  964. /* ISOLATE_CLEAN means only clean pages */
  965. if (mode & ISOLATE_CLEAN)
  966. return ret;
  967. /*
  968. * Only pages without mappings or that have a
  969. * ->migratepage callback are possible to migrate
  970. * without blocking
  971. */
  972. mapping = page_mapping(page);
  973. if (mapping && !mapping->a_ops->migratepage)
  974. return ret;
  975. }
  976. }
  977. if ((mode & ISOLATE_UNMAPPED) && page_mapped(page))
  978. return ret;
  979. if (likely(get_page_unless_zero(page))) {
  980. /*
  981. * Be careful not to clear PageLRU until after we're
  982. * sure the page is not being freed elsewhere -- the
  983. * page release code relies on it.
  984. */
  985. ClearPageLRU(page);
  986. ret = 0;
  987. }
  988. return ret;
  989. }
  990. /*
  991. * zone->lru_lock is heavily contended. Some of the functions that
  992. * shrink the lists perform better by taking out a batch of pages
  993. * and working on them outside the LRU lock.
  994. *
  995. * For pagecache intensive workloads, this function is the hottest
  996. * spot in the kernel (apart from copy_*_user functions).
  997. *
  998. * Appropriate locks must be held before calling this function.
  999. *
  1000. * @nr_to_scan: The number of pages to look through on the list.
  1001. * @mz: The mem_cgroup_zone to pull pages from.
  1002. * @dst: The temp list to put pages on to.
  1003. * @nr_scanned: The number of pages that were scanned.
  1004. * @order: The caller's attempted allocation order
  1005. * @mode: One of the LRU isolation modes
  1006. * @active: True [1] if isolating active pages
  1007. * @file: True [1] if isolating file [!anon] pages
  1008. *
  1009. * returns how many pages were moved onto *@dst.
  1010. */
  1011. static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
  1012. struct mem_cgroup_zone *mz, struct list_head *dst,
  1013. unsigned long *nr_scanned, int order, isolate_mode_t mode,
  1014. int active, int file)
  1015. {
  1016. struct lruvec *lruvec;
  1017. struct list_head *src;
  1018. unsigned long nr_taken = 0;
  1019. unsigned long nr_lumpy_taken = 0;
  1020. unsigned long nr_lumpy_dirty = 0;
  1021. unsigned long nr_lumpy_failed = 0;
  1022. unsigned long scan;
  1023. int lru = LRU_BASE;
  1024. lruvec = mem_cgroup_zone_lruvec(mz->zone, mz->mem_cgroup);
  1025. if (active)
  1026. lru += LRU_ACTIVE;
  1027. if (file)
  1028. lru += LRU_FILE;
  1029. src = &lruvec->lists[lru];
  1030. for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
  1031. struct page *page;
  1032. unsigned long pfn;
  1033. unsigned long end_pfn;
  1034. unsigned long page_pfn;
  1035. int zone_id;
  1036. page = lru_to_page(src);
  1037. prefetchw_prev_lru_page(page, src, flags);
  1038. VM_BUG_ON(!PageLRU(page));
  1039. switch (__isolate_lru_page(page, mode, file)) {
  1040. case 0:
  1041. mem_cgroup_lru_del(page);
  1042. list_move(&page->lru, dst);
  1043. nr_taken += hpage_nr_pages(page);
  1044. break;
  1045. case -EBUSY:
  1046. /* else it is being freed elsewhere */
  1047. list_move(&page->lru, src);
  1048. continue;
  1049. default:
  1050. BUG();
  1051. }
  1052. if (!order)
  1053. continue;
  1054. /*
  1055. * Attempt to take all pages in the order aligned region
  1056. * surrounding the tag page. Only take those pages of
  1057. * the same active state as that tag page. We may safely
  1058. * round the target page pfn down to the requested order
  1059. * as the mem_map is guaranteed valid out to MAX_ORDER,
  1060. * where that page is in a different zone we will detect
  1061. * it from its zone id and abort this block scan.
  1062. */
  1063. zone_id = page_zone_id(page);
  1064. page_pfn = page_to_pfn(page);
  1065. pfn = page_pfn & ~((1 << order) - 1);
  1066. end_pfn = pfn + (1 << order);
  1067. for (; pfn < end_pfn; pfn++) {
  1068. struct page *cursor_page;
  1069. /* The target page is in the block, ignore it. */
  1070. if (unlikely(pfn == page_pfn))
  1071. continue;
  1072. /* Avoid holes within the zone. */
  1073. if (unlikely(!pfn_valid_within(pfn)))
  1074. break;
  1075. cursor_page = pfn_to_page(pfn);
  1076. /* Check that we have not crossed a zone boundary. */
  1077. if (unlikely(page_zone_id(cursor_page) != zone_id))
  1078. break;
  1079. /*
  1080. * If we don't have enough swap space, reclaiming of
  1081. * anon page which don't already have a swap slot is
  1082. * pointless.
  1083. */
  1084. if (nr_swap_pages <= 0 && PageSwapBacked(cursor_page) &&
  1085. !PageSwapCache(cursor_page))
  1086. break;
  1087. if (__isolate_lru_page(cursor_page, mode, file) == 0) {
  1088. unsigned int isolated_pages;
  1089. mem_cgroup_lru_del(cursor_page);
  1090. list_move(&cursor_page->lru, dst);
  1091. isolated_pages = hpage_nr_pages(cursor_page);
  1092. nr_taken += isolated_pages;
  1093. nr_lumpy_taken += isolated_pages;
  1094. if (PageDirty(cursor_page))
  1095. nr_lumpy_dirty += isolated_pages;
  1096. scan++;
  1097. pfn += isolated_pages - 1;
  1098. } else {
  1099. /*
  1100. * Check if the page is freed already.
  1101. *
  1102. * We can't use page_count() as that
  1103. * requires compound_head and we don't
  1104. * have a pin on the page here. If a
  1105. * page is tail, we may or may not
  1106. * have isolated the head, so assume
  1107. * it's not free, it'd be tricky to
  1108. * track the head status without a
  1109. * page pin.
  1110. */
  1111. if (!PageTail(cursor_page) &&
  1112. !atomic_read(&cursor_page->_count))
  1113. continue;
  1114. break;
  1115. }
  1116. }
  1117. /* If we break out of the loop above, lumpy reclaim failed */
  1118. if (pfn < end_pfn)
  1119. nr_lumpy_failed++;
  1120. }
  1121. *nr_scanned = scan;
  1122. trace_mm_vmscan_lru_isolate(order,
  1123. nr_to_scan, scan,
  1124. nr_taken,
  1125. nr_lumpy_taken, nr_lumpy_dirty, nr_lumpy_failed,
  1126. mode, file);
  1127. return nr_taken;
  1128. }
  1129. /**
  1130. * isolate_lru_page - tries to isolate a page from its LRU list
  1131. * @page: page to isolate from its LRU list
  1132. *
  1133. * Isolates a @page from an LRU list, clears PageLRU and adjusts the
  1134. * vmstat statistic corresponding to whatever LRU list the page was on.
  1135. *
  1136. * Returns 0 if the page was removed from an LRU list.
  1137. * Returns -EBUSY if the page was not on an LRU list.
  1138. *
  1139. * The returned page will have PageLRU() cleared. If it was found on
  1140. * the active list, it will have PageActive set. If it was found on
  1141. * the unevictable list, it will have the PageUnevictable bit set. That flag
  1142. * may need to be cleared by the caller before letting the page go.
  1143. *
  1144. * The vmstat statistic corresponding to the list on which the page was
  1145. * found will be decremented.
  1146. *
  1147. * Restrictions:
  1148. * (1) Must be called with an elevated refcount on the page. This is a
  1149. * fundamentnal difference from isolate_lru_pages (which is called
  1150. * without a stable reference).
  1151. * (2) the lru_lock must not be held.
  1152. * (3) interrupts must be enabled.
  1153. */
  1154. int isolate_lru_page(struct page *page)
  1155. {
  1156. int ret = -EBUSY;
  1157. VM_BUG_ON(!page_count(page));
  1158. if (PageLRU(page)) {
  1159. struct zone *zone = page_zone(page);
  1160. spin_lock_irq(&zone->lru_lock);
  1161. if (PageLRU(page)) {
  1162. int lru = page_lru(page);
  1163. ret = 0;
  1164. get_page(page);
  1165. ClearPageLRU(page);
  1166. del_page_from_lru_list(zone, page, lru);
  1167. }
  1168. spin_unlock_irq(&zone->lru_lock);
  1169. }
  1170. return ret;
  1171. }
  1172. /*
  1173. * Are there way too many processes in the direct reclaim path already?
  1174. */
  1175. static int too_many_isolated(struct zone *zone, int file,
  1176. struct scan_control *sc)
  1177. {
  1178. unsigned long inactive, isolated;
  1179. if (current_is_kswapd())
  1180. return 0;
  1181. if (!global_reclaim(sc))
  1182. return 0;
  1183. if (file) {
  1184. inactive = zone_page_state(zone, NR_INACTIVE_FILE);
  1185. isolated = zone_page_state(zone, NR_ISOLATED_FILE);
  1186. } else {
  1187. inactive = zone_page_state(zone, NR_INACTIVE_ANON);
  1188. isolated = zone_page_state(zone, NR_ISOLATED_ANON);
  1189. }
  1190. return isolated > inactive;
  1191. }
  1192. static noinline_for_stack void
  1193. putback_inactive_pages(struct mem_cgroup_zone *mz,
  1194. struct list_head *page_list)
  1195. {
  1196. struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(mz);
  1197. struct zone *zone = mz->zone;
  1198. LIST_HEAD(pages_to_free);
  1199. /*
  1200. * Put back any unfreeable pages.
  1201. */
  1202. while (!list_empty(page_list)) {
  1203. struct page *page = lru_to_page(page_list);
  1204. int lru;
  1205. VM_BUG_ON(PageLRU(page));
  1206. list_del(&page->lru);
  1207. if (unlikely(!page_evictable(page, NULL))) {
  1208. spin_unlock_irq(&zone->lru_lock);
  1209. putback_lru_page(page);
  1210. spin_lock_irq(&zone->lru_lock);
  1211. continue;
  1212. }
  1213. SetPageLRU(page);
  1214. lru = page_lru(page);
  1215. add_page_to_lru_list(zone, page, lru);
  1216. if (is_active_lru(lru)) {
  1217. int file = is_file_lru(lru);
  1218. int numpages = hpage_nr_pages(page);
  1219. reclaim_stat->recent_rotated[file] += numpages;
  1220. }
  1221. if (put_page_testzero(page)) {
  1222. __ClearPageLRU(page);
  1223. __ClearPageActive(page);
  1224. del_page_from_lru_list(zone, page, lru);
  1225. if (unlikely(PageCompound(page))) {
  1226. spin_unlock_irq(&zone->lru_lock);
  1227. (*get_compound_page_dtor(page))(page);
  1228. spin_lock_irq(&zone->lru_lock);
  1229. } else
  1230. list_add(&page->lru, &pages_to_free);
  1231. }
  1232. }
  1233. /*
  1234. * To save our caller's stack, now use input list for pages to free.
  1235. */
  1236. list_splice(&pages_to_free, page_list);
  1237. }
  1238. static noinline_for_stack void
  1239. update_isolated_counts(struct mem_cgroup_zone *mz,
  1240. struct list_head *page_list,
  1241. unsigned long *nr_anon,
  1242. unsigned long *nr_file)
  1243. {
  1244. struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(mz);
  1245. struct zone *zone = mz->zone;
  1246. unsigned int count[NR_LRU_LISTS] = { 0, };
  1247. unsigned long nr_active = 0;
  1248. struct page *page;
  1249. int lru;
  1250. /*
  1251. * Count pages and clear active flags
  1252. */
  1253. list_for_each_entry(page, page_list, lru) {
  1254. int numpages = hpage_nr_pages(page);
  1255. lru = page_lru_base_type(page);
  1256. if (PageActive(page)) {
  1257. lru += LRU_ACTIVE;
  1258. ClearPageActive(page);
  1259. nr_active += numpages;
  1260. }
  1261. count[lru] += numpages;
  1262. }
  1263. __count_vm_events(PGDEACTIVATE, nr_active);
  1264. __mod_zone_page_state(zone, NR_ACTIVE_FILE,
  1265. -count[LRU_ACTIVE_FILE]);
  1266. __mod_zone_page_state(zone, NR_INACTIVE_FILE,
  1267. -count[LRU_INACTIVE_FILE]);
  1268. __mod_zone_page_state(zone, NR_ACTIVE_ANON,
  1269. -count[LRU_ACTIVE_ANON]);
  1270. __mod_zone_page_state(zone, NR_INACTIVE_ANON,
  1271. -count[LRU_INACTIVE_ANON]);
  1272. *nr_anon = count[LRU_ACTIVE_ANON] + count[LRU_INACTIVE_ANON];
  1273. *nr_file = count[LRU_ACTIVE_FILE] + count[LRU_INACTIVE_FILE];
  1274. reclaim_stat->recent_scanned[0] += *nr_anon;
  1275. reclaim_stat->recent_scanned[1] += *nr_file;
  1276. }
  1277. /*
  1278. * Returns true if a direct reclaim should wait on pages under writeback.
  1279. *
  1280. * If we are direct reclaiming for contiguous pages and we do not reclaim
  1281. * everything in the list, try again and wait for writeback IO to complete.
  1282. * This will stall high-order allocations noticeably. Only do that when really
  1283. * need to free the pages under high memory pressure.
  1284. */
  1285. static inline bool should_reclaim_stall(unsigned long nr_taken,
  1286. unsigned long nr_freed,
  1287. int priority,
  1288. struct scan_control *sc)
  1289. {
  1290. int lumpy_stall_priority;
  1291. /* kswapd should not stall on sync IO */
  1292. if (current_is_kswapd())
  1293. return false;
  1294. /* Only stall on lumpy reclaim */
  1295. if (sc->reclaim_mode & RECLAIM_MODE_SINGLE)
  1296. return false;
  1297. /* If we have reclaimed everything on the isolated list, no stall */
  1298. if (nr_freed == nr_taken)
  1299. return false;
  1300. /*
  1301. * For high-order allocations, there are two stall thresholds.
  1302. * High-cost allocations stall immediately where as lower
  1303. * order allocations such as stacks require the scanning
  1304. * priority to be much higher before stalling.
  1305. */
  1306. if (sc->order > PAGE_ALLOC_COSTLY_ORDER)
  1307. lumpy_stall_priority = DEF_PRIORITY;
  1308. else
  1309. lumpy_stall_priority = DEF_PRIORITY / 3;
  1310. return priority <= lumpy_stall_priority;
  1311. }
  1312. /*
  1313. * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
  1314. * of reclaimed pages
  1315. */
  1316. static noinline_for_stack unsigned long
  1317. shrink_inactive_list(unsigned long nr_to_scan, struct mem_cgroup_zone *mz,
  1318. struct scan_control *sc, int priority, int file)
  1319. {
  1320. LIST_HEAD(page_list);
  1321. unsigned long nr_scanned;
  1322. unsigned long nr_reclaimed = 0;
  1323. unsigned long nr_taken;
  1324. unsigned long nr_anon;
  1325. unsigned long nr_file;
  1326. unsigned long nr_dirty = 0;
  1327. unsigned long nr_writeback = 0;
  1328. isolate_mode_t reclaim_mode = ISOLATE_INACTIVE;
  1329. struct zone *zone = mz->zone;
  1330. while (unlikely(too_many_isolated(zone, file, sc))) {
  1331. congestion_wait(BLK_RW_ASYNC, HZ/10);
  1332. /* We are about to die and free our memory. Return now. */
  1333. if (fatal_signal_pending(current))
  1334. return SWAP_CLUSTER_MAX;
  1335. }
  1336. set_reclaim_mode(priority, sc, false);
  1337. if (sc->reclaim_mode & RECLAIM_MODE_LUMPYRECLAIM)
  1338. reclaim_mode |= ISOLATE_ACTIVE;
  1339. lru_add_drain();
  1340. if (!sc->may_unmap)
  1341. reclaim_mode |= ISOLATE_UNMAPPED;
  1342. if (!sc->may_writepage)
  1343. reclaim_mode |= ISOLATE_CLEAN;
  1344. spin_lock_irq(&zone->lru_lock);
  1345. nr_taken = isolate_lru_pages(nr_to_scan, mz, &page_list,
  1346. &nr_scanned, sc->order,
  1347. reclaim_mode, 0, file);
  1348. if (global_reclaim(sc)) {
  1349. zone->pages_scanned += nr_scanned;
  1350. if (current_is_kswapd())
  1351. __count_zone_vm_events(PGSCAN_KSWAPD, zone,
  1352. nr_scanned);
  1353. else
  1354. __count_zone_vm_events(PGSCAN_DIRECT, zone,
  1355. nr_scanned);
  1356. }
  1357. if (nr_taken == 0) {
  1358. spin_unlock_irq(&zone->lru_lock);
  1359. return 0;
  1360. }
  1361. update_isolated_counts(mz, &page_list, &nr_anon, &nr_file);
  1362. __mod_zone_page_state(zone, NR_ISOLATED_ANON, nr_anon);
  1363. __mod_zone_page_state(zone, NR_ISOLATED_FILE, nr_file);
  1364. spin_unlock_irq(&zone->lru_lock);
  1365. nr_reclaimed = shrink_page_list(&page_list, mz, sc, priority,
  1366. &nr_dirty, &nr_writeback);
  1367. /* Check if we should syncronously wait for writeback */
  1368. if (should_reclaim_stall(nr_taken, nr_reclaimed, priority, sc)) {
  1369. set_reclaim_mode(priority, sc, true);
  1370. nr_reclaimed += shrink_page_list(&page_list, mz, sc,
  1371. priority, &nr_dirty, &nr_writeback);
  1372. }
  1373. spin_lock_irq(&zone->lru_lock);
  1374. if (current_is_kswapd())
  1375. __count_vm_events(KSWAPD_STEAL, nr_reclaimed);
  1376. __count_zone_vm_events(PGSTEAL, zone, nr_reclaimed);
  1377. putback_inactive_pages(mz, &page_list);
  1378. __mod_zone_page_state(zone, NR_ISOLATED_ANON, -nr_anon);
  1379. __mod_zone_page_state(zone, NR_ISOLATED_FILE, -nr_file);
  1380. spin_unlock_irq(&zone->lru_lock);
  1381. free_hot_cold_page_list(&page_list, 1);
  1382. /*
  1383. * If reclaim is isolating dirty pages under writeback, it implies
  1384. * that the long-lived page allocation rate is exceeding the page
  1385. * laundering rate. Either the global limits are not being effective
  1386. * at throttling processes due to the page distribution throughout
  1387. * zones or there is heavy usage of a slow backing device. The
  1388. * only option is to throttle from reclaim context which is not ideal
  1389. * as there is no guarantee the dirtying process is throttled in the
  1390. * same way balance_dirty_pages() manages.
  1391. *
  1392. * This scales the number of dirty pages that must be under writeback
  1393. * before throttling depending on priority. It is a simple backoff
  1394. * function that has the most effect in the range DEF_PRIORITY to
  1395. * DEF_PRIORITY-2 which is the priority reclaim is considered to be
  1396. * in trouble and reclaim is considered to be in trouble.
  1397. *
  1398. * DEF_PRIORITY 100% isolated pages must be PageWriteback to throttle
  1399. * DEF_PRIORITY-1 50% must be PageWriteback
  1400. * DEF_PRIORITY-2 25% must be PageWriteback, kswapd in trouble
  1401. * ...
  1402. * DEF_PRIORITY-6 For SWAP_CLUSTER_MAX isolated pages, throttle if any
  1403. * isolated page is PageWriteback
  1404. */
  1405. if (nr_writeback && nr_writeback >= (nr_taken >> (DEF_PRIORITY-priority)))
  1406. wait_iff_congested(zone, BLK_RW_ASYNC, HZ/10);
  1407. trace_mm_vmscan_lru_shrink_inactive(zone->zone_pgdat->node_id,
  1408. zone_idx(zone),
  1409. nr_scanned, nr_reclaimed,
  1410. priority,
  1411. trace_shrink_flags(file, sc->reclaim_mode));
  1412. return nr_reclaimed;
  1413. }
  1414. /*
  1415. * This moves pages from the active list to the inactive list.
  1416. *
  1417. * We move them the other way if the page is referenced by one or more
  1418. * processes, from rmap.
  1419. *
  1420. * If the pages are mostly unmapped, the processing is fast and it is
  1421. * appropriate to hold zone->lru_lock across the whole operation. But if
  1422. * the pages are mapped, the processing is slow (page_referenced()) so we
  1423. * should drop zone->lru_lock around each page. It's impossible to balance
  1424. * this, so instead we remove the pages from the LRU while processing them.
  1425. * It is safe to rely on PG_active against the non-LRU pages in here because
  1426. * nobody will play with that bit on a non-LRU page.
  1427. *
  1428. * The downside is that we have to touch page->_count against each page.
  1429. * But we had to alter page->flags anyway.
  1430. */
  1431. static void move_active_pages_to_lru(struct zone *zone,
  1432. struct list_head *list,
  1433. struct list_head *pages_to_free,
  1434. enum lru_list lru)
  1435. {
  1436. unsigned long pgmoved = 0;
  1437. struct page *page;
  1438. if (buffer_heads_over_limit) {
  1439. spin_unlock_irq(&zone->lru_lock);
  1440. list_for_each_entry(page, list, lru) {
  1441. if (page_has_private(page) && trylock_page(page)) {
  1442. if (page_has_private(page))
  1443. try_to_release_page(page, 0);
  1444. unlock_page(page);
  1445. }
  1446. }
  1447. spin_lock_irq(&zone->lru_lock);
  1448. }
  1449. while (!list_empty(list)) {
  1450. struct lruvec *lruvec;
  1451. page = lru_to_page(list);
  1452. VM_BUG_ON(PageLRU(page));
  1453. SetPageLRU(page);
  1454. lruvec = mem_cgroup_lru_add_list(zone, page, lru);
  1455. list_move(&page->lru, &lruvec->lists[lru]);
  1456. pgmoved += hpage_nr_pages(page);
  1457. if (put_page_testzero(page)) {
  1458. __ClearPageLRU(page);
  1459. __ClearPageActive(page);
  1460. del_page_from_lru_list(zone, page, lru);
  1461. if (unlikely(PageCompound(page))) {
  1462. spin_unlock_irq(&zone->lru_lock);
  1463. (*get_compound_page_dtor(page))(page);
  1464. spin_lock_irq(&zone->lru_lock);
  1465. } else
  1466. list_add(&page->lru, pages_to_free);
  1467. }
  1468. }
  1469. __mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved);
  1470. if (!is_active_lru(lru))
  1471. __count_vm_events(PGDEACTIVATE, pgmoved);
  1472. }
  1473. static void shrink_active_list(unsigned long nr_to_scan,
  1474. struct mem_cgroup_zone *mz,
  1475. struct scan_control *sc,
  1476. int priority, int file)
  1477. {
  1478. unsigned long nr_taken;
  1479. unsigned long nr_scanned;
  1480. unsigned long vm_flags;
  1481. LIST_HEAD(l_hold); /* The pages which were snipped off */
  1482. LIST_HEAD(l_active);
  1483. LIST_HEAD(l_inactive);
  1484. struct page *page;
  1485. struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(mz);
  1486. unsigned long nr_rotated = 0;
  1487. isolate_mode_t reclaim_mode = ISOLATE_ACTIVE;
  1488. struct zone *zone = mz->zone;
  1489. lru_add_drain();
  1490. if (!sc->may_unmap)
  1491. reclaim_mode |= ISOLATE_UNMAPPED;
  1492. if (!sc->may_writepage)
  1493. reclaim_mode |= ISOLATE_CLEAN;
  1494. spin_lock_irq(&zone->lru_lock);
  1495. nr_taken = isolate_lru_pages(nr_to_scan, mz, &l_hold,
  1496. &nr_scanned, sc->order,
  1497. reclaim_mode, 1, file);
  1498. if (global_reclaim(sc))
  1499. zone->pages_scanned += nr_scanned;
  1500. reclaim_stat->recent_scanned[file] += nr_taken;
  1501. __count_zone_vm_events(PGREFILL, zone, nr_scanned);
  1502. if (file)
  1503. __mod_zone_page_state(zone, NR_ACTIVE_FILE, -nr_taken);
  1504. else
  1505. __mod_zone_page_state(zone, NR_ACTIVE_ANON, -nr_taken);
  1506. __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken);
  1507. spin_unlock_irq(&zone->lru_lock);
  1508. while (!list_empty(&l_hold)) {
  1509. cond_resched();
  1510. page = lru_to_page(&l_hold);
  1511. list_del(&page->lru);
  1512. if (unlikely(!page_evictable(page, NULL))) {
  1513. putback_lru_page(page);
  1514. continue;
  1515. }
  1516. if (page_referenced(page, 0, mz->mem_cgroup, &vm_flags)) {
  1517. nr_rotated += hpage_nr_pages(page);
  1518. /*
  1519. * Identify referenced, file-backed active pages and
  1520. * give them one more trip around the active list. So
  1521. * that executable code get better chances to stay in
  1522. * memory under moderate memory pressure. Anon pages
  1523. * are not likely to be evicted by use-once streaming
  1524. * IO, plus JVM can create lots of anon VM_EXEC pages,
  1525. * so we ignore them here.
  1526. */
  1527. if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) {
  1528. list_add(&page->lru, &l_active);
  1529. continue;
  1530. }
  1531. }
  1532. ClearPageActive(page); /* we are de-activating */
  1533. list_add(&page->lru, &l_inactive);
  1534. }
  1535. /*
  1536. * Move pages back to the lru list.
  1537. */
  1538. spin_lock_irq(&zone->lru_lock);
  1539. /*
  1540. * Count referenced pages from currently used mappings as rotated,
  1541. * even though only some of them are actually re-activated. This
  1542. * helps balance scan pressure between file and anonymous pages in
  1543. * get_scan_ratio.
  1544. */
  1545. reclaim_stat->recent_rotated[file] += nr_rotated;
  1546. move_active_pages_to_lru(zone, &l_active, &l_hold,
  1547. LRU_ACTIVE + file * LRU_FILE);
  1548. move_active_pages_to_lru(zone, &l_inactive, &l_hold,
  1549. LRU_BASE + file * LRU_FILE);
  1550. __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken);
  1551. spin_unlock_irq(&zone->lru_lock);
  1552. free_hot_cold_page_list(&l_hold, 1);
  1553. }
  1554. #ifdef CONFIG_SWAP
  1555. static int inactive_anon_is_low_global(struct zone *zone)
  1556. {
  1557. unsigned long active, inactive;
  1558. active = zone_page_state(zone, NR_ACTIVE_ANON);
  1559. inactive = zone_page_state(zone, NR_INACTIVE_ANON);
  1560. if (inactive * zone->inactive_ratio < active)
  1561. return 1;
  1562. return 0;
  1563. }
  1564. /**
  1565. * inactive_anon_is_low - check if anonymous pages need to be deactivated
  1566. * @zone: zone to check
  1567. * @sc: scan control of this context
  1568. *
  1569. * Returns true if the zone does not have enough inactive anon pages,
  1570. * meaning some active anon pages need to be deactivated.
  1571. */
  1572. static int inactive_anon_is_low(struct mem_cgroup_zone *mz)
  1573. {
  1574. /*
  1575. * If we don't have swap space, anonymous page deactivation
  1576. * is pointless.
  1577. */
  1578. if (!total_swap_pages)
  1579. return 0;
  1580. if (!scanning_global_lru(mz))
  1581. return mem_cgroup_inactive_anon_is_low(mz->mem_cgroup,
  1582. mz->zone);
  1583. return inactive_anon_is_low_global(mz->zone);
  1584. }
  1585. #else
  1586. static inline int inactive_anon_is_low(struct mem_cgroup_zone *mz)
  1587. {
  1588. return 0;
  1589. }
  1590. #endif
  1591. static int inactive_file_is_low_global(struct zone *zone)
  1592. {
  1593. unsigned long active, inactive;
  1594. active = zone_page_state(zone, NR_ACTIVE_FILE);
  1595. inactive = zone_page_state(zone, NR_INACTIVE_FILE);
  1596. return (active > inactive);
  1597. }
  1598. /**
  1599. * inactive_file_is_low - check if file pages need to be deactivated
  1600. * @mz: memory cgroup and zone to check
  1601. *
  1602. * When the system is doing streaming IO, memory pressure here
  1603. * ensures that active file pages get deactivated, until more
  1604. * than half of the file pages are on the inactive list.
  1605. *
  1606. * Once we get to that situation, protect the system's working
  1607. * set from being evicted by disabling active file page aging.
  1608. *
  1609. * This uses a different ratio than the anonymous pages, because
  1610. * the page cache uses a use-once replacement algorithm.
  1611. */
  1612. static int inactive_file_is_low(struct mem_cgroup_zone *mz)
  1613. {
  1614. if (!scanning_global_lru(mz))
  1615. return mem_cgroup_inactive_file_is_low(mz->mem_cgroup,
  1616. mz->zone);
  1617. return inactive_file_is_low_global(mz->zone);
  1618. }
  1619. static int inactive_list_is_low(struct mem_cgroup_zone *mz, int file)
  1620. {
  1621. if (file)
  1622. return inactive_file_is_low(mz);
  1623. else
  1624. return inactive_anon_is_low(mz);
  1625. }
  1626. static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
  1627. struct mem_cgroup_zone *mz,
  1628. struct scan_control *sc, int priority)
  1629. {
  1630. int file = is_file_lru(lru);
  1631. if (is_active_lru(lru)) {
  1632. if (inactive_list_is_low(mz, file))
  1633. shrink_active_list(nr_to_scan, mz, sc, priority, file);
  1634. return 0;
  1635. }
  1636. return shrink_inactive_list(nr_to_scan, mz, sc, priority, file);
  1637. }
  1638. static int vmscan_swappiness(struct mem_cgroup_zone *mz,
  1639. struct scan_control *sc)
  1640. {
  1641. if (global_reclaim(sc))
  1642. return vm_swappiness;
  1643. return mem_cgroup_swappiness(mz->mem_cgroup);
  1644. }
  1645. /*
  1646. * Determine how aggressively the anon and file LRU lists should be
  1647. * scanned. The relative value of each set of LRU lists is determined
  1648. * by looking at the fraction of the pages scanned we did rotate back
  1649. * onto the active list instead of evict.
  1650. *
  1651. * nr[0] = anon pages to scan; nr[1] = file pages to scan
  1652. */
  1653. static void get_scan_count(struct mem_cgroup_zone *mz, struct scan_control *sc,
  1654. unsigned long *nr, int priority)
  1655. {
  1656. unsigned long anon, file, free;
  1657. unsigned long anon_prio, file_prio;
  1658. unsigned long ap, fp;
  1659. struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(mz);
  1660. u64 fraction[2], denominator;
  1661. enum lru_list lru;
  1662. int noswap = 0;
  1663. bool force_scan = false;
  1664. /*
  1665. * If the zone or memcg is small, nr[l] can be 0. This
  1666. * results in no scanning on this priority and a potential
  1667. * priority drop. Global direct reclaim can go to the next
  1668. * zone and tends to have no problems. Global kswapd is for
  1669. * zone balancing and it needs to scan a minimum amount. When
  1670. * reclaiming for a memcg, a priority drop can cause high
  1671. * latencies, so it's better to scan a minimum amount there as
  1672. * well.
  1673. */
  1674. if (current_is_kswapd() && mz->zone->all_unreclaimable)
  1675. force_scan = true;
  1676. if (!global_reclaim(sc))
  1677. force_scan = true;
  1678. /* If we have no swap space, do not bother scanning anon pages. */
  1679. if (!sc->may_swap || (nr_swap_pages <= 0)) {
  1680. noswap = 1;
  1681. fraction[0] = 0;
  1682. fraction[1] = 1;
  1683. denominator = 1;
  1684. goto out;
  1685. }
  1686. anon = zone_nr_lru_pages(mz, LRU_ACTIVE_ANON) +
  1687. zone_nr_lru_pages(mz, LRU_INACTIVE_ANON);
  1688. file = zone_nr_lru_pages(mz, LRU_ACTIVE_FILE) +
  1689. zone_nr_lru_pages(mz, LRU_INACTIVE_FILE);
  1690. if (global_reclaim(sc)) {
  1691. free = zone_page_state(mz->zone, NR_FREE_PAGES);
  1692. /* If we have very few page cache pages,
  1693. force-scan anon pages. */
  1694. if (unlikely(file + free <= high_wmark_pages(mz->zone))) {
  1695. fraction[0] = 1;
  1696. fraction[1] = 0;
  1697. denominator = 1;
  1698. goto out;
  1699. }
  1700. }
  1701. /*
  1702. * With swappiness at 100, anonymous and file have the same priority.
  1703. * This scanning priority is essentially the inverse of IO cost.
  1704. */
  1705. anon_prio = vmscan_swappiness(mz, sc);
  1706. file_prio = 200 - vmscan_swappiness(mz, sc);
  1707. /*
  1708. * OK, so we have swap space and a fair amount of page cache
  1709. * pages. We use the recently rotated / recently scanned
  1710. * ratios to determine how valuable each cache is.
  1711. *
  1712. * Because workloads change over time (and to avoid overflow)
  1713. * we keep these statistics as a floating average, which ends
  1714. * up weighing recent references more than old ones.
  1715. *
  1716. * anon in [0], file in [1]
  1717. */
  1718. spin_lock_irq(&mz->zone->lru_lock);
  1719. if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) {
  1720. reclaim_stat->recent_scanned[0] /= 2;
  1721. reclaim_stat->recent_rotated[0] /= 2;
  1722. }
  1723. if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) {
  1724. reclaim_stat->recent_scanned[1] /= 2;
  1725. reclaim_stat->recent_rotated[1] /= 2;
  1726. }
  1727. /*
  1728. * The amount of pressure on anon vs file pages is inversely
  1729. * proportional to the fraction of recently scanned pages on
  1730. * each list that were recently referenced and in active use.
  1731. */
  1732. ap = (anon_prio + 1) * (reclaim_stat->recent_scanned[0] + 1);
  1733. ap /= reclaim_stat->recent_rotated[0] + 1;
  1734. fp = (file_prio + 1) * (reclaim_stat->recent_scanned[1] + 1);
  1735. fp /= reclaim_stat->recent_rotated[1] + 1;
  1736. spin_unlock_irq(&mz->zone->lru_lock);
  1737. fraction[0] = ap;
  1738. fraction[1] = fp;
  1739. denominator = ap + fp + 1;
  1740. out:
  1741. for_each_evictable_lru(lru) {
  1742. int file = is_file_lru(lru);
  1743. unsigned long scan;
  1744. scan = zone_nr_lru_pages(mz, lru);
  1745. if (priority || noswap) {
  1746. scan >>= priority;
  1747. if (!scan && force_scan)
  1748. scan = SWAP_CLUSTER_MAX;
  1749. scan = div64_u64(scan * fraction[file], denominator);
  1750. }
  1751. nr[lru] = scan;
  1752. }
  1753. }
  1754. /*
  1755. * Reclaim/compaction depends on a number of pages being freed. To avoid
  1756. * disruption to the system, a small number of order-0 pages continue to be
  1757. * rotated and reclaimed in the normal fashion. However, by the time we get
  1758. * back to the allocator and call try_to_compact_zone(), we ensure that
  1759. * there are enough free pages for it to be likely successful
  1760. */
  1761. static inline bool should_continue_reclaim(struct mem_cgroup_zone *mz,
  1762. unsigned long nr_reclaimed,
  1763. unsigned long nr_scanned,
  1764. struct scan_control *sc)
  1765. {
  1766. unsigned long pages_for_compaction;
  1767. unsigned long inactive_lru_pages;
  1768. /* If not in reclaim/compaction mode, stop */
  1769. if (!(sc->reclaim_mode & RECLAIM_MODE_COMPACTION))
  1770. return false;
  1771. /* Consider stopping depending on scan and reclaim activity */
  1772. if (sc->gfp_mask & __GFP_REPEAT) {
  1773. /*
  1774. * For __GFP_REPEAT allocations, stop reclaiming if the
  1775. * full LRU list has been scanned and we are still failing
  1776. * to reclaim pages. This full LRU scan is potentially
  1777. * expensive but a __GFP_REPEAT caller really wants to succeed
  1778. */
  1779. if (!nr_reclaimed && !nr_scanned)
  1780. return false;
  1781. } else {
  1782. /*
  1783. * For non-__GFP_REPEAT allocations which can presumably
  1784. * fail without consequence, stop if we failed to reclaim
  1785. * any pages from the last SWAP_CLUSTER_MAX number of
  1786. * pages that were scanned. This will return to the
  1787. * caller faster at the risk reclaim/compaction and
  1788. * the resulting allocation attempt fails
  1789. */
  1790. if (!nr_reclaimed)
  1791. return false;
  1792. }
  1793. /*
  1794. * If we have not reclaimed enough pages for compaction and the
  1795. * inactive lists are large enough, continue reclaiming
  1796. */
  1797. pages_for_compaction = (2UL << sc->order);
  1798. inactive_lru_pages = zone_nr_lru_pages(mz, LRU_INACTIVE_FILE);
  1799. if (nr_swap_pages > 0)
  1800. inactive_lru_pages += zone_nr_lru_pages(mz, LRU_INACTIVE_ANON);
  1801. if (sc->nr_reclaimed < pages_for_compaction &&
  1802. inactive_lru_pages > pages_for_compaction)
  1803. return true;
  1804. /* If compaction would go ahead or the allocation would succeed, stop */
  1805. switch (compaction_suitable(mz->zone, sc->order)) {
  1806. case COMPACT_PARTIAL:
  1807. case COMPACT_CONTINUE:
  1808. return false;
  1809. default:
  1810. return true;
  1811. }
  1812. }
  1813. /*
  1814. * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
  1815. */
  1816. static void shrink_mem_cgroup_zone(int priority, struct mem_cgroup_zone *mz,
  1817. struct scan_control *sc)
  1818. {
  1819. unsigned long nr[NR_LRU_LISTS];
  1820. unsigned long nr_to_scan;
  1821. enum lru_list lru;
  1822. unsigned long nr_reclaimed, nr_scanned;
  1823. unsigned long nr_to_reclaim = sc->nr_to_reclaim;
  1824. struct blk_plug plug;
  1825. restart:
  1826. nr_reclaimed = 0;
  1827. nr_scanned = sc->nr_scanned;
  1828. get_scan_count(mz, sc, nr, priority);
  1829. blk_start_plug(&plug);
  1830. while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] ||
  1831. nr[LRU_INACTIVE_FILE]) {
  1832. for_each_evictable_lru(lru) {
  1833. if (nr[lru]) {
  1834. nr_to_scan = min_t(unsigned long,
  1835. nr[lru], SWAP_CLUSTER_MAX);
  1836. nr[lru] -= nr_to_scan;
  1837. nr_reclaimed += shrink_list(lru, nr_to_scan,
  1838. mz, sc, priority);
  1839. }
  1840. }
  1841. /*
  1842. * On large memory systems, scan >> priority can become
  1843. * really large. This is fine for the starting priority;
  1844. * we want to put equal scanning pressure on each zone.
  1845. * However, if the VM has a harder time of freeing pages,
  1846. * with multiple processes reclaiming pages, the total
  1847. * freeing target can get unreasonably large.
  1848. */
  1849. if (nr_reclaimed >= nr_to_reclaim && priority < DEF_PRIORITY)
  1850. break;
  1851. }
  1852. blk_finish_plug(&plug);
  1853. sc->nr_reclaimed += nr_reclaimed;
  1854. /*
  1855. * Even if we did not try to evict anon pages at all, we want to
  1856. * rebalance the anon lru active/inactive ratio.
  1857. */
  1858. if (inactive_anon_is_low(mz))
  1859. shrink_active_list(SWAP_CLUSTER_MAX, mz, sc, priority, 0);
  1860. /* reclaim/compaction might need reclaim to continue */
  1861. if (should_continue_reclaim(mz, nr_reclaimed,
  1862. sc->nr_scanned - nr_scanned, sc))
  1863. goto restart;
  1864. throttle_vm_writeout(sc->gfp_mask);
  1865. }
  1866. static void shrink_zone(int priority, struct zone *zone,
  1867. struct scan_control *sc)
  1868. {
  1869. struct mem_cgroup *root = sc->target_mem_cgroup;
  1870. struct mem_cgroup_reclaim_cookie reclaim = {
  1871. .zone = zone,
  1872. .priority = priority,
  1873. };
  1874. struct mem_cgroup *memcg;
  1875. memcg = mem_cgroup_iter(root, NULL, &reclaim);
  1876. do {
  1877. struct mem_cgroup_zone mz = {
  1878. .mem_cgroup = memcg,
  1879. .zone = zone,
  1880. };
  1881. shrink_mem_cgroup_zone(priority, &mz, sc);
  1882. /*
  1883. * Limit reclaim has historically picked one memcg and
  1884. * scanned it with decreasing priority levels until
  1885. * nr_to_reclaim had been reclaimed. This priority
  1886. * cycle is thus over after a single memcg.
  1887. *
  1888. * Direct reclaim and kswapd, on the other hand, have
  1889. * to scan all memory cgroups to fulfill the overall
  1890. * scan target for the zone.
  1891. */
  1892. if (!global_reclaim(sc)) {
  1893. mem_cgroup_iter_break(root, memcg);
  1894. break;
  1895. }
  1896. memcg = mem_cgroup_iter(root, memcg, &reclaim);
  1897. } while (memcg);
  1898. }
  1899. /* Returns true if compaction should go ahead for a high-order request */
  1900. static inline bool compaction_ready(struct zone *zone, struct scan_control *sc)
  1901. {
  1902. unsigned long balance_gap, watermark;
  1903. bool watermark_ok;
  1904. /* Do not consider compaction for orders reclaim is meant to satisfy */
  1905. if (sc->order <= PAGE_ALLOC_COSTLY_ORDER)
  1906. return false;
  1907. /*
  1908. * Compaction takes time to run and there are potentially other
  1909. * callers using the pages just freed. Continue reclaiming until
  1910. * there is a buffer of free pages available to give compaction
  1911. * a reasonable chance of completing and allocating the page
  1912. */
  1913. balance_gap = min(low_wmark_pages(zone),
  1914. (zone->present_pages + KSWAPD_ZONE_BALANCE_GAP_RATIO-1) /
  1915. KSWAPD_ZONE_BALANCE_GAP_RATIO);
  1916. watermark = high_wmark_pages(zone) + balance_gap + (2UL << sc->order);
  1917. watermark_ok = zone_watermark_ok_safe(zone, 0, watermark, 0, 0);
  1918. /*
  1919. * If compaction is deferred, reclaim up to a point where
  1920. * compaction will have a chance of success when re-enabled
  1921. */
  1922. if (compaction_deferred(zone))
  1923. return watermark_ok;
  1924. /* If compaction is not ready to start, keep reclaiming */
  1925. if (!compaction_suitable(zone, sc->order))
  1926. return false;
  1927. return watermark_ok;
  1928. }
  1929. /*
  1930. * This is the direct reclaim path, for page-allocating processes. We only
  1931. * try to reclaim pages from zones which will satisfy the caller's allocation
  1932. * request.
  1933. *
  1934. * We reclaim from a zone even if that zone is over high_wmark_pages(zone).
  1935. * Because:
  1936. * a) The caller may be trying to free *extra* pages to satisfy a higher-order
  1937. * allocation or
  1938. * b) The target zone may be at high_wmark_pages(zone) but the lower zones
  1939. * must go *over* high_wmark_pages(zone) to satisfy the `incremental min'
  1940. * zone defense algorithm.
  1941. *
  1942. * If a zone is deemed to be full of pinned pages then just give it a light
  1943. * scan then give up on it.
  1944. *
  1945. * This function returns true if a zone is being reclaimed for a costly
  1946. * high-order allocation and compaction is ready to begin. This indicates to
  1947. * the caller that it should consider retrying the allocation instead of
  1948. * further reclaim.
  1949. */
  1950. static bool shrink_zones(int priority, struct zonelist *zonelist,
  1951. struct scan_control *sc)
  1952. {
  1953. struct zoneref *z;
  1954. struct zone *zone;
  1955. unsigned long nr_soft_reclaimed;
  1956. unsigned long nr_soft_scanned;
  1957. bool aborted_reclaim = false;
  1958. for_each_zone_zonelist_nodemask(zone, z, zonelist,
  1959. gfp_zone(sc->gfp_mask), sc->nodemask) {
  1960. if (!populated_zone(zone))
  1961. continue;
  1962. /*
  1963. * Take care memory controller reclaiming has small influence
  1964. * to global LRU.
  1965. */
  1966. if (global_reclaim(sc)) {
  1967. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1968. continue;
  1969. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  1970. continue; /* Let kswapd poll it */
  1971. if (COMPACTION_BUILD) {
  1972. /*
  1973. * If we already have plenty of memory free for
  1974. * compaction in this zone, don't free any more.
  1975. * Even though compaction is invoked for any
  1976. * non-zero order, only frequent costly order
  1977. * reclamation is disruptive enough to become a
  1978. * noticable problem, like transparent huge page
  1979. * allocations.
  1980. */
  1981. if (compaction_ready(zone, sc)) {
  1982. aborted_reclaim = true;
  1983. continue;
  1984. }
  1985. }
  1986. /*
  1987. * This steals pages from memory cgroups over softlimit
  1988. * and returns the number of reclaimed pages and
  1989. * scanned pages. This works for global memory pressure
  1990. * and balancing, not for a memcg's limit.
  1991. */
  1992. nr_soft_scanned = 0;
  1993. nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone,
  1994. sc->order, sc->gfp_mask,
  1995. &nr_soft_scanned);
  1996. sc->nr_reclaimed += nr_soft_reclaimed;
  1997. sc->nr_scanned += nr_soft_scanned;
  1998. /* need some check for avoid more shrink_zone() */
  1999. }
  2000. shrink_zone(priority, zone, sc);
  2001. }
  2002. return aborted_reclaim;
  2003. }
  2004. static bool zone_reclaimable(struct zone *zone)
  2005. {
  2006. return zone->pages_scanned < zone_reclaimable_pages(zone) * 6;
  2007. }
  2008. /* All zones in zonelist are unreclaimable? */
  2009. static bool all_unreclaimable(struct zonelist *zonelist,
  2010. struct scan_control *sc)
  2011. {
  2012. struct zoneref *z;
  2013. struct zone *zone;
  2014. for_each_zone_zonelist_nodemask(zone, z, zonelist,
  2015. gfp_zone(sc->gfp_mask), sc->nodemask) {
  2016. if (!populated_zone(zone))
  2017. continue;
  2018. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  2019. continue;
  2020. if (!zone->all_unreclaimable)
  2021. return false;
  2022. }
  2023. return true;
  2024. }
  2025. /*
  2026. * This is the main entry point to direct page reclaim.
  2027. *
  2028. * If a full scan of the inactive list fails to free enough memory then we
  2029. * are "out of memory" and something needs to be killed.
  2030. *
  2031. * If the caller is !__GFP_FS then the probability of a failure is reasonably
  2032. * high - the zone may be full of dirty or under-writeback pages, which this
  2033. * caller can't do much about. We kick the writeback threads and take explicit
  2034. * naps in the hope that some of these pages can be written. But if the
  2035. * allocating task holds filesystem locks which prevent writeout this might not
  2036. * work, and the allocation attempt will fail.
  2037. *
  2038. * returns: 0, if no pages reclaimed
  2039. * else, the number of pages reclaimed
  2040. */
  2041. static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
  2042. struct scan_control *sc,
  2043. struct shrink_control *shrink)
  2044. {
  2045. int priority;
  2046. unsigned long total_scanned = 0;
  2047. struct reclaim_state *reclaim_state = current->reclaim_state;
  2048. struct zoneref *z;
  2049. struct zone *zone;
  2050. unsigned long writeback_threshold;
  2051. bool aborted_reclaim;
  2052. get_mems_allowed();
  2053. delayacct_freepages_start();
  2054. if (global_reclaim(sc))
  2055. count_vm_event(ALLOCSTALL);
  2056. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  2057. sc->nr_scanned = 0;
  2058. if (!priority)
  2059. disable_swap_token(sc->target_mem_cgroup);
  2060. aborted_reclaim = shrink_zones(priority, zonelist, sc);
  2061. /*
  2062. * Don't shrink slabs when reclaiming memory from
  2063. * over limit cgroups
  2064. */
  2065. if (global_reclaim(sc)) {
  2066. unsigned long lru_pages = 0;
  2067. for_each_zone_zonelist(zone, z, zonelist,
  2068. gfp_zone(sc->gfp_mask)) {
  2069. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  2070. continue;
  2071. lru_pages += zone_reclaimable_pages(zone);
  2072. }
  2073. shrink_slab(shrink, sc->nr_scanned, lru_pages);
  2074. if (reclaim_state) {
  2075. sc->nr_reclaimed += reclaim_state->reclaimed_slab;
  2076. reclaim_state->reclaimed_slab = 0;
  2077. }
  2078. }
  2079. total_scanned += sc->nr_scanned;
  2080. if (sc->nr_reclaimed >= sc->nr_to_reclaim)
  2081. goto out;
  2082. /*
  2083. * Try to write back as many pages as we just scanned. This
  2084. * tends to cause slow streaming writers to write data to the
  2085. * disk smoothly, at the dirtying rate, which is nice. But
  2086. * that's undesirable in laptop mode, where we *want* lumpy
  2087. * writeout. So in laptop mode, write out the whole world.
  2088. */
  2089. writeback_threshold = sc->nr_to_reclaim + sc->nr_to_reclaim / 2;
  2090. if (total_scanned > writeback_threshold) {
  2091. wakeup_flusher_threads(laptop_mode ? 0 : total_scanned,
  2092. WB_REASON_TRY_TO_FREE_PAGES);
  2093. sc->may_writepage = 1;
  2094. }
  2095. /* Take a nap, wait for some writeback to complete */
  2096. if (!sc->hibernation_mode && sc->nr_scanned &&
  2097. priority < DEF_PRIORITY - 2) {
  2098. struct zone *preferred_zone;
  2099. first_zones_zonelist(zonelist, gfp_zone(sc->gfp_mask),
  2100. &cpuset_current_mems_allowed,
  2101. &preferred_zone);
  2102. wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/10);
  2103. }
  2104. }
  2105. out:
  2106. delayacct_freepages_end();
  2107. put_mems_allowed();
  2108. if (sc->nr_reclaimed)
  2109. return sc->nr_reclaimed;
  2110. /*
  2111. * As hibernation is going on, kswapd is freezed so that it can't mark
  2112. * the zone into all_unreclaimable. Thus bypassing all_unreclaimable
  2113. * check.
  2114. */
  2115. if (oom_killer_disabled)
  2116. return 0;
  2117. /* Aborted reclaim to try compaction? don't OOM, then */
  2118. if (aborted_reclaim)
  2119. return 1;
  2120. /* top priority shrink_zones still had more to do? don't OOM, then */
  2121. if (global_reclaim(sc) && !all_unreclaimable(zonelist, sc))
  2122. return 1;
  2123. return 0;
  2124. }
  2125. unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
  2126. gfp_t gfp_mask, nodemask_t *nodemask)
  2127. {
  2128. unsigned long nr_reclaimed;
  2129. struct scan_control sc = {
  2130. .gfp_mask = gfp_mask,
  2131. .may_writepage = !laptop_mode,
  2132. .nr_to_reclaim = SWAP_CLUSTER_MAX,
  2133. .may_unmap = 1,
  2134. .may_swap = 1,
  2135. .order = order,
  2136. .target_mem_cgroup = NULL,
  2137. .nodemask = nodemask,
  2138. };
  2139. struct shrink_control shrink = {
  2140. .gfp_mask = sc.gfp_mask,
  2141. };
  2142. trace_mm_vmscan_direct_reclaim_begin(order,
  2143. sc.may_writepage,
  2144. gfp_mask);
  2145. nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink);
  2146. trace_mm_vmscan_direct_reclaim_end(nr_reclaimed);
  2147. return nr_reclaimed;
  2148. }
  2149. #ifdef CONFIG_CGROUP_MEM_RES_CTLR
  2150. unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup *memcg,
  2151. gfp_t gfp_mask, bool noswap,
  2152. struct zone *zone,
  2153. unsigned long *nr_scanned)
  2154. {
  2155. struct scan_control sc = {
  2156. .nr_scanned = 0,
  2157. .nr_to_reclaim = SWAP_CLUSTER_MAX,
  2158. .may_writepage = !laptop_mode,
  2159. .may_unmap = 1,
  2160. .may_swap = !noswap,
  2161. .order = 0,
  2162. .target_mem_cgroup = memcg,
  2163. };
  2164. struct mem_cgroup_zone mz = {
  2165. .mem_cgroup = memcg,
  2166. .zone = zone,
  2167. };
  2168. sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
  2169. (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
  2170. trace_mm_vmscan_memcg_softlimit_reclaim_begin(0,
  2171. sc.may_writepage,
  2172. sc.gfp_mask);
  2173. /*
  2174. * NOTE: Although we can get the priority field, using it
  2175. * here is not a good idea, since it limits the pages we can scan.
  2176. * if we don't reclaim here, the shrink_zone from balance_pgdat
  2177. * will pick up pages from other mem cgroup's as well. We hack
  2178. * the priority and make it zero.
  2179. */
  2180. shrink_mem_cgroup_zone(0, &mz, &sc);
  2181. trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed);
  2182. *nr_scanned = sc.nr_scanned;
  2183. return sc.nr_reclaimed;
  2184. }
  2185. unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
  2186. gfp_t gfp_mask,
  2187. bool noswap)
  2188. {
  2189. struct zonelist *zonelist;
  2190. unsigned long nr_reclaimed;
  2191. int nid;
  2192. struct scan_control sc = {
  2193. .may_writepage = !laptop_mode,
  2194. .may_unmap = 1,
  2195. .may_swap = !noswap,
  2196. .nr_to_reclaim = SWAP_CLUSTER_MAX,
  2197. .order = 0,
  2198. .target_mem_cgroup = memcg,
  2199. .nodemask = NULL, /* we don't care the placement */
  2200. .gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
  2201. (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK),
  2202. };
  2203. struct shrink_control shrink = {
  2204. .gfp_mask = sc.gfp_mask,
  2205. };
  2206. /*
  2207. * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't
  2208. * take care of from where we get pages. So the node where we start the
  2209. * scan does not need to be the current node.
  2210. */
  2211. nid = mem_cgroup_select_victim_node(memcg);
  2212. zonelist = NODE_DATA(nid)->node_zonelists;
  2213. trace_mm_vmscan_memcg_reclaim_begin(0,
  2214. sc.may_writepage,
  2215. sc.gfp_mask);
  2216. nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink);
  2217. trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed);
  2218. return nr_reclaimed;
  2219. }
  2220. #endif
  2221. static void age_active_anon(struct zone *zone, struct scan_control *sc,
  2222. int priority)
  2223. {
  2224. struct mem_cgroup *memcg;
  2225. if (!total_swap_pages)
  2226. return;
  2227. memcg = mem_cgroup_iter(NULL, NULL, NULL);
  2228. do {
  2229. struct mem_cgroup_zone mz = {
  2230. .mem_cgroup = memcg,
  2231. .zone = zone,
  2232. };
  2233. if (inactive_anon_is_low(&mz))
  2234. shrink_active_list(SWAP_CLUSTER_MAX, &mz,
  2235. sc, priority, 0);
  2236. memcg = mem_cgroup_iter(NULL, memcg, NULL);
  2237. } while (memcg);
  2238. }
  2239. /*
  2240. * pgdat_balanced is used when checking if a node is balanced for high-order
  2241. * allocations. Only zones that meet watermarks and are in a zone allowed
  2242. * by the callers classzone_idx are added to balanced_pages. The total of
  2243. * balanced pages must be at least 25% of the zones allowed by classzone_idx
  2244. * for the node to be considered balanced. Forcing all zones to be balanced
  2245. * for high orders can cause excessive reclaim when there are imbalanced zones.
  2246. * The choice of 25% is due to
  2247. * o a 16M DMA zone that is balanced will not balance a zone on any
  2248. * reasonable sized machine
  2249. * o On all other machines, the top zone must be at least a reasonable
  2250. * percentage of the middle zones. For example, on 32-bit x86, highmem
  2251. * would need to be at least 256M for it to be balance a whole node.
  2252. * Similarly, on x86-64 the Normal zone would need to be at least 1G
  2253. * to balance a node on its own. These seemed like reasonable ratios.
  2254. */
  2255. static bool pgdat_balanced(pg_data_t *pgdat, unsigned long balanced_pages,
  2256. int classzone_idx)
  2257. {
  2258. unsigned long present_pages = 0;
  2259. int i;
  2260. for (i = 0; i <= classzone_idx; i++)
  2261. present_pages += pgdat->node_zones[i].present_pages;
  2262. /* A special case here: if zone has no page, we think it's balanced */
  2263. return balanced_pages >= (present_pages >> 2);
  2264. }
  2265. /* is kswapd sleeping prematurely? */
  2266. static bool sleeping_prematurely(pg_data_t *pgdat, int order, long remaining,
  2267. int classzone_idx)
  2268. {
  2269. int i;
  2270. unsigned long balanced = 0;
  2271. bool all_zones_ok = true;
  2272. /* If a direct reclaimer woke kswapd within HZ/10, it's premature */
  2273. if (remaining)
  2274. return true;
  2275. /* Check the watermark levels */
  2276. for (i = 0; i <= classzone_idx; i++) {
  2277. struct zone *zone = pgdat->node_zones + i;
  2278. if (!populated_zone(zone))
  2279. continue;
  2280. /*
  2281. * balance_pgdat() skips over all_unreclaimable after
  2282. * DEF_PRIORITY. Effectively, it considers them balanced so
  2283. * they must be considered balanced here as well if kswapd
  2284. * is to sleep
  2285. */
  2286. if (zone->all_unreclaimable) {
  2287. balanced += zone->present_pages;
  2288. continue;
  2289. }
  2290. if (!zone_watermark_ok_safe(zone, order, high_wmark_pages(zone),
  2291. i, 0))
  2292. all_zones_ok = false;
  2293. else
  2294. balanced += zone->present_pages;
  2295. }
  2296. /*
  2297. * For high-order requests, the balanced zones must contain at least
  2298. * 25% of the nodes pages for kswapd to sleep. For order-0, all zones
  2299. * must be balanced
  2300. */
  2301. if (order)
  2302. return !pgdat_balanced(pgdat, balanced, classzone_idx);
  2303. else
  2304. return !all_zones_ok;
  2305. }
  2306. /*
  2307. * For kswapd, balance_pgdat() will work across all this node's zones until
  2308. * they are all at high_wmark_pages(zone).
  2309. *
  2310. * Returns the final order kswapd was reclaiming at
  2311. *
  2312. * There is special handling here for zones which are full of pinned pages.
  2313. * This can happen if the pages are all mlocked, or if they are all used by
  2314. * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
  2315. * What we do is to detect the case where all pages in the zone have been
  2316. * scanned twice and there has been zero successful reclaim. Mark the zone as
  2317. * dead and from now on, only perform a short scan. Basically we're polling
  2318. * the zone for when the problem goes away.
  2319. *
  2320. * kswapd scans the zones in the highmem->normal->dma direction. It skips
  2321. * zones which have free_pages > high_wmark_pages(zone), but once a zone is
  2322. * found to have free_pages <= high_wmark_pages(zone), we scan that zone and the
  2323. * lower zones regardless of the number of free pages in the lower zones. This
  2324. * interoperates with the page allocator fallback scheme to ensure that aging
  2325. * of pages is balanced across the zones.
  2326. */
  2327. static unsigned long balance_pgdat(pg_data_t *pgdat, int order,
  2328. int *classzone_idx)
  2329. {
  2330. int all_zones_ok;
  2331. unsigned long balanced;
  2332. int priority;
  2333. int i;
  2334. int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
  2335. unsigned long total_scanned;
  2336. struct reclaim_state *reclaim_state = current->reclaim_state;
  2337. unsigned long nr_soft_reclaimed;
  2338. unsigned long nr_soft_scanned;
  2339. struct scan_control sc = {
  2340. .gfp_mask = GFP_KERNEL,
  2341. .may_unmap = 1,
  2342. .may_swap = 1,
  2343. /*
  2344. * kswapd doesn't want to be bailed out while reclaim. because
  2345. * we want to put equal scanning pressure on each zone.
  2346. */
  2347. .nr_to_reclaim = ULONG_MAX,
  2348. .order = order,
  2349. .target_mem_cgroup = NULL,
  2350. };
  2351. struct shrink_control shrink = {
  2352. .gfp_mask = sc.gfp_mask,
  2353. };
  2354. loop_again:
  2355. total_scanned = 0;
  2356. sc.nr_reclaimed = 0;
  2357. sc.may_writepage = !laptop_mode;
  2358. count_vm_event(PAGEOUTRUN);
  2359. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  2360. unsigned long lru_pages = 0;
  2361. int has_under_min_watermark_zone = 0;
  2362. /* The swap token gets in the way of swapout... */
  2363. if (!priority)
  2364. disable_swap_token(NULL);
  2365. all_zones_ok = 1;
  2366. balanced = 0;
  2367. /*
  2368. * Scan in the highmem->dma direction for the highest
  2369. * zone which needs scanning
  2370. */
  2371. for (i = pgdat->nr_zones - 1; i >= 0; i--) {
  2372. struct zone *zone = pgdat->node_zones + i;
  2373. if (!populated_zone(zone))
  2374. continue;
  2375. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  2376. continue;
  2377. /*
  2378. * Do some background aging of the anon list, to give
  2379. * pages a chance to be referenced before reclaiming.
  2380. */
  2381. age_active_anon(zone, &sc, priority);
  2382. if (!zone_watermark_ok_safe(zone, order,
  2383. high_wmark_pages(zone), 0, 0)) {
  2384. end_zone = i;
  2385. break;
  2386. } else {
  2387. /* If balanced, clear the congested flag */
  2388. zone_clear_flag(zone, ZONE_CONGESTED);
  2389. }
  2390. }
  2391. if (i < 0)
  2392. goto out;
  2393. for (i = 0; i <= end_zone; i++) {
  2394. struct zone *zone = pgdat->node_zones + i;
  2395. lru_pages += zone_reclaimable_pages(zone);
  2396. }
  2397. /*
  2398. * Now scan the zone in the dma->highmem direction, stopping
  2399. * at the last zone which needs scanning.
  2400. *
  2401. * We do this because the page allocator works in the opposite
  2402. * direction. This prevents the page allocator from allocating
  2403. * pages behind kswapd's direction of progress, which would
  2404. * cause too much scanning of the lower zones.
  2405. */
  2406. for (i = 0; i <= end_zone; i++) {
  2407. struct zone *zone = pgdat->node_zones + i;
  2408. int nr_slab;
  2409. unsigned long balance_gap;
  2410. if (!populated_zone(zone))
  2411. continue;
  2412. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  2413. continue;
  2414. sc.nr_scanned = 0;
  2415. nr_soft_scanned = 0;
  2416. /*
  2417. * Call soft limit reclaim before calling shrink_zone.
  2418. */
  2419. nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone,
  2420. order, sc.gfp_mask,
  2421. &nr_soft_scanned);
  2422. sc.nr_reclaimed += nr_soft_reclaimed;
  2423. total_scanned += nr_soft_scanned;
  2424. /*
  2425. * We put equal pressure on every zone, unless
  2426. * one zone has way too many pages free
  2427. * already. The "too many pages" is defined
  2428. * as the high wmark plus a "gap" where the
  2429. * gap is either the low watermark or 1%
  2430. * of the zone, whichever is smaller.
  2431. */
  2432. balance_gap = min(low_wmark_pages(zone),
  2433. (zone->present_pages +
  2434. KSWAPD_ZONE_BALANCE_GAP_RATIO-1) /
  2435. KSWAPD_ZONE_BALANCE_GAP_RATIO);
  2436. if (!zone_watermark_ok_safe(zone, order,
  2437. high_wmark_pages(zone) + balance_gap,
  2438. end_zone, 0)) {
  2439. shrink_zone(priority, zone, &sc);
  2440. reclaim_state->reclaimed_slab = 0;
  2441. nr_slab = shrink_slab(&shrink, sc.nr_scanned, lru_pages);
  2442. sc.nr_reclaimed += reclaim_state->reclaimed_slab;
  2443. total_scanned += sc.nr_scanned;
  2444. if (nr_slab == 0 && !zone_reclaimable(zone))
  2445. zone->all_unreclaimable = 1;
  2446. }
  2447. /*
  2448. * If we've done a decent amount of scanning and
  2449. * the reclaim ratio is low, start doing writepage
  2450. * even in laptop mode
  2451. */
  2452. if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
  2453. total_scanned > sc.nr_reclaimed + sc.nr_reclaimed / 2)
  2454. sc.may_writepage = 1;
  2455. if (zone->all_unreclaimable) {
  2456. if (end_zone && end_zone == i)
  2457. end_zone--;
  2458. continue;
  2459. }
  2460. if (!zone_watermark_ok_safe(zone, order,
  2461. high_wmark_pages(zone), end_zone, 0)) {
  2462. all_zones_ok = 0;
  2463. /*
  2464. * We are still under min water mark. This
  2465. * means that we have a GFP_ATOMIC allocation
  2466. * failure risk. Hurry up!
  2467. */
  2468. if (!zone_watermark_ok_safe(zone, order,
  2469. min_wmark_pages(zone), end_zone, 0))
  2470. has_under_min_watermark_zone = 1;
  2471. } else {
  2472. /*
  2473. * If a zone reaches its high watermark,
  2474. * consider it to be no longer congested. It's
  2475. * possible there are dirty pages backed by
  2476. * congested BDIs but as pressure is relieved,
  2477. * spectulatively avoid congestion waits
  2478. */
  2479. zone_clear_flag(zone, ZONE_CONGESTED);
  2480. if (i <= *classzone_idx)
  2481. balanced += zone->present_pages;
  2482. }
  2483. }
  2484. if (all_zones_ok || (order && pgdat_balanced(pgdat, balanced, *classzone_idx)))
  2485. break; /* kswapd: all done */
  2486. /*
  2487. * OK, kswapd is getting into trouble. Take a nap, then take
  2488. * another pass across the zones.
  2489. */
  2490. if (total_scanned && (priority < DEF_PRIORITY - 2)) {
  2491. if (has_under_min_watermark_zone)
  2492. count_vm_event(KSWAPD_SKIP_CONGESTION_WAIT);
  2493. else
  2494. congestion_wait(BLK_RW_ASYNC, HZ/10);
  2495. }
  2496. /*
  2497. * We do this so kswapd doesn't build up large priorities for
  2498. * example when it is freeing in parallel with allocators. It
  2499. * matches the direct reclaim path behaviour in terms of impact
  2500. * on zone->*_priority.
  2501. */
  2502. if (sc.nr_reclaimed >= SWAP_CLUSTER_MAX)
  2503. break;
  2504. }
  2505. out:
  2506. /*
  2507. * order-0: All zones must meet high watermark for a balanced node
  2508. * high-order: Balanced zones must make up at least 25% of the node
  2509. * for the node to be balanced
  2510. */
  2511. if (!(all_zones_ok || (order && pgdat_balanced(pgdat, balanced, *classzone_idx)))) {
  2512. cond_resched();
  2513. try_to_freeze();
  2514. /*
  2515. * Fragmentation may mean that the system cannot be
  2516. * rebalanced for high-order allocations in all zones.
  2517. * At this point, if nr_reclaimed < SWAP_CLUSTER_MAX,
  2518. * it means the zones have been fully scanned and are still
  2519. * not balanced. For high-order allocations, there is
  2520. * little point trying all over again as kswapd may
  2521. * infinite loop.
  2522. *
  2523. * Instead, recheck all watermarks at order-0 as they
  2524. * are the most important. If watermarks are ok, kswapd will go
  2525. * back to sleep. High-order users can still perform direct
  2526. * reclaim if they wish.
  2527. */
  2528. if (sc.nr_reclaimed < SWAP_CLUSTER_MAX)
  2529. order = sc.order = 0;
  2530. goto loop_again;
  2531. }
  2532. /*
  2533. * If kswapd was reclaiming at a higher order, it has the option of
  2534. * sleeping without all zones being balanced. Before it does, it must
  2535. * ensure that the watermarks for order-0 on *all* zones are met and
  2536. * that the congestion flags are cleared. The congestion flag must
  2537. * be cleared as kswapd is the only mechanism that clears the flag
  2538. * and it is potentially going to sleep here.
  2539. */
  2540. if (order) {
  2541. for (i = 0; i <= end_zone; i++) {
  2542. struct zone *zone = pgdat->node_zones + i;
  2543. if (!populated_zone(zone))
  2544. continue;
  2545. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  2546. continue;
  2547. /* Confirm the zone is balanced for order-0 */
  2548. if (!zone_watermark_ok(zone, 0,
  2549. high_wmark_pages(zone), 0, 0)) {
  2550. order = sc.order = 0;
  2551. goto loop_again;
  2552. }
  2553. /* If balanced, clear the congested flag */
  2554. zone_clear_flag(zone, ZONE_CONGESTED);
  2555. if (i <= *classzone_idx)
  2556. balanced += zone->present_pages;
  2557. }
  2558. }
  2559. /*
  2560. * Return the order we were reclaiming at so sleeping_prematurely()
  2561. * makes a decision on the order we were last reclaiming at. However,
  2562. * if another caller entered the allocator slow path while kswapd
  2563. * was awake, order will remain at the higher level
  2564. */
  2565. *classzone_idx = end_zone;
  2566. return order;
  2567. }
  2568. static void kswapd_try_to_sleep(pg_data_t *pgdat, int order, int classzone_idx)
  2569. {
  2570. long remaining = 0;
  2571. DEFINE_WAIT(wait);
  2572. if (freezing(current) || kthread_should_stop())
  2573. return;
  2574. prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
  2575. /* Try to sleep for a short interval */
  2576. if (!sleeping_prematurely(pgdat, order, remaining, classzone_idx)) {
  2577. remaining = schedule_timeout(HZ/10);
  2578. finish_wait(&pgdat->kswapd_wait, &wait);
  2579. prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
  2580. }
  2581. /*
  2582. * After a short sleep, check if it was a premature sleep. If not, then
  2583. * go fully to sleep until explicitly woken up.
  2584. */
  2585. if (!sleeping_prematurely(pgdat, order, remaining, classzone_idx)) {
  2586. trace_mm_vmscan_kswapd_sleep(pgdat->node_id);
  2587. /*
  2588. * vmstat counters are not perfectly accurate and the estimated
  2589. * value for counters such as NR_FREE_PAGES can deviate from the
  2590. * true value by nr_online_cpus * threshold. To avoid the zone
  2591. * watermarks being breached while under pressure, we reduce the
  2592. * per-cpu vmstat threshold while kswapd is awake and restore
  2593. * them before going back to sleep.
  2594. */
  2595. set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold);
  2596. schedule();
  2597. set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold);
  2598. } else {
  2599. if (remaining)
  2600. count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY);
  2601. else
  2602. count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY);
  2603. }
  2604. finish_wait(&pgdat->kswapd_wait, &wait);
  2605. }
  2606. /*
  2607. * The background pageout daemon, started as a kernel thread
  2608. * from the init process.
  2609. *
  2610. * This basically trickles out pages so that we have _some_
  2611. * free memory available even if there is no other activity
  2612. * that frees anything up. This is needed for things like routing
  2613. * etc, where we otherwise might have all activity going on in
  2614. * asynchronous contexts that cannot page things out.
  2615. *
  2616. * If there are applications that are active memory-allocators
  2617. * (most normal use), this basically shouldn't matter.
  2618. */
  2619. static int kswapd(void *p)
  2620. {
  2621. unsigned long order, new_order;
  2622. unsigned balanced_order;
  2623. int classzone_idx, new_classzone_idx;
  2624. int balanced_classzone_idx;
  2625. pg_data_t *pgdat = (pg_data_t*)p;
  2626. struct task_struct *tsk = current;
  2627. struct reclaim_state reclaim_state = {
  2628. .reclaimed_slab = 0,
  2629. };
  2630. const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
  2631. lockdep_set_current_reclaim_state(GFP_KERNEL);
  2632. if (!cpumask_empty(cpumask))
  2633. set_cpus_allowed_ptr(tsk, cpumask);
  2634. current->reclaim_state = &reclaim_state;
  2635. /*
  2636. * Tell the memory management that we're a "memory allocator",
  2637. * and that if we need more memory we should get access to it
  2638. * regardless (see "__alloc_pages()"). "kswapd" should
  2639. * never get caught in the normal page freeing logic.
  2640. *
  2641. * (Kswapd normally doesn't need memory anyway, but sometimes
  2642. * you need a small amount of memory in order to be able to
  2643. * page out something else, and this flag essentially protects
  2644. * us from recursively trying to free more memory as we're
  2645. * trying to free the first piece of memory in the first place).
  2646. */
  2647. tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
  2648. set_freezable();
  2649. order = new_order = 0;
  2650. balanced_order = 0;
  2651. classzone_idx = new_classzone_idx = pgdat->nr_zones - 1;
  2652. balanced_classzone_idx = classzone_idx;
  2653. for ( ; ; ) {
  2654. int ret;
  2655. /*
  2656. * If the last balance_pgdat was unsuccessful it's unlikely a
  2657. * new request of a similar or harder type will succeed soon
  2658. * so consider going to sleep on the basis we reclaimed at
  2659. */
  2660. if (balanced_classzone_idx >= new_classzone_idx &&
  2661. balanced_order == new_order) {
  2662. new_order = pgdat->kswapd_max_order;
  2663. new_classzone_idx = pgdat->classzone_idx;
  2664. pgdat->kswapd_max_order = 0;
  2665. pgdat->classzone_idx = pgdat->nr_zones - 1;
  2666. }
  2667. if (order < new_order || classzone_idx > new_classzone_idx) {
  2668. /*
  2669. * Don't sleep if someone wants a larger 'order'
  2670. * allocation or has tigher zone constraints
  2671. */
  2672. order = new_order;
  2673. classzone_idx = new_classzone_idx;
  2674. } else {
  2675. kswapd_try_to_sleep(pgdat, balanced_order,
  2676. balanced_classzone_idx);
  2677. order = pgdat->kswapd_max_order;
  2678. classzone_idx = pgdat->classzone_idx;
  2679. new_order = order;
  2680. new_classzone_idx = classzone_idx;
  2681. pgdat->kswapd_max_order = 0;
  2682. pgdat->classzone_idx = pgdat->nr_zones - 1;
  2683. }
  2684. ret = try_to_freeze();
  2685. if (kthread_should_stop())
  2686. break;
  2687. /*
  2688. * We can speed up thawing tasks if we don't call balance_pgdat
  2689. * after returning from the refrigerator
  2690. */
  2691. if (!ret) {
  2692. trace_mm_vmscan_kswapd_wake(pgdat->node_id, order);
  2693. balanced_classzone_idx = classzone_idx;
  2694. balanced_order = balance_pgdat(pgdat, order,
  2695. &balanced_classzone_idx);
  2696. }
  2697. }
  2698. return 0;
  2699. }
  2700. /*
  2701. * A zone is low on free memory, so wake its kswapd task to service it.
  2702. */
  2703. void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx)
  2704. {
  2705. pg_data_t *pgdat;
  2706. if (!populated_zone(zone))
  2707. return;
  2708. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  2709. return;
  2710. pgdat = zone->zone_pgdat;
  2711. if (pgdat->kswapd_max_order < order) {
  2712. pgdat->kswapd_max_order = order;
  2713. pgdat->classzone_idx = min(pgdat->classzone_idx, classzone_idx);
  2714. }
  2715. if (!waitqueue_active(&pgdat->kswapd_wait))
  2716. return;
  2717. if (zone_watermark_ok_safe(zone, order, low_wmark_pages(zone), 0, 0))
  2718. return;
  2719. trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, zone_idx(zone), order);
  2720. wake_up_interruptible(&pgdat->kswapd_wait);
  2721. }
  2722. /*
  2723. * The reclaimable count would be mostly accurate.
  2724. * The less reclaimable pages may be
  2725. * - mlocked pages, which will be moved to unevictable list when encountered
  2726. * - mapped pages, which may require several travels to be reclaimed
  2727. * - dirty pages, which is not "instantly" reclaimable
  2728. */
  2729. unsigned long global_reclaimable_pages(void)
  2730. {
  2731. int nr;
  2732. nr = global_page_state(NR_ACTIVE_FILE) +
  2733. global_page_state(NR_INACTIVE_FILE);
  2734. if (nr_swap_pages > 0)
  2735. nr += global_page_state(NR_ACTIVE_ANON) +
  2736. global_page_state(NR_INACTIVE_ANON);
  2737. return nr;
  2738. }
  2739. unsigned long zone_reclaimable_pages(struct zone *zone)
  2740. {
  2741. int nr;
  2742. nr = zone_page_state(zone, NR_ACTIVE_FILE) +
  2743. zone_page_state(zone, NR_INACTIVE_FILE);
  2744. if (nr_swap_pages > 0)
  2745. nr += zone_page_state(zone, NR_ACTIVE_ANON) +
  2746. zone_page_state(zone, NR_INACTIVE_ANON);
  2747. return nr;
  2748. }
  2749. #ifdef CONFIG_HIBERNATION
  2750. /*
  2751. * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of
  2752. * freed pages.
  2753. *
  2754. * Rather than trying to age LRUs the aim is to preserve the overall
  2755. * LRU order by reclaiming preferentially
  2756. * inactive > active > active referenced > active mapped
  2757. */
  2758. unsigned long shrink_all_memory(unsigned long nr_to_reclaim)
  2759. {
  2760. struct reclaim_state reclaim_state;
  2761. struct scan_control sc = {
  2762. .gfp_mask = GFP_HIGHUSER_MOVABLE,
  2763. .may_swap = 1,
  2764. .may_unmap = 1,
  2765. .may_writepage = 1,
  2766. .nr_to_reclaim = nr_to_reclaim,
  2767. .hibernation_mode = 1,
  2768. .order = 0,
  2769. };
  2770. struct shrink_control shrink = {
  2771. .gfp_mask = sc.gfp_mask,
  2772. };
  2773. struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
  2774. struct task_struct *p = current;
  2775. unsigned long nr_reclaimed;
  2776. p->flags |= PF_MEMALLOC;
  2777. lockdep_set_current_reclaim_state(sc.gfp_mask);
  2778. reclaim_state.reclaimed_slab = 0;
  2779. p->reclaim_state = &reclaim_state;
  2780. nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink);
  2781. p->reclaim_state = NULL;
  2782. lockdep_clear_current_reclaim_state();
  2783. p->flags &= ~PF_MEMALLOC;
  2784. return nr_reclaimed;
  2785. }
  2786. #endif /* CONFIG_HIBERNATION */
  2787. /* It's optimal to keep kswapds on the same CPUs as their memory, but
  2788. not required for correctness. So if the last cpu in a node goes
  2789. away, we get changed to run anywhere: as the first one comes back,
  2790. restore their cpu bindings. */
  2791. static int __devinit cpu_callback(struct notifier_block *nfb,
  2792. unsigned long action, void *hcpu)
  2793. {
  2794. int nid;
  2795. if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
  2796. for_each_node_state(nid, N_HIGH_MEMORY) {
  2797. pg_data_t *pgdat = NODE_DATA(nid);
  2798. const struct cpumask *mask;
  2799. mask = cpumask_of_node(pgdat->node_id);
  2800. if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
  2801. /* One of our CPUs online: restore mask */
  2802. set_cpus_allowed_ptr(pgdat->kswapd, mask);
  2803. }
  2804. }
  2805. return NOTIFY_OK;
  2806. }
  2807. /*
  2808. * This kswapd start function will be called by init and node-hot-add.
  2809. * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
  2810. */
  2811. int kswapd_run(int nid)
  2812. {
  2813. pg_data_t *pgdat = NODE_DATA(nid);
  2814. int ret = 0;
  2815. if (pgdat->kswapd)
  2816. return 0;
  2817. pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
  2818. if (IS_ERR(pgdat->kswapd)) {
  2819. /* failure at boot is fatal */
  2820. BUG_ON(system_state == SYSTEM_BOOTING);
  2821. printk("Failed to start kswapd on node %d\n",nid);
  2822. ret = -1;
  2823. }
  2824. return ret;
  2825. }
  2826. /*
  2827. * Called by memory hotplug when all memory in a node is offlined.
  2828. */
  2829. void kswapd_stop(int nid)
  2830. {
  2831. struct task_struct *kswapd = NODE_DATA(nid)->kswapd;
  2832. if (kswapd)
  2833. kthread_stop(kswapd);
  2834. }
  2835. static int __init kswapd_init(void)
  2836. {
  2837. int nid;
  2838. swap_setup();
  2839. for_each_node_state(nid, N_HIGH_MEMORY)
  2840. kswapd_run(nid);
  2841. hotcpu_notifier(cpu_callback, 0);
  2842. return 0;
  2843. }
  2844. module_init(kswapd_init)
  2845. #ifdef CONFIG_NUMA
  2846. /*
  2847. * Zone reclaim mode
  2848. *
  2849. * If non-zero call zone_reclaim when the number of free pages falls below
  2850. * the watermarks.
  2851. */
  2852. int zone_reclaim_mode __read_mostly;
  2853. #define RECLAIM_OFF 0
  2854. #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */
  2855. #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
  2856. #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
  2857. /*
  2858. * Priority for ZONE_RECLAIM. This determines the fraction of pages
  2859. * of a node considered for each zone_reclaim. 4 scans 1/16th of
  2860. * a zone.
  2861. */
  2862. #define ZONE_RECLAIM_PRIORITY 4
  2863. /*
  2864. * Percentage of pages in a zone that must be unmapped for zone_reclaim to
  2865. * occur.
  2866. */
  2867. int sysctl_min_unmapped_ratio = 1;
  2868. /*
  2869. * If the number of slab pages in a zone grows beyond this percentage then
  2870. * slab reclaim needs to occur.
  2871. */
  2872. int sysctl_min_slab_ratio = 5;
  2873. static inline unsigned long zone_unmapped_file_pages(struct zone *zone)
  2874. {
  2875. unsigned long file_mapped = zone_page_state(zone, NR_FILE_MAPPED);
  2876. unsigned long file_lru = zone_page_state(zone, NR_INACTIVE_FILE) +
  2877. zone_page_state(zone, NR_ACTIVE_FILE);
  2878. /*
  2879. * It's possible for there to be more file mapped pages than
  2880. * accounted for by the pages on the file LRU lists because
  2881. * tmpfs pages accounted for as ANON can also be FILE_MAPPED
  2882. */
  2883. return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0;
  2884. }
  2885. /* Work out how many page cache pages we can reclaim in this reclaim_mode */
  2886. static long zone_pagecache_reclaimable(struct zone *zone)
  2887. {
  2888. long nr_pagecache_reclaimable;
  2889. long delta = 0;
  2890. /*
  2891. * If RECLAIM_SWAP is set, then all file pages are considered
  2892. * potentially reclaimable. Otherwise, we have to worry about
  2893. * pages like swapcache and zone_unmapped_file_pages() provides
  2894. * a better estimate
  2895. */
  2896. if (zone_reclaim_mode & RECLAIM_SWAP)
  2897. nr_pagecache_reclaimable = zone_page_state(zone, NR_FILE_PAGES);
  2898. else
  2899. nr_pagecache_reclaimable = zone_unmapped_file_pages(zone);
  2900. /* If we can't clean pages, remove dirty pages from consideration */
  2901. if (!(zone_reclaim_mode & RECLAIM_WRITE))
  2902. delta += zone_page_state(zone, NR_FILE_DIRTY);
  2903. /* Watch for any possible underflows due to delta */
  2904. if (unlikely(delta > nr_pagecache_reclaimable))
  2905. delta = nr_pagecache_reclaimable;
  2906. return nr_pagecache_reclaimable - delta;
  2907. }
  2908. /*
  2909. * Try to free up some pages from this zone through reclaim.
  2910. */
  2911. static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  2912. {
  2913. /* Minimum pages needed in order to stay on node */
  2914. const unsigned long nr_pages = 1 << order;
  2915. struct task_struct *p = current;
  2916. struct reclaim_state reclaim_state;
  2917. int priority;
  2918. struct scan_control sc = {
  2919. .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
  2920. .may_unmap = !!(zone_reclaim_mode & RECLAIM_SWAP),
  2921. .may_swap = 1,
  2922. .nr_to_reclaim = max_t(unsigned long, nr_pages,
  2923. SWAP_CLUSTER_MAX),
  2924. .gfp_mask = gfp_mask,
  2925. .order = order,
  2926. };
  2927. struct shrink_control shrink = {
  2928. .gfp_mask = sc.gfp_mask,
  2929. };
  2930. unsigned long nr_slab_pages0, nr_slab_pages1;
  2931. cond_resched();
  2932. /*
  2933. * We need to be able to allocate from the reserves for RECLAIM_SWAP
  2934. * and we also need to be able to write out pages for RECLAIM_WRITE
  2935. * and RECLAIM_SWAP.
  2936. */
  2937. p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
  2938. lockdep_set_current_reclaim_state(gfp_mask);
  2939. reclaim_state.reclaimed_slab = 0;
  2940. p->reclaim_state = &reclaim_state;
  2941. if (zone_pagecache_reclaimable(zone) > zone->min_unmapped_pages) {
  2942. /*
  2943. * Free memory by calling shrink zone with increasing
  2944. * priorities until we have enough memory freed.
  2945. */
  2946. priority = ZONE_RECLAIM_PRIORITY;
  2947. do {
  2948. shrink_zone(priority, zone, &sc);
  2949. priority--;
  2950. } while (priority >= 0 && sc.nr_reclaimed < nr_pages);
  2951. }
  2952. nr_slab_pages0 = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
  2953. if (nr_slab_pages0 > zone->min_slab_pages) {
  2954. /*
  2955. * shrink_slab() does not currently allow us to determine how
  2956. * many pages were freed in this zone. So we take the current
  2957. * number of slab pages and shake the slab until it is reduced
  2958. * by the same nr_pages that we used for reclaiming unmapped
  2959. * pages.
  2960. *
  2961. * Note that shrink_slab will free memory on all zones and may
  2962. * take a long time.
  2963. */
  2964. for (;;) {
  2965. unsigned long lru_pages = zone_reclaimable_pages(zone);
  2966. /* No reclaimable slab or very low memory pressure */
  2967. if (!shrink_slab(&shrink, sc.nr_scanned, lru_pages))
  2968. break;
  2969. /* Freed enough memory */
  2970. nr_slab_pages1 = zone_page_state(zone,
  2971. NR_SLAB_RECLAIMABLE);
  2972. if (nr_slab_pages1 + nr_pages <= nr_slab_pages0)
  2973. break;
  2974. }
  2975. /*
  2976. * Update nr_reclaimed by the number of slab pages we
  2977. * reclaimed from this zone.
  2978. */
  2979. nr_slab_pages1 = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
  2980. if (nr_slab_pages1 < nr_slab_pages0)
  2981. sc.nr_reclaimed += nr_slab_pages0 - nr_slab_pages1;
  2982. }
  2983. p->reclaim_state = NULL;
  2984. current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
  2985. lockdep_clear_current_reclaim_state();
  2986. return sc.nr_reclaimed >= nr_pages;
  2987. }
  2988. int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  2989. {
  2990. int node_id;
  2991. int ret;
  2992. /*
  2993. * Zone reclaim reclaims unmapped file backed pages and
  2994. * slab pages if we are over the defined limits.
  2995. *
  2996. * A small portion of unmapped file backed pages is needed for
  2997. * file I/O otherwise pages read by file I/O will be immediately
  2998. * thrown out if the zone is overallocated. So we do not reclaim
  2999. * if less than a specified percentage of the zone is used by
  3000. * unmapped file backed pages.
  3001. */
  3002. if (zone_pagecache_reclaimable(zone) <= zone->min_unmapped_pages &&
  3003. zone_page_state(zone, NR_SLAB_RECLAIMABLE) <= zone->min_slab_pages)
  3004. return ZONE_RECLAIM_FULL;
  3005. if (zone->all_unreclaimable)
  3006. return ZONE_RECLAIM_FULL;
  3007. /*
  3008. * Do not scan if the allocation should not be delayed.
  3009. */
  3010. if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC))
  3011. return ZONE_RECLAIM_NOSCAN;
  3012. /*
  3013. * Only run zone reclaim on the local zone or on zones that do not
  3014. * have associated processors. This will favor the local processor
  3015. * over remote processors and spread off node memory allocations
  3016. * as wide as possible.
  3017. */
  3018. node_id = zone_to_nid(zone);
  3019. if (node_state(node_id, N_CPU) && node_id != numa_node_id())
  3020. return ZONE_RECLAIM_NOSCAN;
  3021. if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED))
  3022. return ZONE_RECLAIM_NOSCAN;
  3023. ret = __zone_reclaim(zone, gfp_mask, order);
  3024. zone_clear_flag(zone, ZONE_RECLAIM_LOCKED);
  3025. if (!ret)
  3026. count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED);
  3027. return ret;
  3028. }
  3029. #endif
  3030. /*
  3031. * page_evictable - test whether a page is evictable
  3032. * @page: the page to test
  3033. * @vma: the VMA in which the page is or will be mapped, may be NULL
  3034. *
  3035. * Test whether page is evictable--i.e., should be placed on active/inactive
  3036. * lists vs unevictable list. The vma argument is !NULL when called from the
  3037. * fault path to determine how to instantate a new page.
  3038. *
  3039. * Reasons page might not be evictable:
  3040. * (1) page's mapping marked unevictable
  3041. * (2) page is part of an mlocked VMA
  3042. *
  3043. */
  3044. int page_evictable(struct page *page, struct vm_area_struct *vma)
  3045. {
  3046. if (mapping_unevictable(page_mapping(page)))
  3047. return 0;
  3048. if (PageMlocked(page) || (vma && is_mlocked_vma(vma, page)))
  3049. return 0;
  3050. return 1;
  3051. }
  3052. /**
  3053. * check_move_unevictable_page - check page for evictability and move to appropriate zone lru list
  3054. * @page: page to check evictability and move to appropriate lru list
  3055. * @zone: zone page is in
  3056. *
  3057. * Checks a page for evictability and moves the page to the appropriate
  3058. * zone lru list.
  3059. *
  3060. * Restrictions: zone->lru_lock must be held, page must be on LRU and must
  3061. * have PageUnevictable set.
  3062. */
  3063. static void check_move_unevictable_page(struct page *page, struct zone *zone)
  3064. {
  3065. struct lruvec *lruvec;
  3066. VM_BUG_ON(PageActive(page));
  3067. retry:
  3068. ClearPageUnevictable(page);
  3069. if (page_evictable(page, NULL)) {
  3070. enum lru_list l = page_lru_base_type(page);
  3071. __dec_zone_state(zone, NR_UNEVICTABLE);
  3072. lruvec = mem_cgroup_lru_move_lists(zone, page,
  3073. LRU_UNEVICTABLE, l);
  3074. list_move(&page->lru, &lruvec->lists[l]);
  3075. __inc_zone_state(zone, NR_INACTIVE_ANON + l);
  3076. __count_vm_event(UNEVICTABLE_PGRESCUED);
  3077. } else {
  3078. /*
  3079. * rotate unevictable list
  3080. */
  3081. SetPageUnevictable(page);
  3082. lruvec = mem_cgroup_lru_move_lists(zone, page, LRU_UNEVICTABLE,
  3083. LRU_UNEVICTABLE);
  3084. list_move(&page->lru, &lruvec->lists[LRU_UNEVICTABLE]);
  3085. if (page_evictable(page, NULL))
  3086. goto retry;
  3087. }
  3088. }
  3089. /**
  3090. * scan_mapping_unevictable_pages - scan an address space for evictable pages
  3091. * @mapping: struct address_space to scan for evictable pages
  3092. *
  3093. * Scan all pages in mapping. Check unevictable pages for
  3094. * evictability and move them to the appropriate zone lru list.
  3095. */
  3096. void scan_mapping_unevictable_pages(struct address_space *mapping)
  3097. {
  3098. pgoff_t next = 0;
  3099. pgoff_t end = (i_size_read(mapping->host) + PAGE_CACHE_SIZE - 1) >>
  3100. PAGE_CACHE_SHIFT;
  3101. struct zone *zone;
  3102. struct pagevec pvec;
  3103. if (mapping->nrpages == 0)
  3104. return;
  3105. pagevec_init(&pvec, 0);
  3106. while (next < end &&
  3107. pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  3108. int i;
  3109. int pg_scanned = 0;
  3110. zone = NULL;
  3111. for (i = 0; i < pagevec_count(&pvec); i++) {
  3112. struct page *page = pvec.pages[i];
  3113. pgoff_t page_index = page->index;
  3114. struct zone *pagezone = page_zone(page);
  3115. pg_scanned++;
  3116. if (page_index > next)
  3117. next = page_index;
  3118. next++;
  3119. if (pagezone != zone) {
  3120. if (zone)
  3121. spin_unlock_irq(&zone->lru_lock);
  3122. zone = pagezone;
  3123. spin_lock_irq(&zone->lru_lock);
  3124. }
  3125. if (PageLRU(page) && PageUnevictable(page))
  3126. check_move_unevictable_page(page, zone);
  3127. }
  3128. if (zone)
  3129. spin_unlock_irq(&zone->lru_lock);
  3130. pagevec_release(&pvec);
  3131. count_vm_events(UNEVICTABLE_PGSCANNED, pg_scanned);
  3132. }
  3133. }
  3134. static void warn_scan_unevictable_pages(void)
  3135. {
  3136. printk_once(KERN_WARNING
  3137. "%s: The scan_unevictable_pages sysctl/node-interface has been "
  3138. "disabled for lack of a legitimate use case. If you have "
  3139. "one, please send an email to linux-mm@kvack.org.\n",
  3140. current->comm);
  3141. }
  3142. /*
  3143. * scan_unevictable_pages [vm] sysctl handler. On demand re-scan of
  3144. * all nodes' unevictable lists for evictable pages
  3145. */
  3146. unsigned long scan_unevictable_pages;
  3147. int scan_unevictable_handler(struct ctl_table *table, int write,
  3148. void __user *buffer,
  3149. size_t *length, loff_t *ppos)
  3150. {
  3151. warn_scan_unevictable_pages();
  3152. proc_doulongvec_minmax(table, write, buffer, length, ppos);
  3153. scan_unevictable_pages = 0;
  3154. return 0;
  3155. }
  3156. #ifdef CONFIG_NUMA
  3157. /*
  3158. * per node 'scan_unevictable_pages' attribute. On demand re-scan of
  3159. * a specified node's per zone unevictable lists for evictable pages.
  3160. */
  3161. static ssize_t read_scan_unevictable_node(struct device *dev,
  3162. struct device_attribute *attr,
  3163. char *buf)
  3164. {
  3165. warn_scan_unevictable_pages();
  3166. return sprintf(buf, "0\n"); /* always zero; should fit... */
  3167. }
  3168. static ssize_t write_scan_unevictable_node(struct device *dev,
  3169. struct device_attribute *attr,
  3170. const char *buf, size_t count)
  3171. {
  3172. warn_scan_unevictable_pages();
  3173. return 1;
  3174. }
  3175. static DEVICE_ATTR(scan_unevictable_pages, S_IRUGO | S_IWUSR,
  3176. read_scan_unevictable_node,
  3177. write_scan_unevictable_node);
  3178. int scan_unevictable_register_node(struct node *node)
  3179. {
  3180. return device_create_file(&node->dev, &dev_attr_scan_unevictable_pages);
  3181. }
  3182. void scan_unevictable_unregister_node(struct node *node)
  3183. {
  3184. device_remove_file(&node->dev, &dev_attr_scan_unevictable_pages);
  3185. }
  3186. #endif