vmscan.c 73 KB

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