vmscan.c 81 KB

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