vmscan.c 81 KB

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