vmscan.c 80 KB

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