vmscan.c 79 KB

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