vmscan.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130
  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 <asm/tlbflush.h>
  42. #include <asm/div64.h>
  43. #include <linux/swapops.h>
  44. #include "internal.h"
  45. struct scan_control {
  46. /* Incremented by the number of inactive pages that were scanned */
  47. unsigned long nr_scanned;
  48. /* This context's GFP mask */
  49. gfp_t gfp_mask;
  50. int may_writepage;
  51. /* Can pages be swapped as part of reclaim? */
  52. int may_swap;
  53. /* This context's SWAP_CLUSTER_MAX. If freeing memory for
  54. * suspend, we effectively ignore SWAP_CLUSTER_MAX.
  55. * In this context, it doesn't matter that we scan the
  56. * whole list at once. */
  57. int swap_cluster_max;
  58. int swappiness;
  59. int all_unreclaimable;
  60. int order;
  61. /* Which cgroup do we reclaim from */
  62. struct mem_cgroup *mem_cgroup;
  63. /* Pluggable isolate pages callback */
  64. unsigned long (*isolate_pages)(unsigned long nr, struct list_head *dst,
  65. unsigned long *scanned, int order, int mode,
  66. struct zone *z, struct mem_cgroup *mem_cont,
  67. int active);
  68. };
  69. #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
  70. #ifdef ARCH_HAS_PREFETCH
  71. #define prefetch_prev_lru_page(_page, _base, _field) \
  72. do { \
  73. if ((_page)->lru.prev != _base) { \
  74. struct page *prev; \
  75. \
  76. prev = lru_to_page(&(_page->lru)); \
  77. prefetch(&prev->_field); \
  78. } \
  79. } while (0)
  80. #else
  81. #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
  82. #endif
  83. #ifdef ARCH_HAS_PREFETCHW
  84. #define prefetchw_prev_lru_page(_page, _base, _field) \
  85. do { \
  86. if ((_page)->lru.prev != _base) { \
  87. struct page *prev; \
  88. \
  89. prev = lru_to_page(&(_page->lru)); \
  90. prefetchw(&prev->_field); \
  91. } \
  92. } while (0)
  93. #else
  94. #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
  95. #endif
  96. /*
  97. * From 0 .. 100. Higher means more swappy.
  98. */
  99. int vm_swappiness = 60;
  100. long vm_total_pages; /* The total number of pages which the VM controls */
  101. static LIST_HEAD(shrinker_list);
  102. static DECLARE_RWSEM(shrinker_rwsem);
  103. #ifdef CONFIG_CGROUP_MEM_RES_CTLR
  104. #define scan_global_lru(sc) (!(sc)->mem_cgroup)
  105. #else
  106. #define scan_global_lru(sc) (1)
  107. #endif
  108. /*
  109. * Add a shrinker callback to be called from the vm
  110. */
  111. void register_shrinker(struct shrinker *shrinker)
  112. {
  113. shrinker->nr = 0;
  114. down_write(&shrinker_rwsem);
  115. list_add_tail(&shrinker->list, &shrinker_list);
  116. up_write(&shrinker_rwsem);
  117. }
  118. EXPORT_SYMBOL(register_shrinker);
  119. /*
  120. * Remove one
  121. */
  122. void unregister_shrinker(struct shrinker *shrinker)
  123. {
  124. down_write(&shrinker_rwsem);
  125. list_del(&shrinker->list);
  126. up_write(&shrinker_rwsem);
  127. }
  128. EXPORT_SYMBOL(unregister_shrinker);
  129. #define SHRINK_BATCH 128
  130. /*
  131. * Call the shrink functions to age shrinkable caches
  132. *
  133. * Here we assume it costs one seek to replace a lru page and that it also
  134. * takes a seek to recreate a cache object. With this in mind we age equal
  135. * percentages of the lru and ageable caches. This should balance the seeks
  136. * generated by these structures.
  137. *
  138. * If the vm encountered mapped pages on the LRU it increase the pressure on
  139. * slab to avoid swapping.
  140. *
  141. * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
  142. *
  143. * `lru_pages' represents the number of on-LRU pages in all the zones which
  144. * are eligible for the caller's allocation attempt. It is used for balancing
  145. * slab reclaim versus page reclaim.
  146. *
  147. * Returns the number of slab objects which we shrunk.
  148. */
  149. unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
  150. unsigned long lru_pages)
  151. {
  152. struct shrinker *shrinker;
  153. unsigned long ret = 0;
  154. if (scanned == 0)
  155. scanned = SWAP_CLUSTER_MAX;
  156. if (!down_read_trylock(&shrinker_rwsem))
  157. return 1; /* Assume we'll be able to shrink next time */
  158. list_for_each_entry(shrinker, &shrinker_list, list) {
  159. unsigned long long delta;
  160. unsigned long total_scan;
  161. unsigned long max_pass = (*shrinker->shrink)(0, gfp_mask);
  162. delta = (4 * scanned) / shrinker->seeks;
  163. delta *= max_pass;
  164. do_div(delta, lru_pages + 1);
  165. shrinker->nr += delta;
  166. if (shrinker->nr < 0) {
  167. printk(KERN_ERR "%s: nr=%ld\n",
  168. __func__, shrinker->nr);
  169. shrinker->nr = max_pass;
  170. }
  171. /*
  172. * Avoid risking looping forever due to too large nr value:
  173. * never try to free more than twice the estimate number of
  174. * freeable entries.
  175. */
  176. if (shrinker->nr > max_pass * 2)
  177. shrinker->nr = max_pass * 2;
  178. total_scan = shrinker->nr;
  179. shrinker->nr = 0;
  180. while (total_scan >= SHRINK_BATCH) {
  181. long this_scan = SHRINK_BATCH;
  182. int shrink_ret;
  183. int nr_before;
  184. nr_before = (*shrinker->shrink)(0, gfp_mask);
  185. shrink_ret = (*shrinker->shrink)(this_scan, gfp_mask);
  186. if (shrink_ret == -1)
  187. break;
  188. if (shrink_ret < nr_before)
  189. ret += nr_before - shrink_ret;
  190. count_vm_events(SLABS_SCANNED, this_scan);
  191. total_scan -= this_scan;
  192. cond_resched();
  193. }
  194. shrinker->nr += total_scan;
  195. }
  196. up_read(&shrinker_rwsem);
  197. return ret;
  198. }
  199. /* Called without lock on whether page is mapped, so answer is unstable */
  200. static inline int page_mapping_inuse(struct page *page)
  201. {
  202. struct address_space *mapping;
  203. /* Page is in somebody's page tables. */
  204. if (page_mapped(page))
  205. return 1;
  206. /* Be more reluctant to reclaim swapcache than pagecache */
  207. if (PageSwapCache(page))
  208. return 1;
  209. mapping = page_mapping(page);
  210. if (!mapping)
  211. return 0;
  212. /* File is mmap'd by somebody? */
  213. return mapping_mapped(mapping);
  214. }
  215. static inline int is_page_cache_freeable(struct page *page)
  216. {
  217. return page_count(page) - !!PagePrivate(page) == 2;
  218. }
  219. static int may_write_to_queue(struct backing_dev_info *bdi)
  220. {
  221. if (current->flags & PF_SWAPWRITE)
  222. return 1;
  223. if (!bdi_write_congested(bdi))
  224. return 1;
  225. if (bdi == current->backing_dev_info)
  226. return 1;
  227. return 0;
  228. }
  229. /*
  230. * We detected a synchronous write error writing a page out. Probably
  231. * -ENOSPC. We need to propagate that into the address_space for a subsequent
  232. * fsync(), msync() or close().
  233. *
  234. * The tricky part is that after writepage we cannot touch the mapping: nothing
  235. * prevents it from being freed up. But we have a ref on the page and once
  236. * that page is locked, the mapping is pinned.
  237. *
  238. * We're allowed to run sleeping lock_page() here because we know the caller has
  239. * __GFP_FS.
  240. */
  241. static void handle_write_error(struct address_space *mapping,
  242. struct page *page, int error)
  243. {
  244. lock_page(page);
  245. if (page_mapping(page) == mapping)
  246. mapping_set_error(mapping, error);
  247. unlock_page(page);
  248. }
  249. /* Request for sync pageout. */
  250. enum pageout_io {
  251. PAGEOUT_IO_ASYNC,
  252. PAGEOUT_IO_SYNC,
  253. };
  254. /* possible outcome of pageout() */
  255. typedef enum {
  256. /* failed to write page out, page is locked */
  257. PAGE_KEEP,
  258. /* move page to the active list, page is locked */
  259. PAGE_ACTIVATE,
  260. /* page has been sent to the disk successfully, page is unlocked */
  261. PAGE_SUCCESS,
  262. /* page is clean and locked */
  263. PAGE_CLEAN,
  264. } pageout_t;
  265. /*
  266. * pageout is called by shrink_page_list() for each dirty page.
  267. * Calls ->writepage().
  268. */
  269. static pageout_t pageout(struct page *page, struct address_space *mapping,
  270. enum pageout_io sync_writeback)
  271. {
  272. /*
  273. * If the page is dirty, only perform writeback if that write
  274. * will be non-blocking. To prevent this allocation from being
  275. * stalled by pagecache activity. But note that there may be
  276. * stalls if we need to run get_block(). We could test
  277. * PagePrivate for that.
  278. *
  279. * If this process is currently in generic_file_write() against
  280. * this page's queue, we can perform writeback even if that
  281. * will block.
  282. *
  283. * If the page is swapcache, write it back even if that would
  284. * block, for some throttling. This happens by accident, because
  285. * swap_backing_dev_info is bust: it doesn't reflect the
  286. * congestion state of the swapdevs. Easy to fix, if needed.
  287. * See swapfile.c:page_queue_congested().
  288. */
  289. if (!is_page_cache_freeable(page))
  290. return PAGE_KEEP;
  291. if (!mapping) {
  292. /*
  293. * Some data journaling orphaned pages can have
  294. * page->mapping == NULL while being dirty with clean buffers.
  295. */
  296. if (PagePrivate(page)) {
  297. if (try_to_free_buffers(page)) {
  298. ClearPageDirty(page);
  299. printk("%s: orphaned page\n", __func__);
  300. return PAGE_CLEAN;
  301. }
  302. }
  303. return PAGE_KEEP;
  304. }
  305. if (mapping->a_ops->writepage == NULL)
  306. return PAGE_ACTIVATE;
  307. if (!may_write_to_queue(mapping->backing_dev_info))
  308. return PAGE_KEEP;
  309. if (clear_page_dirty_for_io(page)) {
  310. int res;
  311. struct writeback_control wbc = {
  312. .sync_mode = WB_SYNC_NONE,
  313. .nr_to_write = SWAP_CLUSTER_MAX,
  314. .range_start = 0,
  315. .range_end = LLONG_MAX,
  316. .nonblocking = 1,
  317. .for_reclaim = 1,
  318. };
  319. SetPageReclaim(page);
  320. res = mapping->a_ops->writepage(page, &wbc);
  321. if (res < 0)
  322. handle_write_error(mapping, page, res);
  323. if (res == AOP_WRITEPAGE_ACTIVATE) {
  324. ClearPageReclaim(page);
  325. return PAGE_ACTIVATE;
  326. }
  327. /*
  328. * Wait on writeback if requested to. This happens when
  329. * direct reclaiming a large contiguous area and the
  330. * first attempt to free a range of pages fails.
  331. */
  332. if (PageWriteback(page) && sync_writeback == PAGEOUT_IO_SYNC)
  333. wait_on_page_writeback(page);
  334. if (!PageWriteback(page)) {
  335. /* synchronous write or broken a_ops? */
  336. ClearPageReclaim(page);
  337. }
  338. inc_zone_page_state(page, NR_VMSCAN_WRITE);
  339. return PAGE_SUCCESS;
  340. }
  341. return PAGE_CLEAN;
  342. }
  343. /*
  344. * Same as remove_mapping, but if the page is removed from the mapping, it
  345. * gets returned with a refcount of 0.
  346. */
  347. static int __remove_mapping(struct address_space *mapping, struct page *page)
  348. {
  349. BUG_ON(!PageLocked(page));
  350. BUG_ON(mapping != page_mapping(page));
  351. spin_lock_irq(&mapping->tree_lock);
  352. /*
  353. * The non racy check for a busy page.
  354. *
  355. * Must be careful with the order of the tests. When someone has
  356. * a ref to the page, it may be possible that they dirty it then
  357. * drop the reference. So if PageDirty is tested before page_count
  358. * here, then the following race may occur:
  359. *
  360. * get_user_pages(&page);
  361. * [user mapping goes away]
  362. * write_to(page);
  363. * !PageDirty(page) [good]
  364. * SetPageDirty(page);
  365. * put_page(page);
  366. * !page_count(page) [good, discard it]
  367. *
  368. * [oops, our write_to data is lost]
  369. *
  370. * Reversing the order of the tests ensures such a situation cannot
  371. * escape unnoticed. The smp_rmb is needed to ensure the page->flags
  372. * load is not satisfied before that of page->_count.
  373. *
  374. * Note that if SetPageDirty is always performed via set_page_dirty,
  375. * and thus under tree_lock, then this ordering is not required.
  376. */
  377. if (!page_freeze_refs(page, 2))
  378. goto cannot_free;
  379. /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */
  380. if (unlikely(PageDirty(page))) {
  381. page_unfreeze_refs(page, 2);
  382. goto cannot_free;
  383. }
  384. if (PageSwapCache(page)) {
  385. swp_entry_t swap = { .val = page_private(page) };
  386. __delete_from_swap_cache(page);
  387. spin_unlock_irq(&mapping->tree_lock);
  388. swap_free(swap);
  389. } else {
  390. __remove_from_page_cache(page);
  391. spin_unlock_irq(&mapping->tree_lock);
  392. }
  393. return 1;
  394. cannot_free:
  395. spin_unlock_irq(&mapping->tree_lock);
  396. return 0;
  397. }
  398. /*
  399. * Attempt to detach a locked page from its ->mapping. If it is dirty or if
  400. * someone else has a ref on the page, abort and return 0. If it was
  401. * successfully detached, return 1. Assumes the caller has a single ref on
  402. * this page.
  403. */
  404. int remove_mapping(struct address_space *mapping, struct page *page)
  405. {
  406. if (__remove_mapping(mapping, page)) {
  407. /*
  408. * Unfreezing the refcount with 1 rather than 2 effectively
  409. * drops the pagecache ref for us without requiring another
  410. * atomic operation.
  411. */
  412. page_unfreeze_refs(page, 1);
  413. return 1;
  414. }
  415. return 0;
  416. }
  417. /*
  418. * shrink_page_list() returns the number of reclaimed pages
  419. */
  420. static unsigned long shrink_page_list(struct list_head *page_list,
  421. struct scan_control *sc,
  422. enum pageout_io sync_writeback)
  423. {
  424. LIST_HEAD(ret_pages);
  425. struct pagevec freed_pvec;
  426. int pgactivate = 0;
  427. unsigned long nr_reclaimed = 0;
  428. cond_resched();
  429. pagevec_init(&freed_pvec, 1);
  430. while (!list_empty(page_list)) {
  431. struct address_space *mapping;
  432. struct page *page;
  433. int may_enter_fs;
  434. int referenced;
  435. cond_resched();
  436. page = lru_to_page(page_list);
  437. list_del(&page->lru);
  438. if (!trylock_page(page))
  439. goto keep;
  440. VM_BUG_ON(PageActive(page));
  441. sc->nr_scanned++;
  442. if (!sc->may_swap && page_mapped(page))
  443. goto keep_locked;
  444. /* Double the slab pressure for mapped and swapcache pages */
  445. if (page_mapped(page) || PageSwapCache(page))
  446. sc->nr_scanned++;
  447. may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
  448. (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
  449. if (PageWriteback(page)) {
  450. /*
  451. * Synchronous reclaim is performed in two passes,
  452. * first an asynchronous pass over the list to
  453. * start parallel writeback, and a second synchronous
  454. * pass to wait for the IO to complete. Wait here
  455. * for any page for which writeback has already
  456. * started.
  457. */
  458. if (sync_writeback == PAGEOUT_IO_SYNC && may_enter_fs)
  459. wait_on_page_writeback(page);
  460. else
  461. goto keep_locked;
  462. }
  463. referenced = page_referenced(page, 1, sc->mem_cgroup);
  464. /* In active use or really unfreeable? Activate it. */
  465. if (sc->order <= PAGE_ALLOC_COSTLY_ORDER &&
  466. referenced && page_mapping_inuse(page))
  467. goto activate_locked;
  468. #ifdef CONFIG_SWAP
  469. /*
  470. * Anonymous process memory has backing store?
  471. * Try to allocate it some swap space here.
  472. */
  473. if (PageAnon(page) && !PageSwapCache(page))
  474. if (!add_to_swap(page, GFP_ATOMIC))
  475. goto activate_locked;
  476. #endif /* CONFIG_SWAP */
  477. mapping = page_mapping(page);
  478. /*
  479. * The page is mapped into the page tables of one or more
  480. * processes. Try to unmap it here.
  481. */
  482. if (page_mapped(page) && mapping) {
  483. switch (try_to_unmap(page, 0)) {
  484. case SWAP_FAIL:
  485. goto activate_locked;
  486. case SWAP_AGAIN:
  487. goto keep_locked;
  488. case SWAP_SUCCESS:
  489. ; /* try to free the page below */
  490. }
  491. }
  492. if (PageDirty(page)) {
  493. if (sc->order <= PAGE_ALLOC_COSTLY_ORDER && referenced)
  494. goto keep_locked;
  495. if (!may_enter_fs)
  496. goto keep_locked;
  497. if (!sc->may_writepage)
  498. goto keep_locked;
  499. /* Page is dirty, try to write it out here */
  500. switch (pageout(page, mapping, sync_writeback)) {
  501. case PAGE_KEEP:
  502. goto keep_locked;
  503. case PAGE_ACTIVATE:
  504. goto activate_locked;
  505. case PAGE_SUCCESS:
  506. if (PageWriteback(page) || PageDirty(page))
  507. goto keep;
  508. /*
  509. * A synchronous write - probably a ramdisk. Go
  510. * ahead and try to reclaim the page.
  511. */
  512. if (!trylock_page(page))
  513. goto keep;
  514. if (PageDirty(page) || PageWriteback(page))
  515. goto keep_locked;
  516. mapping = page_mapping(page);
  517. case PAGE_CLEAN:
  518. ; /* try to free the page below */
  519. }
  520. }
  521. /*
  522. * If the page has buffers, try to free the buffer mappings
  523. * associated with this page. If we succeed we try to free
  524. * the page as well.
  525. *
  526. * We do this even if the page is PageDirty().
  527. * try_to_release_page() does not perform I/O, but it is
  528. * possible for a page to have PageDirty set, but it is actually
  529. * clean (all its buffers are clean). This happens if the
  530. * buffers were written out directly, with submit_bh(). ext3
  531. * will do this, as well as the blockdev mapping.
  532. * try_to_release_page() will discover that cleanness and will
  533. * drop the buffers and mark the page clean - it can be freed.
  534. *
  535. * Rarely, pages can have buffers and no ->mapping. These are
  536. * the pages which were not successfully invalidated in
  537. * truncate_complete_page(). We try to drop those buffers here
  538. * and if that worked, and the page is no longer mapped into
  539. * process address space (page_count == 1) it can be freed.
  540. * Otherwise, leave the page on the LRU so it is swappable.
  541. */
  542. if (PagePrivate(page)) {
  543. if (!try_to_release_page(page, sc->gfp_mask))
  544. goto activate_locked;
  545. if (!mapping && page_count(page) == 1) {
  546. unlock_page(page);
  547. if (put_page_testzero(page))
  548. goto free_it;
  549. else {
  550. /*
  551. * rare race with speculative reference.
  552. * the speculative reference will free
  553. * this page shortly, so we may
  554. * increment nr_reclaimed here (and
  555. * leave it off the LRU).
  556. */
  557. nr_reclaimed++;
  558. continue;
  559. }
  560. }
  561. }
  562. if (!mapping || !__remove_mapping(mapping, page))
  563. goto keep_locked;
  564. unlock_page(page);
  565. free_it:
  566. nr_reclaimed++;
  567. if (!pagevec_add(&freed_pvec, page)) {
  568. __pagevec_free(&freed_pvec);
  569. pagevec_reinit(&freed_pvec);
  570. }
  571. continue;
  572. activate_locked:
  573. SetPageActive(page);
  574. pgactivate++;
  575. keep_locked:
  576. unlock_page(page);
  577. keep:
  578. list_add(&page->lru, &ret_pages);
  579. VM_BUG_ON(PageLRU(page));
  580. }
  581. list_splice(&ret_pages, page_list);
  582. if (pagevec_count(&freed_pvec))
  583. __pagevec_free(&freed_pvec);
  584. count_vm_events(PGACTIVATE, pgactivate);
  585. return nr_reclaimed;
  586. }
  587. /* LRU Isolation modes. */
  588. #define ISOLATE_INACTIVE 0 /* Isolate inactive pages. */
  589. #define ISOLATE_ACTIVE 1 /* Isolate active pages. */
  590. #define ISOLATE_BOTH 2 /* Isolate both active and inactive pages. */
  591. /*
  592. * Attempt to remove the specified page from its LRU. Only take this page
  593. * if it is of the appropriate PageActive status. Pages which are being
  594. * freed elsewhere are also ignored.
  595. *
  596. * page: page to consider
  597. * mode: one of the LRU isolation modes defined above
  598. *
  599. * returns 0 on success, -ve errno on failure.
  600. */
  601. int __isolate_lru_page(struct page *page, int mode)
  602. {
  603. int ret = -EINVAL;
  604. /* Only take pages on the LRU. */
  605. if (!PageLRU(page))
  606. return ret;
  607. /*
  608. * When checking the active state, we need to be sure we are
  609. * dealing with comparible boolean values. Take the logical not
  610. * of each.
  611. */
  612. if (mode != ISOLATE_BOTH && (!PageActive(page) != !mode))
  613. return ret;
  614. ret = -EBUSY;
  615. if (likely(get_page_unless_zero(page))) {
  616. /*
  617. * Be careful not to clear PageLRU until after we're
  618. * sure the page is not being freed elsewhere -- the
  619. * page release code relies on it.
  620. */
  621. ClearPageLRU(page);
  622. ret = 0;
  623. }
  624. return ret;
  625. }
  626. /*
  627. * zone->lru_lock is heavily contended. Some of the functions that
  628. * shrink the lists perform better by taking out a batch of pages
  629. * and working on them outside the LRU lock.
  630. *
  631. * For pagecache intensive workloads, this function is the hottest
  632. * spot in the kernel (apart from copy_*_user functions).
  633. *
  634. * Appropriate locks must be held before calling this function.
  635. *
  636. * @nr_to_scan: The number of pages to look through on the list.
  637. * @src: The LRU list to pull pages off.
  638. * @dst: The temp list to put pages on to.
  639. * @scanned: The number of pages that were scanned.
  640. * @order: The caller's attempted allocation order
  641. * @mode: One of the LRU isolation modes
  642. *
  643. * returns how many pages were moved onto *@dst.
  644. */
  645. static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
  646. struct list_head *src, struct list_head *dst,
  647. unsigned long *scanned, int order, int mode)
  648. {
  649. unsigned long nr_taken = 0;
  650. unsigned long scan;
  651. for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
  652. struct page *page;
  653. unsigned long pfn;
  654. unsigned long end_pfn;
  655. unsigned long page_pfn;
  656. int zone_id;
  657. page = lru_to_page(src);
  658. prefetchw_prev_lru_page(page, src, flags);
  659. VM_BUG_ON(!PageLRU(page));
  660. switch (__isolate_lru_page(page, mode)) {
  661. case 0:
  662. list_move(&page->lru, dst);
  663. nr_taken++;
  664. break;
  665. case -EBUSY:
  666. /* else it is being freed elsewhere */
  667. list_move(&page->lru, src);
  668. continue;
  669. default:
  670. BUG();
  671. }
  672. if (!order)
  673. continue;
  674. /*
  675. * Attempt to take all pages in the order aligned region
  676. * surrounding the tag page. Only take those pages of
  677. * the same active state as that tag page. We may safely
  678. * round the target page pfn down to the requested order
  679. * as the mem_map is guarenteed valid out to MAX_ORDER,
  680. * where that page is in a different zone we will detect
  681. * it from its zone id and abort this block scan.
  682. */
  683. zone_id = page_zone_id(page);
  684. page_pfn = page_to_pfn(page);
  685. pfn = page_pfn & ~((1 << order) - 1);
  686. end_pfn = pfn + (1 << order);
  687. for (; pfn < end_pfn; pfn++) {
  688. struct page *cursor_page;
  689. /* The target page is in the block, ignore it. */
  690. if (unlikely(pfn == page_pfn))
  691. continue;
  692. /* Avoid holes within the zone. */
  693. if (unlikely(!pfn_valid_within(pfn)))
  694. break;
  695. cursor_page = pfn_to_page(pfn);
  696. /* Check that we have not crossed a zone boundary. */
  697. if (unlikely(page_zone_id(cursor_page) != zone_id))
  698. continue;
  699. switch (__isolate_lru_page(cursor_page, mode)) {
  700. case 0:
  701. list_move(&cursor_page->lru, dst);
  702. nr_taken++;
  703. scan++;
  704. break;
  705. case -EBUSY:
  706. /* else it is being freed elsewhere */
  707. list_move(&cursor_page->lru, src);
  708. default:
  709. break;
  710. }
  711. }
  712. }
  713. *scanned = scan;
  714. return nr_taken;
  715. }
  716. static unsigned long isolate_pages_global(unsigned long nr,
  717. struct list_head *dst,
  718. unsigned long *scanned, int order,
  719. int mode, struct zone *z,
  720. struct mem_cgroup *mem_cont,
  721. int active)
  722. {
  723. if (active)
  724. return isolate_lru_pages(nr, &z->active_list, dst,
  725. scanned, order, mode);
  726. else
  727. return isolate_lru_pages(nr, &z->inactive_list, dst,
  728. scanned, order, mode);
  729. }
  730. /*
  731. * clear_active_flags() is a helper for shrink_active_list(), clearing
  732. * any active bits from the pages in the list.
  733. */
  734. static unsigned long clear_active_flags(struct list_head *page_list)
  735. {
  736. int nr_active = 0;
  737. struct page *page;
  738. list_for_each_entry(page, page_list, lru)
  739. if (PageActive(page)) {
  740. ClearPageActive(page);
  741. nr_active++;
  742. }
  743. return nr_active;
  744. }
  745. /*
  746. * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
  747. * of reclaimed pages
  748. */
  749. static unsigned long shrink_inactive_list(unsigned long max_scan,
  750. struct zone *zone, struct scan_control *sc)
  751. {
  752. LIST_HEAD(page_list);
  753. struct pagevec pvec;
  754. unsigned long nr_scanned = 0;
  755. unsigned long nr_reclaimed = 0;
  756. pagevec_init(&pvec, 1);
  757. lru_add_drain();
  758. spin_lock_irq(&zone->lru_lock);
  759. do {
  760. struct page *page;
  761. unsigned long nr_taken;
  762. unsigned long nr_scan;
  763. unsigned long nr_freed;
  764. unsigned long nr_active;
  765. nr_taken = sc->isolate_pages(sc->swap_cluster_max,
  766. &page_list, &nr_scan, sc->order,
  767. (sc->order > PAGE_ALLOC_COSTLY_ORDER)?
  768. ISOLATE_BOTH : ISOLATE_INACTIVE,
  769. zone, sc->mem_cgroup, 0);
  770. nr_active = clear_active_flags(&page_list);
  771. __count_vm_events(PGDEACTIVATE, nr_active);
  772. __mod_zone_page_state(zone, NR_ACTIVE, -nr_active);
  773. __mod_zone_page_state(zone, NR_INACTIVE,
  774. -(nr_taken - nr_active));
  775. if (scan_global_lru(sc))
  776. zone->pages_scanned += nr_scan;
  777. spin_unlock_irq(&zone->lru_lock);
  778. nr_scanned += nr_scan;
  779. nr_freed = shrink_page_list(&page_list, sc, PAGEOUT_IO_ASYNC);
  780. /*
  781. * If we are direct reclaiming for contiguous pages and we do
  782. * not reclaim everything in the list, try again and wait
  783. * for IO to complete. This will stall high-order allocations
  784. * but that should be acceptable to the caller
  785. */
  786. if (nr_freed < nr_taken && !current_is_kswapd() &&
  787. sc->order > PAGE_ALLOC_COSTLY_ORDER) {
  788. congestion_wait(WRITE, HZ/10);
  789. /*
  790. * The attempt at page out may have made some
  791. * of the pages active, mark them inactive again.
  792. */
  793. nr_active = clear_active_flags(&page_list);
  794. count_vm_events(PGDEACTIVATE, nr_active);
  795. nr_freed += shrink_page_list(&page_list, sc,
  796. PAGEOUT_IO_SYNC);
  797. }
  798. nr_reclaimed += nr_freed;
  799. local_irq_disable();
  800. if (current_is_kswapd()) {
  801. __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scan);
  802. __count_vm_events(KSWAPD_STEAL, nr_freed);
  803. } else if (scan_global_lru(sc))
  804. __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scan);
  805. __count_zone_vm_events(PGSTEAL, zone, nr_freed);
  806. if (nr_taken == 0)
  807. goto done;
  808. spin_lock(&zone->lru_lock);
  809. /*
  810. * Put back any unfreeable pages.
  811. */
  812. while (!list_empty(&page_list)) {
  813. page = lru_to_page(&page_list);
  814. VM_BUG_ON(PageLRU(page));
  815. SetPageLRU(page);
  816. list_del(&page->lru);
  817. if (PageActive(page))
  818. add_page_to_active_list(zone, page);
  819. else
  820. add_page_to_inactive_list(zone, page);
  821. if (!pagevec_add(&pvec, page)) {
  822. spin_unlock_irq(&zone->lru_lock);
  823. __pagevec_release(&pvec);
  824. spin_lock_irq(&zone->lru_lock);
  825. }
  826. }
  827. } while (nr_scanned < max_scan);
  828. spin_unlock(&zone->lru_lock);
  829. done:
  830. local_irq_enable();
  831. pagevec_release(&pvec);
  832. return nr_reclaimed;
  833. }
  834. /*
  835. * We are about to scan this zone at a certain priority level. If that priority
  836. * level is smaller (ie: more urgent) than the previous priority, then note
  837. * that priority level within the zone. This is done so that when the next
  838. * process comes in to scan this zone, it will immediately start out at this
  839. * priority level rather than having to build up its own scanning priority.
  840. * Here, this priority affects only the reclaim-mapped threshold.
  841. */
  842. static inline void note_zone_scanning_priority(struct zone *zone, int priority)
  843. {
  844. if (priority < zone->prev_priority)
  845. zone->prev_priority = priority;
  846. }
  847. static inline int zone_is_near_oom(struct zone *zone)
  848. {
  849. return zone->pages_scanned >= (zone_page_state(zone, NR_ACTIVE)
  850. + zone_page_state(zone, NR_INACTIVE))*3;
  851. }
  852. /*
  853. * Determine we should try to reclaim mapped pages.
  854. * This is called only when sc->mem_cgroup is NULL.
  855. */
  856. static int calc_reclaim_mapped(struct scan_control *sc, struct zone *zone,
  857. int priority)
  858. {
  859. long mapped_ratio;
  860. long distress;
  861. long swap_tendency;
  862. long imbalance;
  863. int reclaim_mapped = 0;
  864. int prev_priority;
  865. if (scan_global_lru(sc) && zone_is_near_oom(zone))
  866. return 1;
  867. /*
  868. * `distress' is a measure of how much trouble we're having
  869. * reclaiming pages. 0 -> no problems. 100 -> great trouble.
  870. */
  871. if (scan_global_lru(sc))
  872. prev_priority = zone->prev_priority;
  873. else
  874. prev_priority = mem_cgroup_get_reclaim_priority(sc->mem_cgroup);
  875. distress = 100 >> min(prev_priority, priority);
  876. /*
  877. * The point of this algorithm is to decide when to start
  878. * reclaiming mapped memory instead of just pagecache. Work out
  879. * how much memory
  880. * is mapped.
  881. */
  882. if (scan_global_lru(sc))
  883. mapped_ratio = ((global_page_state(NR_FILE_MAPPED) +
  884. global_page_state(NR_ANON_PAGES)) * 100) /
  885. vm_total_pages;
  886. else
  887. mapped_ratio = mem_cgroup_calc_mapped_ratio(sc->mem_cgroup);
  888. /*
  889. * Now decide how much we really want to unmap some pages. The
  890. * mapped ratio is downgraded - just because there's a lot of
  891. * mapped memory doesn't necessarily mean that page reclaim
  892. * isn't succeeding.
  893. *
  894. * The distress ratio is important - we don't want to start
  895. * going oom.
  896. *
  897. * A 100% value of vm_swappiness overrides this algorithm
  898. * altogether.
  899. */
  900. swap_tendency = mapped_ratio / 2 + distress + sc->swappiness;
  901. /*
  902. * If there's huge imbalance between active and inactive
  903. * (think active 100 times larger than inactive) we should
  904. * become more permissive, or the system will take too much
  905. * cpu before it start swapping during memory pressure.
  906. * Distress is about avoiding early-oom, this is about
  907. * making swappiness graceful despite setting it to low
  908. * values.
  909. *
  910. * Avoid div by zero with nr_inactive+1, and max resulting
  911. * value is vm_total_pages.
  912. */
  913. if (scan_global_lru(sc)) {
  914. imbalance = zone_page_state(zone, NR_ACTIVE);
  915. imbalance /= zone_page_state(zone, NR_INACTIVE) + 1;
  916. } else
  917. imbalance = mem_cgroup_reclaim_imbalance(sc->mem_cgroup);
  918. /*
  919. * Reduce the effect of imbalance if swappiness is low,
  920. * this means for a swappiness very low, the imbalance
  921. * must be much higher than 100 for this logic to make
  922. * the difference.
  923. *
  924. * Max temporary value is vm_total_pages*100.
  925. */
  926. imbalance *= (vm_swappiness + 1);
  927. imbalance /= 100;
  928. /*
  929. * If not much of the ram is mapped, makes the imbalance
  930. * less relevant, it's high priority we refill the inactive
  931. * list with mapped pages only in presence of high ratio of
  932. * mapped pages.
  933. *
  934. * Max temporary value is vm_total_pages*100.
  935. */
  936. imbalance *= mapped_ratio;
  937. imbalance /= 100;
  938. /* apply imbalance feedback to swap_tendency */
  939. swap_tendency += imbalance;
  940. /*
  941. * Now use this metric to decide whether to start moving mapped
  942. * memory onto the inactive list.
  943. */
  944. if (swap_tendency >= 100)
  945. reclaim_mapped = 1;
  946. return reclaim_mapped;
  947. }
  948. /*
  949. * This moves pages from the active list to the inactive list.
  950. *
  951. * We move them the other way if the page is referenced by one or more
  952. * processes, from rmap.
  953. *
  954. * If the pages are mostly unmapped, the processing is fast and it is
  955. * appropriate to hold zone->lru_lock across the whole operation. But if
  956. * the pages are mapped, the processing is slow (page_referenced()) so we
  957. * should drop zone->lru_lock around each page. It's impossible to balance
  958. * this, so instead we remove the pages from the LRU while processing them.
  959. * It is safe to rely on PG_active against the non-LRU pages in here because
  960. * nobody will play with that bit on a non-LRU page.
  961. *
  962. * The downside is that we have to touch page->_count against each page.
  963. * But we had to alter page->flags anyway.
  964. */
  965. static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
  966. struct scan_control *sc, int priority)
  967. {
  968. unsigned long pgmoved;
  969. int pgdeactivate = 0;
  970. unsigned long pgscanned;
  971. LIST_HEAD(l_hold); /* The pages which were snipped off */
  972. LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
  973. LIST_HEAD(l_active); /* Pages to go onto the active_list */
  974. struct page *page;
  975. struct pagevec pvec;
  976. int reclaim_mapped = 0;
  977. if (sc->may_swap)
  978. reclaim_mapped = calc_reclaim_mapped(sc, zone, priority);
  979. lru_add_drain();
  980. spin_lock_irq(&zone->lru_lock);
  981. pgmoved = sc->isolate_pages(nr_pages, &l_hold, &pgscanned, sc->order,
  982. ISOLATE_ACTIVE, zone,
  983. sc->mem_cgroup, 1);
  984. /*
  985. * zone->pages_scanned is used for detect zone's oom
  986. * mem_cgroup remembers nr_scan by itself.
  987. */
  988. if (scan_global_lru(sc))
  989. zone->pages_scanned += pgscanned;
  990. __mod_zone_page_state(zone, NR_ACTIVE, -pgmoved);
  991. spin_unlock_irq(&zone->lru_lock);
  992. while (!list_empty(&l_hold)) {
  993. cond_resched();
  994. page = lru_to_page(&l_hold);
  995. list_del(&page->lru);
  996. if (page_mapped(page)) {
  997. if (!reclaim_mapped ||
  998. (total_swap_pages == 0 && PageAnon(page)) ||
  999. page_referenced(page, 0, sc->mem_cgroup)) {
  1000. list_add(&page->lru, &l_active);
  1001. continue;
  1002. }
  1003. }
  1004. list_add(&page->lru, &l_inactive);
  1005. }
  1006. pagevec_init(&pvec, 1);
  1007. pgmoved = 0;
  1008. spin_lock_irq(&zone->lru_lock);
  1009. while (!list_empty(&l_inactive)) {
  1010. page = lru_to_page(&l_inactive);
  1011. prefetchw_prev_lru_page(page, &l_inactive, flags);
  1012. VM_BUG_ON(PageLRU(page));
  1013. SetPageLRU(page);
  1014. VM_BUG_ON(!PageActive(page));
  1015. ClearPageActive(page);
  1016. list_move(&page->lru, &zone->inactive_list);
  1017. mem_cgroup_move_lists(page, false);
  1018. pgmoved++;
  1019. if (!pagevec_add(&pvec, page)) {
  1020. __mod_zone_page_state(zone, NR_INACTIVE, pgmoved);
  1021. spin_unlock_irq(&zone->lru_lock);
  1022. pgdeactivate += pgmoved;
  1023. pgmoved = 0;
  1024. if (buffer_heads_over_limit)
  1025. pagevec_strip(&pvec);
  1026. __pagevec_release(&pvec);
  1027. spin_lock_irq(&zone->lru_lock);
  1028. }
  1029. }
  1030. __mod_zone_page_state(zone, NR_INACTIVE, pgmoved);
  1031. pgdeactivate += pgmoved;
  1032. if (buffer_heads_over_limit) {
  1033. spin_unlock_irq(&zone->lru_lock);
  1034. pagevec_strip(&pvec);
  1035. spin_lock_irq(&zone->lru_lock);
  1036. }
  1037. pgmoved = 0;
  1038. while (!list_empty(&l_active)) {
  1039. page = lru_to_page(&l_active);
  1040. prefetchw_prev_lru_page(page, &l_active, flags);
  1041. VM_BUG_ON(PageLRU(page));
  1042. SetPageLRU(page);
  1043. VM_BUG_ON(!PageActive(page));
  1044. list_move(&page->lru, &zone->active_list);
  1045. mem_cgroup_move_lists(page, true);
  1046. pgmoved++;
  1047. if (!pagevec_add(&pvec, page)) {
  1048. __mod_zone_page_state(zone, NR_ACTIVE, pgmoved);
  1049. pgmoved = 0;
  1050. spin_unlock_irq(&zone->lru_lock);
  1051. __pagevec_release(&pvec);
  1052. spin_lock_irq(&zone->lru_lock);
  1053. }
  1054. }
  1055. __mod_zone_page_state(zone, NR_ACTIVE, pgmoved);
  1056. __count_zone_vm_events(PGREFILL, zone, pgscanned);
  1057. __count_vm_events(PGDEACTIVATE, pgdeactivate);
  1058. spin_unlock_irq(&zone->lru_lock);
  1059. pagevec_release(&pvec);
  1060. }
  1061. /*
  1062. * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
  1063. */
  1064. static unsigned long shrink_zone(int priority, struct zone *zone,
  1065. struct scan_control *sc)
  1066. {
  1067. unsigned long nr_active;
  1068. unsigned long nr_inactive;
  1069. unsigned long nr_to_scan;
  1070. unsigned long nr_reclaimed = 0;
  1071. if (scan_global_lru(sc)) {
  1072. /*
  1073. * Add one to nr_to_scan just to make sure that the kernel
  1074. * will slowly sift through the active list.
  1075. */
  1076. zone->nr_scan_active +=
  1077. (zone_page_state(zone, NR_ACTIVE) >> priority) + 1;
  1078. nr_active = zone->nr_scan_active;
  1079. zone->nr_scan_inactive +=
  1080. (zone_page_state(zone, NR_INACTIVE) >> priority) + 1;
  1081. nr_inactive = zone->nr_scan_inactive;
  1082. if (nr_inactive >= sc->swap_cluster_max)
  1083. zone->nr_scan_inactive = 0;
  1084. else
  1085. nr_inactive = 0;
  1086. if (nr_active >= sc->swap_cluster_max)
  1087. zone->nr_scan_active = 0;
  1088. else
  1089. nr_active = 0;
  1090. } else {
  1091. /*
  1092. * This reclaim occurs not because zone memory shortage but
  1093. * because memory controller hits its limit.
  1094. * Then, don't modify zone reclaim related data.
  1095. */
  1096. nr_active = mem_cgroup_calc_reclaim_active(sc->mem_cgroup,
  1097. zone, priority);
  1098. nr_inactive = mem_cgroup_calc_reclaim_inactive(sc->mem_cgroup,
  1099. zone, priority);
  1100. }
  1101. while (nr_active || nr_inactive) {
  1102. if (nr_active) {
  1103. nr_to_scan = min(nr_active,
  1104. (unsigned long)sc->swap_cluster_max);
  1105. nr_active -= nr_to_scan;
  1106. shrink_active_list(nr_to_scan, zone, sc, priority);
  1107. }
  1108. if (nr_inactive) {
  1109. nr_to_scan = min(nr_inactive,
  1110. (unsigned long)sc->swap_cluster_max);
  1111. nr_inactive -= nr_to_scan;
  1112. nr_reclaimed += shrink_inactive_list(nr_to_scan, zone,
  1113. sc);
  1114. }
  1115. }
  1116. throttle_vm_writeout(sc->gfp_mask);
  1117. return nr_reclaimed;
  1118. }
  1119. /*
  1120. * This is the direct reclaim path, for page-allocating processes. We only
  1121. * try to reclaim pages from zones which will satisfy the caller's allocation
  1122. * request.
  1123. *
  1124. * We reclaim from a zone even if that zone is over pages_high. Because:
  1125. * a) The caller may be trying to free *extra* pages to satisfy a higher-order
  1126. * allocation or
  1127. * b) The zones may be over pages_high but they must go *over* pages_high to
  1128. * satisfy the `incremental min' zone defense algorithm.
  1129. *
  1130. * Returns the number of reclaimed pages.
  1131. *
  1132. * If a zone is deemed to be full of pinned pages then just give it a light
  1133. * scan then give up on it.
  1134. */
  1135. static unsigned long shrink_zones(int priority, struct zonelist *zonelist,
  1136. struct scan_control *sc)
  1137. {
  1138. enum zone_type high_zoneidx = gfp_zone(sc->gfp_mask);
  1139. unsigned long nr_reclaimed = 0;
  1140. struct zoneref *z;
  1141. struct zone *zone;
  1142. sc->all_unreclaimable = 1;
  1143. for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
  1144. if (!populated_zone(zone))
  1145. continue;
  1146. /*
  1147. * Take care memory controller reclaiming has small influence
  1148. * to global LRU.
  1149. */
  1150. if (scan_global_lru(sc)) {
  1151. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1152. continue;
  1153. note_zone_scanning_priority(zone, priority);
  1154. if (zone_is_all_unreclaimable(zone) &&
  1155. priority != DEF_PRIORITY)
  1156. continue; /* Let kswapd poll it */
  1157. sc->all_unreclaimable = 0;
  1158. } else {
  1159. /*
  1160. * Ignore cpuset limitation here. We just want to reduce
  1161. * # of used pages by us regardless of memory shortage.
  1162. */
  1163. sc->all_unreclaimable = 0;
  1164. mem_cgroup_note_reclaim_priority(sc->mem_cgroup,
  1165. priority);
  1166. }
  1167. nr_reclaimed += shrink_zone(priority, zone, sc);
  1168. }
  1169. return nr_reclaimed;
  1170. }
  1171. /*
  1172. * This is the main entry point to direct page reclaim.
  1173. *
  1174. * If a full scan of the inactive list fails to free enough memory then we
  1175. * are "out of memory" and something needs to be killed.
  1176. *
  1177. * If the caller is !__GFP_FS then the probability of a failure is reasonably
  1178. * high - the zone may be full of dirty or under-writeback pages, which this
  1179. * caller can't do much about. We kick pdflush and take explicit naps in the
  1180. * hope that some of these pages can be written. But if the allocating task
  1181. * holds filesystem locks which prevent writeout this might not work, and the
  1182. * allocation attempt will fail.
  1183. *
  1184. * returns: 0, if no pages reclaimed
  1185. * else, the number of pages reclaimed
  1186. */
  1187. static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
  1188. struct scan_control *sc)
  1189. {
  1190. int priority;
  1191. unsigned long ret = 0;
  1192. unsigned long total_scanned = 0;
  1193. unsigned long nr_reclaimed = 0;
  1194. struct reclaim_state *reclaim_state = current->reclaim_state;
  1195. unsigned long lru_pages = 0;
  1196. struct zoneref *z;
  1197. struct zone *zone;
  1198. enum zone_type high_zoneidx = gfp_zone(sc->gfp_mask);
  1199. delayacct_freepages_start();
  1200. if (scan_global_lru(sc))
  1201. count_vm_event(ALLOCSTALL);
  1202. /*
  1203. * mem_cgroup will not do shrink_slab.
  1204. */
  1205. if (scan_global_lru(sc)) {
  1206. for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
  1207. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1208. continue;
  1209. lru_pages += zone_page_state(zone, NR_ACTIVE)
  1210. + zone_page_state(zone, NR_INACTIVE);
  1211. }
  1212. }
  1213. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  1214. sc->nr_scanned = 0;
  1215. if (!priority)
  1216. disable_swap_token();
  1217. nr_reclaimed += shrink_zones(priority, zonelist, sc);
  1218. /*
  1219. * Don't shrink slabs when reclaiming memory from
  1220. * over limit cgroups
  1221. */
  1222. if (scan_global_lru(sc)) {
  1223. shrink_slab(sc->nr_scanned, sc->gfp_mask, lru_pages);
  1224. if (reclaim_state) {
  1225. nr_reclaimed += reclaim_state->reclaimed_slab;
  1226. reclaim_state->reclaimed_slab = 0;
  1227. }
  1228. }
  1229. total_scanned += sc->nr_scanned;
  1230. if (nr_reclaimed >= sc->swap_cluster_max) {
  1231. ret = nr_reclaimed;
  1232. goto out;
  1233. }
  1234. /*
  1235. * Try to write back as many pages as we just scanned. This
  1236. * tends to cause slow streaming writers to write data to the
  1237. * disk smoothly, at the dirtying rate, which is nice. But
  1238. * that's undesirable in laptop mode, where we *want* lumpy
  1239. * writeout. So in laptop mode, write out the whole world.
  1240. */
  1241. if (total_scanned > sc->swap_cluster_max +
  1242. sc->swap_cluster_max / 2) {
  1243. wakeup_pdflush(laptop_mode ? 0 : total_scanned);
  1244. sc->may_writepage = 1;
  1245. }
  1246. /* Take a nap, wait for some writeback to complete */
  1247. if (sc->nr_scanned && priority < DEF_PRIORITY - 2)
  1248. congestion_wait(WRITE, HZ/10);
  1249. }
  1250. /* top priority shrink_zones still had more to do? don't OOM, then */
  1251. if (!sc->all_unreclaimable && scan_global_lru(sc))
  1252. ret = nr_reclaimed;
  1253. out:
  1254. /*
  1255. * Now that we've scanned all the zones at this priority level, note
  1256. * that level within the zone so that the next thread which performs
  1257. * scanning of this zone will immediately start out at this priority
  1258. * level. This affects only the decision whether or not to bring
  1259. * mapped pages onto the inactive list.
  1260. */
  1261. if (priority < 0)
  1262. priority = 0;
  1263. if (scan_global_lru(sc)) {
  1264. for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
  1265. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1266. continue;
  1267. zone->prev_priority = priority;
  1268. }
  1269. } else
  1270. mem_cgroup_record_reclaim_priority(sc->mem_cgroup, priority);
  1271. delayacct_freepages_end();
  1272. return ret;
  1273. }
  1274. unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
  1275. gfp_t gfp_mask)
  1276. {
  1277. struct scan_control sc = {
  1278. .gfp_mask = gfp_mask,
  1279. .may_writepage = !laptop_mode,
  1280. .swap_cluster_max = SWAP_CLUSTER_MAX,
  1281. .may_swap = 1,
  1282. .swappiness = vm_swappiness,
  1283. .order = order,
  1284. .mem_cgroup = NULL,
  1285. .isolate_pages = isolate_pages_global,
  1286. };
  1287. return do_try_to_free_pages(zonelist, &sc);
  1288. }
  1289. #ifdef CONFIG_CGROUP_MEM_RES_CTLR
  1290. unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *mem_cont,
  1291. gfp_t gfp_mask)
  1292. {
  1293. struct scan_control sc = {
  1294. .may_writepage = !laptop_mode,
  1295. .may_swap = 1,
  1296. .swap_cluster_max = SWAP_CLUSTER_MAX,
  1297. .swappiness = vm_swappiness,
  1298. .order = 0,
  1299. .mem_cgroup = mem_cont,
  1300. .isolate_pages = mem_cgroup_isolate_pages,
  1301. };
  1302. struct zonelist *zonelist;
  1303. sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
  1304. (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
  1305. zonelist = NODE_DATA(numa_node_id())->node_zonelists;
  1306. return do_try_to_free_pages(zonelist, &sc);
  1307. }
  1308. #endif
  1309. /*
  1310. * For kswapd, balance_pgdat() will work across all this node's zones until
  1311. * they are all at pages_high.
  1312. *
  1313. * Returns the number of pages which were actually freed.
  1314. *
  1315. * There is special handling here for zones which are full of pinned pages.
  1316. * This can happen if the pages are all mlocked, or if they are all used by
  1317. * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
  1318. * What we do is to detect the case where all pages in the zone have been
  1319. * scanned twice and there has been zero successful reclaim. Mark the zone as
  1320. * dead and from now on, only perform a short scan. Basically we're polling
  1321. * the zone for when the problem goes away.
  1322. *
  1323. * kswapd scans the zones in the highmem->normal->dma direction. It skips
  1324. * zones which have free_pages > pages_high, but once a zone is found to have
  1325. * free_pages <= pages_high, we scan that zone and the lower zones regardless
  1326. * of the number of free pages in the lower zones. This interoperates with
  1327. * the page allocator fallback scheme to ensure that aging of pages is balanced
  1328. * across the zones.
  1329. */
  1330. static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
  1331. {
  1332. int all_zones_ok;
  1333. int priority;
  1334. int i;
  1335. unsigned long total_scanned;
  1336. unsigned long nr_reclaimed;
  1337. struct reclaim_state *reclaim_state = current->reclaim_state;
  1338. struct scan_control sc = {
  1339. .gfp_mask = GFP_KERNEL,
  1340. .may_swap = 1,
  1341. .swap_cluster_max = SWAP_CLUSTER_MAX,
  1342. .swappiness = vm_swappiness,
  1343. .order = order,
  1344. .mem_cgroup = NULL,
  1345. .isolate_pages = isolate_pages_global,
  1346. };
  1347. /*
  1348. * temp_priority is used to remember the scanning priority at which
  1349. * this zone was successfully refilled to free_pages == pages_high.
  1350. */
  1351. int temp_priority[MAX_NR_ZONES];
  1352. loop_again:
  1353. total_scanned = 0;
  1354. nr_reclaimed = 0;
  1355. sc.may_writepage = !laptop_mode;
  1356. count_vm_event(PAGEOUTRUN);
  1357. for (i = 0; i < pgdat->nr_zones; i++)
  1358. temp_priority[i] = DEF_PRIORITY;
  1359. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  1360. int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
  1361. unsigned long lru_pages = 0;
  1362. /* The swap token gets in the way of swapout... */
  1363. if (!priority)
  1364. disable_swap_token();
  1365. all_zones_ok = 1;
  1366. /*
  1367. * Scan in the highmem->dma direction for the highest
  1368. * zone which needs scanning
  1369. */
  1370. for (i = pgdat->nr_zones - 1; i >= 0; i--) {
  1371. struct zone *zone = pgdat->node_zones + i;
  1372. if (!populated_zone(zone))
  1373. continue;
  1374. if (zone_is_all_unreclaimable(zone) &&
  1375. priority != DEF_PRIORITY)
  1376. continue;
  1377. if (!zone_watermark_ok(zone, order, zone->pages_high,
  1378. 0, 0)) {
  1379. end_zone = i;
  1380. break;
  1381. }
  1382. }
  1383. if (i < 0)
  1384. goto out;
  1385. for (i = 0; i <= end_zone; i++) {
  1386. struct zone *zone = pgdat->node_zones + i;
  1387. lru_pages += zone_page_state(zone, NR_ACTIVE)
  1388. + zone_page_state(zone, NR_INACTIVE);
  1389. }
  1390. /*
  1391. * Now scan the zone in the dma->highmem direction, stopping
  1392. * at the last zone which needs scanning.
  1393. *
  1394. * We do this because the page allocator works in the opposite
  1395. * direction. This prevents the page allocator from allocating
  1396. * pages behind kswapd's direction of progress, which would
  1397. * cause too much scanning of the lower zones.
  1398. */
  1399. for (i = 0; i <= end_zone; i++) {
  1400. struct zone *zone = pgdat->node_zones + i;
  1401. int nr_slab;
  1402. if (!populated_zone(zone))
  1403. continue;
  1404. if (zone_is_all_unreclaimable(zone) &&
  1405. priority != DEF_PRIORITY)
  1406. continue;
  1407. if (!zone_watermark_ok(zone, order, zone->pages_high,
  1408. end_zone, 0))
  1409. all_zones_ok = 0;
  1410. temp_priority[i] = priority;
  1411. sc.nr_scanned = 0;
  1412. note_zone_scanning_priority(zone, priority);
  1413. /*
  1414. * We put equal pressure on every zone, unless one
  1415. * zone has way too many pages free already.
  1416. */
  1417. if (!zone_watermark_ok(zone, order, 8*zone->pages_high,
  1418. end_zone, 0))
  1419. nr_reclaimed += shrink_zone(priority, zone, &sc);
  1420. reclaim_state->reclaimed_slab = 0;
  1421. nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
  1422. lru_pages);
  1423. nr_reclaimed += reclaim_state->reclaimed_slab;
  1424. total_scanned += sc.nr_scanned;
  1425. if (zone_is_all_unreclaimable(zone))
  1426. continue;
  1427. if (nr_slab == 0 && zone->pages_scanned >=
  1428. (zone_page_state(zone, NR_ACTIVE)
  1429. + zone_page_state(zone, NR_INACTIVE)) * 6)
  1430. zone_set_flag(zone,
  1431. ZONE_ALL_UNRECLAIMABLE);
  1432. /*
  1433. * If we've done a decent amount of scanning and
  1434. * the reclaim ratio is low, start doing writepage
  1435. * even in laptop mode
  1436. */
  1437. if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
  1438. total_scanned > nr_reclaimed + nr_reclaimed / 2)
  1439. sc.may_writepage = 1;
  1440. }
  1441. if (all_zones_ok)
  1442. break; /* kswapd: all done */
  1443. /*
  1444. * OK, kswapd is getting into trouble. Take a nap, then take
  1445. * another pass across the zones.
  1446. */
  1447. if (total_scanned && priority < DEF_PRIORITY - 2)
  1448. congestion_wait(WRITE, HZ/10);
  1449. /*
  1450. * We do this so kswapd doesn't build up large priorities for
  1451. * example when it is freeing in parallel with allocators. It
  1452. * matches the direct reclaim path behaviour in terms of impact
  1453. * on zone->*_priority.
  1454. */
  1455. if (nr_reclaimed >= SWAP_CLUSTER_MAX)
  1456. break;
  1457. }
  1458. out:
  1459. /*
  1460. * Note within each zone the priority level at which this zone was
  1461. * brought into a happy state. So that the next thread which scans this
  1462. * zone will start out at that priority level.
  1463. */
  1464. for (i = 0; i < pgdat->nr_zones; i++) {
  1465. struct zone *zone = pgdat->node_zones + i;
  1466. zone->prev_priority = temp_priority[i];
  1467. }
  1468. if (!all_zones_ok) {
  1469. cond_resched();
  1470. try_to_freeze();
  1471. goto loop_again;
  1472. }
  1473. return nr_reclaimed;
  1474. }
  1475. /*
  1476. * The background pageout daemon, started as a kernel thread
  1477. * from the init process.
  1478. *
  1479. * This basically trickles out pages so that we have _some_
  1480. * free memory available even if there is no other activity
  1481. * that frees anything up. This is needed for things like routing
  1482. * etc, where we otherwise might have all activity going on in
  1483. * asynchronous contexts that cannot page things out.
  1484. *
  1485. * If there are applications that are active memory-allocators
  1486. * (most normal use), this basically shouldn't matter.
  1487. */
  1488. static int kswapd(void *p)
  1489. {
  1490. unsigned long order;
  1491. pg_data_t *pgdat = (pg_data_t*)p;
  1492. struct task_struct *tsk = current;
  1493. DEFINE_WAIT(wait);
  1494. struct reclaim_state reclaim_state = {
  1495. .reclaimed_slab = 0,
  1496. };
  1497. node_to_cpumask_ptr(cpumask, pgdat->node_id);
  1498. if (!cpus_empty(*cpumask))
  1499. set_cpus_allowed_ptr(tsk, cpumask);
  1500. current->reclaim_state = &reclaim_state;
  1501. /*
  1502. * Tell the memory management that we're a "memory allocator",
  1503. * and that if we need more memory we should get access to it
  1504. * regardless (see "__alloc_pages()"). "kswapd" should
  1505. * never get caught in the normal page freeing logic.
  1506. *
  1507. * (Kswapd normally doesn't need memory anyway, but sometimes
  1508. * you need a small amount of memory in order to be able to
  1509. * page out something else, and this flag essentially protects
  1510. * us from recursively trying to free more memory as we're
  1511. * trying to free the first piece of memory in the first place).
  1512. */
  1513. tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
  1514. set_freezable();
  1515. order = 0;
  1516. for ( ; ; ) {
  1517. unsigned long new_order;
  1518. prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
  1519. new_order = pgdat->kswapd_max_order;
  1520. pgdat->kswapd_max_order = 0;
  1521. if (order < new_order) {
  1522. /*
  1523. * Don't sleep if someone wants a larger 'order'
  1524. * allocation
  1525. */
  1526. order = new_order;
  1527. } else {
  1528. if (!freezing(current))
  1529. schedule();
  1530. order = pgdat->kswapd_max_order;
  1531. }
  1532. finish_wait(&pgdat->kswapd_wait, &wait);
  1533. if (!try_to_freeze()) {
  1534. /* We can speed up thawing tasks if we don't call
  1535. * balance_pgdat after returning from the refrigerator
  1536. */
  1537. balance_pgdat(pgdat, order);
  1538. }
  1539. }
  1540. return 0;
  1541. }
  1542. /*
  1543. * A zone is low on free memory, so wake its kswapd task to service it.
  1544. */
  1545. void wakeup_kswapd(struct zone *zone, int order)
  1546. {
  1547. pg_data_t *pgdat;
  1548. if (!populated_zone(zone))
  1549. return;
  1550. pgdat = zone->zone_pgdat;
  1551. if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
  1552. return;
  1553. if (pgdat->kswapd_max_order < order)
  1554. pgdat->kswapd_max_order = order;
  1555. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1556. return;
  1557. if (!waitqueue_active(&pgdat->kswapd_wait))
  1558. return;
  1559. wake_up_interruptible(&pgdat->kswapd_wait);
  1560. }
  1561. #ifdef CONFIG_PM
  1562. /*
  1563. * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages
  1564. * from LRU lists system-wide, for given pass and priority, and returns the
  1565. * number of reclaimed pages
  1566. *
  1567. * For pass > 3 we also try to shrink the LRU lists that contain a few pages
  1568. */
  1569. static unsigned long shrink_all_zones(unsigned long nr_pages, int prio,
  1570. int pass, struct scan_control *sc)
  1571. {
  1572. struct zone *zone;
  1573. unsigned long nr_to_scan, ret = 0;
  1574. for_each_zone(zone) {
  1575. if (!populated_zone(zone))
  1576. continue;
  1577. if (zone_is_all_unreclaimable(zone) && prio != DEF_PRIORITY)
  1578. continue;
  1579. /* For pass = 0 we don't shrink the active list */
  1580. if (pass > 0) {
  1581. zone->nr_scan_active +=
  1582. (zone_page_state(zone, NR_ACTIVE) >> prio) + 1;
  1583. if (zone->nr_scan_active >= nr_pages || pass > 3) {
  1584. zone->nr_scan_active = 0;
  1585. nr_to_scan = min(nr_pages,
  1586. zone_page_state(zone, NR_ACTIVE));
  1587. shrink_active_list(nr_to_scan, zone, sc, prio);
  1588. }
  1589. }
  1590. zone->nr_scan_inactive +=
  1591. (zone_page_state(zone, NR_INACTIVE) >> prio) + 1;
  1592. if (zone->nr_scan_inactive >= nr_pages || pass > 3) {
  1593. zone->nr_scan_inactive = 0;
  1594. nr_to_scan = min(nr_pages,
  1595. zone_page_state(zone, NR_INACTIVE));
  1596. ret += shrink_inactive_list(nr_to_scan, zone, sc);
  1597. if (ret >= nr_pages)
  1598. return ret;
  1599. }
  1600. }
  1601. return ret;
  1602. }
  1603. static unsigned long count_lru_pages(void)
  1604. {
  1605. return global_page_state(NR_ACTIVE) + global_page_state(NR_INACTIVE);
  1606. }
  1607. /*
  1608. * Try to free `nr_pages' of memory, system-wide, and return the number of
  1609. * freed pages.
  1610. *
  1611. * Rather than trying to age LRUs the aim is to preserve the overall
  1612. * LRU order by reclaiming preferentially
  1613. * inactive > active > active referenced > active mapped
  1614. */
  1615. unsigned long shrink_all_memory(unsigned long nr_pages)
  1616. {
  1617. unsigned long lru_pages, nr_slab;
  1618. unsigned long ret = 0;
  1619. int pass;
  1620. struct reclaim_state reclaim_state;
  1621. struct scan_control sc = {
  1622. .gfp_mask = GFP_KERNEL,
  1623. .may_swap = 0,
  1624. .swap_cluster_max = nr_pages,
  1625. .may_writepage = 1,
  1626. .swappiness = vm_swappiness,
  1627. .isolate_pages = isolate_pages_global,
  1628. };
  1629. current->reclaim_state = &reclaim_state;
  1630. lru_pages = count_lru_pages();
  1631. nr_slab = global_page_state(NR_SLAB_RECLAIMABLE);
  1632. /* If slab caches are huge, it's better to hit them first */
  1633. while (nr_slab >= lru_pages) {
  1634. reclaim_state.reclaimed_slab = 0;
  1635. shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
  1636. if (!reclaim_state.reclaimed_slab)
  1637. break;
  1638. ret += reclaim_state.reclaimed_slab;
  1639. if (ret >= nr_pages)
  1640. goto out;
  1641. nr_slab -= reclaim_state.reclaimed_slab;
  1642. }
  1643. /*
  1644. * We try to shrink LRUs in 5 passes:
  1645. * 0 = Reclaim from inactive_list only
  1646. * 1 = Reclaim from active list but don't reclaim mapped
  1647. * 2 = 2nd pass of type 1
  1648. * 3 = Reclaim mapped (normal reclaim)
  1649. * 4 = 2nd pass of type 3
  1650. */
  1651. for (pass = 0; pass < 5; pass++) {
  1652. int prio;
  1653. /* Force reclaiming mapped pages in the passes #3 and #4 */
  1654. if (pass > 2) {
  1655. sc.may_swap = 1;
  1656. sc.swappiness = 100;
  1657. }
  1658. for (prio = DEF_PRIORITY; prio >= 0; prio--) {
  1659. unsigned long nr_to_scan = nr_pages - ret;
  1660. sc.nr_scanned = 0;
  1661. ret += shrink_all_zones(nr_to_scan, prio, pass, &sc);
  1662. if (ret >= nr_pages)
  1663. goto out;
  1664. reclaim_state.reclaimed_slab = 0;
  1665. shrink_slab(sc.nr_scanned, sc.gfp_mask,
  1666. count_lru_pages());
  1667. ret += reclaim_state.reclaimed_slab;
  1668. if (ret >= nr_pages)
  1669. goto out;
  1670. if (sc.nr_scanned && prio < DEF_PRIORITY - 2)
  1671. congestion_wait(WRITE, HZ / 10);
  1672. }
  1673. }
  1674. /*
  1675. * If ret = 0, we could not shrink LRUs, but there may be something
  1676. * in slab caches
  1677. */
  1678. if (!ret) {
  1679. do {
  1680. reclaim_state.reclaimed_slab = 0;
  1681. shrink_slab(nr_pages, sc.gfp_mask, count_lru_pages());
  1682. ret += reclaim_state.reclaimed_slab;
  1683. } while (ret < nr_pages && reclaim_state.reclaimed_slab > 0);
  1684. }
  1685. out:
  1686. current->reclaim_state = NULL;
  1687. return ret;
  1688. }
  1689. #endif
  1690. /* It's optimal to keep kswapds on the same CPUs as their memory, but
  1691. not required for correctness. So if the last cpu in a node goes
  1692. away, we get changed to run anywhere: as the first one comes back,
  1693. restore their cpu bindings. */
  1694. static int __devinit cpu_callback(struct notifier_block *nfb,
  1695. unsigned long action, void *hcpu)
  1696. {
  1697. int nid;
  1698. if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
  1699. for_each_node_state(nid, N_HIGH_MEMORY) {
  1700. pg_data_t *pgdat = NODE_DATA(nid);
  1701. node_to_cpumask_ptr(mask, pgdat->node_id);
  1702. if (any_online_cpu(*mask) < nr_cpu_ids)
  1703. /* One of our CPUs online: restore mask */
  1704. set_cpus_allowed_ptr(pgdat->kswapd, mask);
  1705. }
  1706. }
  1707. return NOTIFY_OK;
  1708. }
  1709. /*
  1710. * This kswapd start function will be called by init and node-hot-add.
  1711. * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
  1712. */
  1713. int kswapd_run(int nid)
  1714. {
  1715. pg_data_t *pgdat = NODE_DATA(nid);
  1716. int ret = 0;
  1717. if (pgdat->kswapd)
  1718. return 0;
  1719. pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
  1720. if (IS_ERR(pgdat->kswapd)) {
  1721. /* failure at boot is fatal */
  1722. BUG_ON(system_state == SYSTEM_BOOTING);
  1723. printk("Failed to start kswapd on node %d\n",nid);
  1724. ret = -1;
  1725. }
  1726. return ret;
  1727. }
  1728. static int __init kswapd_init(void)
  1729. {
  1730. int nid;
  1731. swap_setup();
  1732. for_each_node_state(nid, N_HIGH_MEMORY)
  1733. kswapd_run(nid);
  1734. hotcpu_notifier(cpu_callback, 0);
  1735. return 0;
  1736. }
  1737. module_init(kswapd_init)
  1738. #ifdef CONFIG_NUMA
  1739. /*
  1740. * Zone reclaim mode
  1741. *
  1742. * If non-zero call zone_reclaim when the number of free pages falls below
  1743. * the watermarks.
  1744. */
  1745. int zone_reclaim_mode __read_mostly;
  1746. #define RECLAIM_OFF 0
  1747. #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */
  1748. #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
  1749. #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
  1750. /*
  1751. * Priority for ZONE_RECLAIM. This determines the fraction of pages
  1752. * of a node considered for each zone_reclaim. 4 scans 1/16th of
  1753. * a zone.
  1754. */
  1755. #define ZONE_RECLAIM_PRIORITY 4
  1756. /*
  1757. * Percentage of pages in a zone that must be unmapped for zone_reclaim to
  1758. * occur.
  1759. */
  1760. int sysctl_min_unmapped_ratio = 1;
  1761. /*
  1762. * If the number of slab pages in a zone grows beyond this percentage then
  1763. * slab reclaim needs to occur.
  1764. */
  1765. int sysctl_min_slab_ratio = 5;
  1766. /*
  1767. * Try to free up some pages from this zone through reclaim.
  1768. */
  1769. static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  1770. {
  1771. /* Minimum pages needed in order to stay on node */
  1772. const unsigned long nr_pages = 1 << order;
  1773. struct task_struct *p = current;
  1774. struct reclaim_state reclaim_state;
  1775. int priority;
  1776. unsigned long nr_reclaimed = 0;
  1777. struct scan_control sc = {
  1778. .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
  1779. .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
  1780. .swap_cluster_max = max_t(unsigned long, nr_pages,
  1781. SWAP_CLUSTER_MAX),
  1782. .gfp_mask = gfp_mask,
  1783. .swappiness = vm_swappiness,
  1784. .isolate_pages = isolate_pages_global,
  1785. };
  1786. unsigned long slab_reclaimable;
  1787. disable_swap_token();
  1788. cond_resched();
  1789. /*
  1790. * We need to be able to allocate from the reserves for RECLAIM_SWAP
  1791. * and we also need to be able to write out pages for RECLAIM_WRITE
  1792. * and RECLAIM_SWAP.
  1793. */
  1794. p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
  1795. reclaim_state.reclaimed_slab = 0;
  1796. p->reclaim_state = &reclaim_state;
  1797. if (zone_page_state(zone, NR_FILE_PAGES) -
  1798. zone_page_state(zone, NR_FILE_MAPPED) >
  1799. zone->min_unmapped_pages) {
  1800. /*
  1801. * Free memory by calling shrink zone with increasing
  1802. * priorities until we have enough memory freed.
  1803. */
  1804. priority = ZONE_RECLAIM_PRIORITY;
  1805. do {
  1806. note_zone_scanning_priority(zone, priority);
  1807. nr_reclaimed += shrink_zone(priority, zone, &sc);
  1808. priority--;
  1809. } while (priority >= 0 && nr_reclaimed < nr_pages);
  1810. }
  1811. slab_reclaimable = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
  1812. if (slab_reclaimable > zone->min_slab_pages) {
  1813. /*
  1814. * shrink_slab() does not currently allow us to determine how
  1815. * many pages were freed in this zone. So we take the current
  1816. * number of slab pages and shake the slab until it is reduced
  1817. * by the same nr_pages that we used for reclaiming unmapped
  1818. * pages.
  1819. *
  1820. * Note that shrink_slab will free memory on all zones and may
  1821. * take a long time.
  1822. */
  1823. while (shrink_slab(sc.nr_scanned, gfp_mask, order) &&
  1824. zone_page_state(zone, NR_SLAB_RECLAIMABLE) >
  1825. slab_reclaimable - nr_pages)
  1826. ;
  1827. /*
  1828. * Update nr_reclaimed by the number of slab pages we
  1829. * reclaimed from this zone.
  1830. */
  1831. nr_reclaimed += slab_reclaimable -
  1832. zone_page_state(zone, NR_SLAB_RECLAIMABLE);
  1833. }
  1834. p->reclaim_state = NULL;
  1835. current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
  1836. return nr_reclaimed >= nr_pages;
  1837. }
  1838. int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  1839. {
  1840. int node_id;
  1841. int ret;
  1842. /*
  1843. * Zone reclaim reclaims unmapped file backed pages and
  1844. * slab pages if we are over the defined limits.
  1845. *
  1846. * A small portion of unmapped file backed pages is needed for
  1847. * file I/O otherwise pages read by file I/O will be immediately
  1848. * thrown out if the zone is overallocated. So we do not reclaim
  1849. * if less than a specified percentage of the zone is used by
  1850. * unmapped file backed pages.
  1851. */
  1852. if (zone_page_state(zone, NR_FILE_PAGES) -
  1853. zone_page_state(zone, NR_FILE_MAPPED) <= zone->min_unmapped_pages
  1854. && zone_page_state(zone, NR_SLAB_RECLAIMABLE)
  1855. <= zone->min_slab_pages)
  1856. return 0;
  1857. if (zone_is_all_unreclaimable(zone))
  1858. return 0;
  1859. /*
  1860. * Do not scan if the allocation should not be delayed.
  1861. */
  1862. if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC))
  1863. return 0;
  1864. /*
  1865. * Only run zone reclaim on the local zone or on zones that do not
  1866. * have associated processors. This will favor the local processor
  1867. * over remote processors and spread off node memory allocations
  1868. * as wide as possible.
  1869. */
  1870. node_id = zone_to_nid(zone);
  1871. if (node_state(node_id, N_CPU) && node_id != numa_node_id())
  1872. return 0;
  1873. if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED))
  1874. return 0;
  1875. ret = __zone_reclaim(zone, gfp_mask, order);
  1876. zone_clear_flag(zone, ZONE_RECLAIM_LOCKED);
  1877. return ret;
  1878. }
  1879. #endif