vmscan.c 80 KB

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