vmscan.c 80 KB

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