vmscan.c 80 KB

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