vmscan.c 80 KB

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