vmscan.c 77 KB

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