vmscan.c 105 KB

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