vmscan.c 71 KB

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