vmscan.c 79 KB

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