vmscan.c 73 KB

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