vmscan.c 70 KB

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