vmscan.c 71 KB

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