vmscan.c 81 KB

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