vmscan.c 73 KB

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