vmscan.c 71 KB

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