vmscan.c 80 KB

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