vmscan.c 80 KB

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