vmscan.c 73 KB

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