vmscan.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938
  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/file.h>
  22. #include <linux/writeback.h>
  23. #include <linux/blkdev.h>
  24. #include <linux/buffer_head.h> /* for try_to_release_page(),
  25. buffer_heads_over_limit */
  26. #include <linux/mm_inline.h>
  27. #include <linux/pagevec.h>
  28. #include <linux/backing-dev.h>
  29. #include <linux/rmap.h>
  30. #include <linux/topology.h>
  31. #include <linux/cpu.h>
  32. #include <linux/cpuset.h>
  33. #include <linux/notifier.h>
  34. #include <linux/rwsem.h>
  35. #include <asm/tlbflush.h>
  36. #include <asm/div64.h>
  37. #include <linux/swapops.h>
  38. /* possible outcome of pageout() */
  39. typedef enum {
  40. /* failed to write page out, page is locked */
  41. PAGE_KEEP,
  42. /* move page to the active list, page is locked */
  43. PAGE_ACTIVATE,
  44. /* page has been sent to the disk successfully, page is unlocked */
  45. PAGE_SUCCESS,
  46. /* page is clean and locked */
  47. PAGE_CLEAN,
  48. } pageout_t;
  49. struct scan_control {
  50. /* Ask refill_inactive_zone, or shrink_cache to scan this many pages */
  51. unsigned long nr_to_scan;
  52. /* Incremented by the number of inactive pages that were scanned */
  53. unsigned long nr_scanned;
  54. /* Incremented by the number of pages reclaimed */
  55. unsigned long nr_reclaimed;
  56. unsigned long nr_mapped; /* From page_state */
  57. /* Ask shrink_caches, or shrink_zone to scan at this priority */
  58. unsigned int priority;
  59. /* This context's GFP mask */
  60. gfp_t gfp_mask;
  61. int may_writepage;
  62. /* Can pages be swapped as part of reclaim? */
  63. int may_swap;
  64. /* This context's SWAP_CLUSTER_MAX. If freeing memory for
  65. * suspend, we effectively ignore SWAP_CLUSTER_MAX.
  66. * In this context, it doesn't matter that we scan the
  67. * whole list at once. */
  68. int swap_cluster_max;
  69. };
  70. /*
  71. * The list of shrinker callbacks used by to apply pressure to
  72. * ageable caches.
  73. */
  74. struct shrinker {
  75. shrinker_t shrinker;
  76. struct list_head list;
  77. int seeks; /* seeks to recreate an obj */
  78. long nr; /* objs pending delete */
  79. };
  80. #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
  81. #ifdef ARCH_HAS_PREFETCH
  82. #define prefetch_prev_lru_page(_page, _base, _field) \
  83. do { \
  84. if ((_page)->lru.prev != _base) { \
  85. struct page *prev; \
  86. \
  87. prev = lru_to_page(&(_page->lru)); \
  88. prefetch(&prev->_field); \
  89. } \
  90. } while (0)
  91. #else
  92. #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
  93. #endif
  94. #ifdef ARCH_HAS_PREFETCHW
  95. #define prefetchw_prev_lru_page(_page, _base, _field) \
  96. do { \
  97. if ((_page)->lru.prev != _base) { \
  98. struct page *prev; \
  99. \
  100. prev = lru_to_page(&(_page->lru)); \
  101. prefetchw(&prev->_field); \
  102. } \
  103. } while (0)
  104. #else
  105. #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
  106. #endif
  107. /*
  108. * From 0 .. 100. Higher means more swappy.
  109. */
  110. int vm_swappiness = 60;
  111. static long total_memory;
  112. static LIST_HEAD(shrinker_list);
  113. static DECLARE_RWSEM(shrinker_rwsem);
  114. /*
  115. * Add a shrinker callback to be called from the vm
  116. */
  117. struct shrinker *set_shrinker(int seeks, shrinker_t theshrinker)
  118. {
  119. struct shrinker *shrinker;
  120. shrinker = kmalloc(sizeof(*shrinker), GFP_KERNEL);
  121. if (shrinker) {
  122. shrinker->shrinker = theshrinker;
  123. shrinker->seeks = seeks;
  124. shrinker->nr = 0;
  125. down_write(&shrinker_rwsem);
  126. list_add_tail(&shrinker->list, &shrinker_list);
  127. up_write(&shrinker_rwsem);
  128. }
  129. return shrinker;
  130. }
  131. EXPORT_SYMBOL(set_shrinker);
  132. /*
  133. * Remove one
  134. */
  135. void remove_shrinker(struct shrinker *shrinker)
  136. {
  137. down_write(&shrinker_rwsem);
  138. list_del(&shrinker->list);
  139. up_write(&shrinker_rwsem);
  140. kfree(shrinker);
  141. }
  142. EXPORT_SYMBOL(remove_shrinker);
  143. #define SHRINK_BATCH 128
  144. /*
  145. * Call the shrink functions to age shrinkable caches
  146. *
  147. * Here we assume it costs one seek to replace a lru page and that it also
  148. * takes a seek to recreate a cache object. With this in mind we age equal
  149. * percentages of the lru and ageable caches. This should balance the seeks
  150. * generated by these structures.
  151. *
  152. * If the vm encounted mapped pages on the LRU it increase the pressure on
  153. * slab to avoid swapping.
  154. *
  155. * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
  156. *
  157. * `lru_pages' represents the number of on-LRU pages in all the zones which
  158. * are eligible for the caller's allocation attempt. It is used for balancing
  159. * slab reclaim versus page reclaim.
  160. *
  161. * Returns the number of slab objects which we shrunk.
  162. */
  163. int shrink_slab(unsigned long scanned, gfp_t gfp_mask, unsigned long lru_pages)
  164. {
  165. struct shrinker *shrinker;
  166. int ret = 0;
  167. if (scanned == 0)
  168. scanned = SWAP_CLUSTER_MAX;
  169. if (!down_read_trylock(&shrinker_rwsem))
  170. return 1; /* Assume we'll be able to shrink next time */
  171. list_for_each_entry(shrinker, &shrinker_list, list) {
  172. unsigned long long delta;
  173. unsigned long total_scan;
  174. unsigned long max_pass = (*shrinker->shrinker)(0, gfp_mask);
  175. delta = (4 * scanned) / shrinker->seeks;
  176. delta *= max_pass;
  177. do_div(delta, lru_pages + 1);
  178. shrinker->nr += delta;
  179. if (shrinker->nr < 0) {
  180. printk(KERN_ERR "%s: nr=%ld\n",
  181. __FUNCTION__, shrinker->nr);
  182. shrinker->nr = max_pass;
  183. }
  184. /*
  185. * Avoid risking looping forever due to too large nr value:
  186. * never try to free more than twice the estimate number of
  187. * freeable entries.
  188. */
  189. if (shrinker->nr > max_pass * 2)
  190. shrinker->nr = max_pass * 2;
  191. total_scan = shrinker->nr;
  192. shrinker->nr = 0;
  193. while (total_scan >= SHRINK_BATCH) {
  194. long this_scan = SHRINK_BATCH;
  195. int shrink_ret;
  196. int nr_before;
  197. nr_before = (*shrinker->shrinker)(0, gfp_mask);
  198. shrink_ret = (*shrinker->shrinker)(this_scan, gfp_mask);
  199. if (shrink_ret == -1)
  200. break;
  201. if (shrink_ret < nr_before)
  202. ret += nr_before - shrink_ret;
  203. mod_page_state(slabs_scanned, this_scan);
  204. total_scan -= this_scan;
  205. cond_resched();
  206. }
  207. shrinker->nr += total_scan;
  208. }
  209. up_read(&shrinker_rwsem);
  210. return ret;
  211. }
  212. /* Called without lock on whether page is mapped, so answer is unstable */
  213. static inline int page_mapping_inuse(struct page *page)
  214. {
  215. struct address_space *mapping;
  216. /* Page is in somebody's page tables. */
  217. if (page_mapped(page))
  218. return 1;
  219. /* Be more reluctant to reclaim swapcache than pagecache */
  220. if (PageSwapCache(page))
  221. return 1;
  222. mapping = page_mapping(page);
  223. if (!mapping)
  224. return 0;
  225. /* File is mmap'd by somebody? */
  226. return mapping_mapped(mapping);
  227. }
  228. static inline int is_page_cache_freeable(struct page *page)
  229. {
  230. return page_count(page) - !!PagePrivate(page) == 2;
  231. }
  232. static int may_write_to_queue(struct backing_dev_info *bdi)
  233. {
  234. if (current->flags & PF_SWAPWRITE)
  235. return 1;
  236. if (!bdi_write_congested(bdi))
  237. return 1;
  238. if (bdi == current->backing_dev_info)
  239. return 1;
  240. return 0;
  241. }
  242. /*
  243. * We detected a synchronous write error writing a page out. Probably
  244. * -ENOSPC. We need to propagate that into the address_space for a subsequent
  245. * fsync(), msync() or close().
  246. *
  247. * The tricky part is that after writepage we cannot touch the mapping: nothing
  248. * prevents it from being freed up. But we have a ref on the page and once
  249. * that page is locked, the mapping is pinned.
  250. *
  251. * We're allowed to run sleeping lock_page() here because we know the caller has
  252. * __GFP_FS.
  253. */
  254. static void handle_write_error(struct address_space *mapping,
  255. struct page *page, int error)
  256. {
  257. lock_page(page);
  258. if (page_mapping(page) == mapping) {
  259. if (error == -ENOSPC)
  260. set_bit(AS_ENOSPC, &mapping->flags);
  261. else
  262. set_bit(AS_EIO, &mapping->flags);
  263. }
  264. unlock_page(page);
  265. }
  266. /*
  267. * pageout is called by shrink_list() for each dirty page. Calls ->writepage().
  268. */
  269. static pageout_t pageout(struct page *page, struct address_space *mapping)
  270. {
  271. /*
  272. * If the page is dirty, only perform writeback if that write
  273. * will be non-blocking. To prevent this allocation from being
  274. * stalled by pagecache activity. But note that there may be
  275. * stalls if we need to run get_block(). We could test
  276. * PagePrivate for that.
  277. *
  278. * If this process is currently in generic_file_write() against
  279. * this page's queue, we can perform writeback even if that
  280. * will block.
  281. *
  282. * If the page is swapcache, write it back even if that would
  283. * block, for some throttling. This happens by accident, because
  284. * swap_backing_dev_info is bust: it doesn't reflect the
  285. * congestion state of the swapdevs. Easy to fix, if needed.
  286. * See swapfile.c:page_queue_congested().
  287. */
  288. if (!is_page_cache_freeable(page))
  289. return PAGE_KEEP;
  290. if (!mapping) {
  291. /*
  292. * Some data journaling orphaned pages can have
  293. * page->mapping == NULL while being dirty with clean buffers.
  294. */
  295. if (PagePrivate(page)) {
  296. if (try_to_free_buffers(page)) {
  297. ClearPageDirty(page);
  298. printk("%s: orphaned page\n", __FUNCTION__);
  299. return PAGE_CLEAN;
  300. }
  301. }
  302. return PAGE_KEEP;
  303. }
  304. if (mapping->a_ops->writepage == NULL)
  305. return PAGE_ACTIVATE;
  306. if (!may_write_to_queue(mapping->backing_dev_info))
  307. return PAGE_KEEP;
  308. if (clear_page_dirty_for_io(page)) {
  309. int res;
  310. struct writeback_control wbc = {
  311. .sync_mode = WB_SYNC_NONE,
  312. .nr_to_write = SWAP_CLUSTER_MAX,
  313. .nonblocking = 1,
  314. .for_reclaim = 1,
  315. };
  316. SetPageReclaim(page);
  317. res = mapping->a_ops->writepage(page, &wbc);
  318. if (res < 0)
  319. handle_write_error(mapping, page, res);
  320. if (res == AOP_WRITEPAGE_ACTIVATE) {
  321. ClearPageReclaim(page);
  322. return PAGE_ACTIVATE;
  323. }
  324. if (!PageWriteback(page)) {
  325. /* synchronous write or broken a_ops? */
  326. ClearPageReclaim(page);
  327. }
  328. return PAGE_SUCCESS;
  329. }
  330. return PAGE_CLEAN;
  331. }
  332. static int remove_mapping(struct address_space *mapping, struct page *page)
  333. {
  334. if (!mapping)
  335. return 0; /* truncate got there first */
  336. write_lock_irq(&mapping->tree_lock);
  337. /*
  338. * The non-racy check for busy page. It is critical to check
  339. * PageDirty _after_ making sure that the page is freeable and
  340. * not in use by anybody. (pagecache + us == 2)
  341. */
  342. if (unlikely(page_count(page) != 2))
  343. goto cannot_free;
  344. smp_rmb();
  345. if (unlikely(PageDirty(page)))
  346. goto cannot_free;
  347. if (PageSwapCache(page)) {
  348. swp_entry_t swap = { .val = page_private(page) };
  349. __delete_from_swap_cache(page);
  350. write_unlock_irq(&mapping->tree_lock);
  351. swap_free(swap);
  352. __put_page(page); /* The pagecache ref */
  353. return 1;
  354. }
  355. __remove_from_page_cache(page);
  356. write_unlock_irq(&mapping->tree_lock);
  357. __put_page(page);
  358. return 1;
  359. cannot_free:
  360. write_unlock_irq(&mapping->tree_lock);
  361. return 0;
  362. }
  363. /*
  364. * shrink_list adds the number of reclaimed pages to sc->nr_reclaimed
  365. */
  366. static int shrink_list(struct list_head *page_list, struct scan_control *sc)
  367. {
  368. LIST_HEAD(ret_pages);
  369. struct pagevec freed_pvec;
  370. int pgactivate = 0;
  371. int reclaimed = 0;
  372. cond_resched();
  373. pagevec_init(&freed_pvec, 1);
  374. while (!list_empty(page_list)) {
  375. struct address_space *mapping;
  376. struct page *page;
  377. int may_enter_fs;
  378. int referenced;
  379. cond_resched();
  380. page = lru_to_page(page_list);
  381. list_del(&page->lru);
  382. if (TestSetPageLocked(page))
  383. goto keep;
  384. BUG_ON(PageActive(page));
  385. sc->nr_scanned++;
  386. /* Double the slab pressure for mapped and swapcache pages */
  387. if (page_mapped(page) || PageSwapCache(page))
  388. sc->nr_scanned++;
  389. if (PageWriteback(page))
  390. goto keep_locked;
  391. referenced = page_referenced(page, 1);
  392. /* In active use or really unfreeable? Activate it. */
  393. if (referenced && page_mapping_inuse(page))
  394. goto activate_locked;
  395. #ifdef CONFIG_SWAP
  396. /*
  397. * Anonymous process memory has backing store?
  398. * Try to allocate it some swap space here.
  399. */
  400. if (PageAnon(page) && !PageSwapCache(page)) {
  401. if (!sc->may_swap)
  402. goto keep_locked;
  403. if (!add_to_swap(page, GFP_ATOMIC))
  404. goto activate_locked;
  405. }
  406. #endif /* CONFIG_SWAP */
  407. mapping = page_mapping(page);
  408. may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
  409. (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
  410. /*
  411. * The page is mapped into the page tables of one or more
  412. * processes. Try to unmap it here.
  413. */
  414. if (page_mapped(page) && mapping) {
  415. /*
  416. * No unmapping if we do not swap
  417. */
  418. if (!sc->may_swap)
  419. goto keep_locked;
  420. switch (try_to_unmap(page, 0)) {
  421. case SWAP_FAIL:
  422. goto activate_locked;
  423. case SWAP_AGAIN:
  424. goto keep_locked;
  425. case SWAP_SUCCESS:
  426. ; /* try to free the page below */
  427. }
  428. }
  429. if (PageDirty(page)) {
  430. if (referenced)
  431. goto keep_locked;
  432. if (!may_enter_fs)
  433. goto keep_locked;
  434. if (!sc->may_writepage)
  435. goto keep_locked;
  436. /* Page is dirty, try to write it out here */
  437. switch(pageout(page, mapping)) {
  438. case PAGE_KEEP:
  439. goto keep_locked;
  440. case PAGE_ACTIVATE:
  441. goto activate_locked;
  442. case PAGE_SUCCESS:
  443. if (PageWriteback(page) || PageDirty(page))
  444. goto keep;
  445. /*
  446. * A synchronous write - probably a ramdisk. Go
  447. * ahead and try to reclaim the page.
  448. */
  449. if (TestSetPageLocked(page))
  450. goto keep;
  451. if (PageDirty(page) || PageWriteback(page))
  452. goto keep_locked;
  453. mapping = page_mapping(page);
  454. case PAGE_CLEAN:
  455. ; /* try to free the page below */
  456. }
  457. }
  458. /*
  459. * If the page has buffers, try to free the buffer mappings
  460. * associated with this page. If we succeed we try to free
  461. * the page as well.
  462. *
  463. * We do this even if the page is PageDirty().
  464. * try_to_release_page() does not perform I/O, but it is
  465. * possible for a page to have PageDirty set, but it is actually
  466. * clean (all its buffers are clean). This happens if the
  467. * buffers were written out directly, with submit_bh(). ext3
  468. * will do this, as well as the blockdev mapping.
  469. * try_to_release_page() will discover that cleanness and will
  470. * drop the buffers and mark the page clean - it can be freed.
  471. *
  472. * Rarely, pages can have buffers and no ->mapping. These are
  473. * the pages which were not successfully invalidated in
  474. * truncate_complete_page(). We try to drop those buffers here
  475. * and if that worked, and the page is no longer mapped into
  476. * process address space (page_count == 1) it can be freed.
  477. * Otherwise, leave the page on the LRU so it is swappable.
  478. */
  479. if (PagePrivate(page)) {
  480. if (!try_to_release_page(page, sc->gfp_mask))
  481. goto activate_locked;
  482. if (!mapping && page_count(page) == 1)
  483. goto free_it;
  484. }
  485. if (!remove_mapping(mapping, page))
  486. goto keep_locked;
  487. free_it:
  488. unlock_page(page);
  489. reclaimed++;
  490. if (!pagevec_add(&freed_pvec, page))
  491. __pagevec_release_nonlru(&freed_pvec);
  492. continue;
  493. activate_locked:
  494. SetPageActive(page);
  495. pgactivate++;
  496. keep_locked:
  497. unlock_page(page);
  498. keep:
  499. list_add(&page->lru, &ret_pages);
  500. BUG_ON(PageLRU(page));
  501. }
  502. list_splice(&ret_pages, page_list);
  503. if (pagevec_count(&freed_pvec))
  504. __pagevec_release_nonlru(&freed_pvec);
  505. mod_page_state(pgactivate, pgactivate);
  506. sc->nr_reclaimed += reclaimed;
  507. return reclaimed;
  508. }
  509. #ifdef CONFIG_MIGRATION
  510. static inline void move_to_lru(struct page *page)
  511. {
  512. list_del(&page->lru);
  513. if (PageActive(page)) {
  514. /*
  515. * lru_cache_add_active checks that
  516. * the PG_active bit is off.
  517. */
  518. ClearPageActive(page);
  519. lru_cache_add_active(page);
  520. } else {
  521. lru_cache_add(page);
  522. }
  523. put_page(page);
  524. }
  525. /*
  526. * Add isolated pages on the list back to the LRU.
  527. *
  528. * returns the number of pages put back.
  529. */
  530. int putback_lru_pages(struct list_head *l)
  531. {
  532. struct page *page;
  533. struct page *page2;
  534. int count = 0;
  535. list_for_each_entry_safe(page, page2, l, lru) {
  536. move_to_lru(page);
  537. count++;
  538. }
  539. return count;
  540. }
  541. /*
  542. * Non migratable page
  543. */
  544. int fail_migrate_page(struct page *newpage, struct page *page)
  545. {
  546. return -EIO;
  547. }
  548. EXPORT_SYMBOL(fail_migrate_page);
  549. /*
  550. * swapout a single page
  551. * page is locked upon entry, unlocked on exit
  552. */
  553. static int swap_page(struct page *page)
  554. {
  555. struct address_space *mapping = page_mapping(page);
  556. if (page_mapped(page) && mapping)
  557. if (try_to_unmap(page, 1) != SWAP_SUCCESS)
  558. goto unlock_retry;
  559. if (PageDirty(page)) {
  560. /* Page is dirty, try to write it out here */
  561. switch(pageout(page, mapping)) {
  562. case PAGE_KEEP:
  563. case PAGE_ACTIVATE:
  564. goto unlock_retry;
  565. case PAGE_SUCCESS:
  566. goto retry;
  567. case PAGE_CLEAN:
  568. ; /* try to free the page below */
  569. }
  570. }
  571. if (PagePrivate(page)) {
  572. if (!try_to_release_page(page, GFP_KERNEL) ||
  573. (!mapping && page_count(page) == 1))
  574. goto unlock_retry;
  575. }
  576. if (remove_mapping(mapping, page)) {
  577. /* Success */
  578. unlock_page(page);
  579. return 0;
  580. }
  581. unlock_retry:
  582. unlock_page(page);
  583. retry:
  584. return -EAGAIN;
  585. }
  586. EXPORT_SYMBOL(swap_page);
  587. /*
  588. * Page migration was first developed in the context of the memory hotplug
  589. * project. The main authors of the migration code are:
  590. *
  591. * IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
  592. * Hirokazu Takahashi <taka@valinux.co.jp>
  593. * Dave Hansen <haveblue@us.ibm.com>
  594. * Christoph Lameter <clameter@sgi.com>
  595. */
  596. /*
  597. * Remove references for a page and establish the new page with the correct
  598. * basic settings to be able to stop accesses to the page.
  599. */
  600. int migrate_page_remove_references(struct page *newpage,
  601. struct page *page, int nr_refs)
  602. {
  603. struct address_space *mapping = page_mapping(page);
  604. struct page **radix_pointer;
  605. /*
  606. * Avoid doing any of the following work if the page count
  607. * indicates that the page is in use or truncate has removed
  608. * the page.
  609. */
  610. if (!mapping || page_mapcount(page) + nr_refs != page_count(page))
  611. return 1;
  612. /*
  613. * Establish swap ptes for anonymous pages or destroy pte
  614. * maps for files.
  615. *
  616. * In order to reestablish file backed mappings the fault handlers
  617. * will take the radix tree_lock which may then be used to stop
  618. * processses from accessing this page until the new page is ready.
  619. *
  620. * A process accessing via a swap pte (an anonymous page) will take a
  621. * page_lock on the old page which will block the process until the
  622. * migration attempt is complete. At that time the PageSwapCache bit
  623. * will be examined. If the page was migrated then the PageSwapCache
  624. * bit will be clear and the operation to retrieve the page will be
  625. * retried which will find the new page in the radix tree. Then a new
  626. * direct mapping may be generated based on the radix tree contents.
  627. *
  628. * If the page was not migrated then the PageSwapCache bit
  629. * is still set and the operation may continue.
  630. */
  631. try_to_unmap(page, 1);
  632. /*
  633. * Give up if we were unable to remove all mappings.
  634. */
  635. if (page_mapcount(page))
  636. return 1;
  637. write_lock_irq(&mapping->tree_lock);
  638. radix_pointer = (struct page **)radix_tree_lookup_slot(
  639. &mapping->page_tree,
  640. page_index(page));
  641. if (!page_mapping(page) || page_count(page) != nr_refs ||
  642. *radix_pointer != page) {
  643. write_unlock_irq(&mapping->tree_lock);
  644. return 1;
  645. }
  646. /*
  647. * Now we know that no one else is looking at the page.
  648. *
  649. * Certain minimal information about a page must be available
  650. * in order for other subsystems to properly handle the page if they
  651. * find it through the radix tree update before we are finished
  652. * copying the page.
  653. */
  654. get_page(newpage);
  655. newpage->index = page->index;
  656. newpage->mapping = page->mapping;
  657. if (PageSwapCache(page)) {
  658. SetPageSwapCache(newpage);
  659. set_page_private(newpage, page_private(page));
  660. }
  661. *radix_pointer = newpage;
  662. __put_page(page);
  663. write_unlock_irq(&mapping->tree_lock);
  664. return 0;
  665. }
  666. EXPORT_SYMBOL(migrate_page_remove_references);
  667. /*
  668. * Copy the page to its new location
  669. */
  670. void migrate_page_copy(struct page *newpage, struct page *page)
  671. {
  672. copy_highpage(newpage, page);
  673. if (PageError(page))
  674. SetPageError(newpage);
  675. if (PageReferenced(page))
  676. SetPageReferenced(newpage);
  677. if (PageUptodate(page))
  678. SetPageUptodate(newpage);
  679. if (PageActive(page))
  680. SetPageActive(newpage);
  681. if (PageChecked(page))
  682. SetPageChecked(newpage);
  683. if (PageMappedToDisk(page))
  684. SetPageMappedToDisk(newpage);
  685. if (PageDirty(page)) {
  686. clear_page_dirty_for_io(page);
  687. set_page_dirty(newpage);
  688. }
  689. ClearPageSwapCache(page);
  690. ClearPageActive(page);
  691. ClearPagePrivate(page);
  692. set_page_private(page, 0);
  693. page->mapping = NULL;
  694. /*
  695. * If any waiters have accumulated on the new page then
  696. * wake them up.
  697. */
  698. if (PageWriteback(newpage))
  699. end_page_writeback(newpage);
  700. }
  701. EXPORT_SYMBOL(migrate_page_copy);
  702. /*
  703. * Common logic to directly migrate a single page suitable for
  704. * pages that do not use PagePrivate.
  705. *
  706. * Pages are locked upon entry and exit.
  707. */
  708. int migrate_page(struct page *newpage, struct page *page)
  709. {
  710. BUG_ON(PageWriteback(page)); /* Writeback must be complete */
  711. if (migrate_page_remove_references(newpage, page, 2))
  712. return -EAGAIN;
  713. migrate_page_copy(newpage, page);
  714. /*
  715. * Remove auxiliary swap entries and replace
  716. * them with real ptes.
  717. *
  718. * Note that a real pte entry will allow processes that are not
  719. * waiting on the page lock to use the new page via the page tables
  720. * before the new page is unlocked.
  721. */
  722. remove_from_swap(newpage);
  723. return 0;
  724. }
  725. EXPORT_SYMBOL(migrate_page);
  726. /*
  727. * migrate_pages
  728. *
  729. * Two lists are passed to this function. The first list
  730. * contains the pages isolated from the LRU to be migrated.
  731. * The second list contains new pages that the pages isolated
  732. * can be moved to. If the second list is NULL then all
  733. * pages are swapped out.
  734. *
  735. * The function returns after 10 attempts or if no pages
  736. * are movable anymore because to has become empty
  737. * or no retryable pages exist anymore.
  738. *
  739. * Return: Number of pages not migrated when "to" ran empty.
  740. */
  741. int migrate_pages(struct list_head *from, struct list_head *to,
  742. struct list_head *moved, struct list_head *failed)
  743. {
  744. int retry;
  745. int nr_failed = 0;
  746. int pass = 0;
  747. struct page *page;
  748. struct page *page2;
  749. int swapwrite = current->flags & PF_SWAPWRITE;
  750. int rc;
  751. if (!swapwrite)
  752. current->flags |= PF_SWAPWRITE;
  753. redo:
  754. retry = 0;
  755. list_for_each_entry_safe(page, page2, from, lru) {
  756. struct page *newpage = NULL;
  757. struct address_space *mapping;
  758. cond_resched();
  759. rc = 0;
  760. if (page_count(page) == 1)
  761. /* page was freed from under us. So we are done. */
  762. goto next;
  763. if (to && list_empty(to))
  764. break;
  765. /*
  766. * Skip locked pages during the first two passes to give the
  767. * functions holding the lock time to release the page. Later we
  768. * use lock_page() to have a higher chance of acquiring the
  769. * lock.
  770. */
  771. rc = -EAGAIN;
  772. if (pass > 2)
  773. lock_page(page);
  774. else
  775. if (TestSetPageLocked(page))
  776. goto next;
  777. /*
  778. * Only wait on writeback if we have already done a pass where
  779. * we we may have triggered writeouts for lots of pages.
  780. */
  781. if (pass > 0) {
  782. wait_on_page_writeback(page);
  783. } else {
  784. if (PageWriteback(page))
  785. goto unlock_page;
  786. }
  787. /*
  788. * Anonymous pages must have swap cache references otherwise
  789. * the information contained in the page maps cannot be
  790. * preserved.
  791. */
  792. if (PageAnon(page) && !PageSwapCache(page)) {
  793. if (!add_to_swap(page, GFP_KERNEL)) {
  794. rc = -ENOMEM;
  795. goto unlock_page;
  796. }
  797. }
  798. if (!to) {
  799. rc = swap_page(page);
  800. goto next;
  801. }
  802. newpage = lru_to_page(to);
  803. lock_page(newpage);
  804. /*
  805. * Pages are properly locked and writeback is complete.
  806. * Try to migrate the page.
  807. */
  808. mapping = page_mapping(page);
  809. if (!mapping)
  810. goto unlock_both;
  811. if (mapping->a_ops->migratepage) {
  812. /*
  813. * Most pages have a mapping and most filesystems
  814. * should provide a migration function. Anonymous
  815. * pages are part of swap space which also has its
  816. * own migration function. This is the most common
  817. * path for page migration.
  818. */
  819. rc = mapping->a_ops->migratepage(newpage, page);
  820. goto unlock_both;
  821. }
  822. /*
  823. * Default handling if a filesystem does not provide
  824. * a migration function. We can only migrate clean
  825. * pages so try to write out any dirty pages first.
  826. */
  827. if (PageDirty(page)) {
  828. switch (pageout(page, mapping)) {
  829. case PAGE_KEEP:
  830. case PAGE_ACTIVATE:
  831. goto unlock_both;
  832. case PAGE_SUCCESS:
  833. unlock_page(newpage);
  834. goto next;
  835. case PAGE_CLEAN:
  836. ; /* try to migrate the page below */
  837. }
  838. }
  839. /*
  840. * Buffers are managed in a filesystem specific way.
  841. * We must have no buffers or drop them.
  842. */
  843. if (!page_has_buffers(page) ||
  844. try_to_release_page(page, GFP_KERNEL)) {
  845. rc = migrate_page(newpage, page);
  846. goto unlock_both;
  847. }
  848. /*
  849. * On early passes with mapped pages simply
  850. * retry. There may be a lock held for some
  851. * buffers that may go away. Later
  852. * swap them out.
  853. */
  854. if (pass > 4) {
  855. /*
  856. * Persistently unable to drop buffers..... As a
  857. * measure of last resort we fall back to
  858. * swap_page().
  859. */
  860. unlock_page(newpage);
  861. newpage = NULL;
  862. rc = swap_page(page);
  863. goto next;
  864. }
  865. unlock_both:
  866. unlock_page(newpage);
  867. unlock_page:
  868. unlock_page(page);
  869. next:
  870. if (rc == -EAGAIN) {
  871. retry++;
  872. } else if (rc) {
  873. /* Permanent failure */
  874. list_move(&page->lru, failed);
  875. nr_failed++;
  876. } else {
  877. if (newpage) {
  878. /* Successful migration. Return page to LRU */
  879. move_to_lru(newpage);
  880. }
  881. list_move(&page->lru, moved);
  882. }
  883. }
  884. if (retry && pass++ < 10)
  885. goto redo;
  886. if (!swapwrite)
  887. current->flags &= ~PF_SWAPWRITE;
  888. return nr_failed + retry;
  889. }
  890. /*
  891. * Isolate one page from the LRU lists and put it on the
  892. * indicated list with elevated refcount.
  893. *
  894. * Result:
  895. * 0 = page not on LRU list
  896. * 1 = page removed from LRU list and added to the specified list.
  897. */
  898. int isolate_lru_page(struct page *page)
  899. {
  900. int ret = 0;
  901. if (PageLRU(page)) {
  902. struct zone *zone = page_zone(page);
  903. spin_lock_irq(&zone->lru_lock);
  904. if (TestClearPageLRU(page)) {
  905. ret = 1;
  906. get_page(page);
  907. if (PageActive(page))
  908. del_page_from_active_list(zone, page);
  909. else
  910. del_page_from_inactive_list(zone, page);
  911. }
  912. spin_unlock_irq(&zone->lru_lock);
  913. }
  914. return ret;
  915. }
  916. #endif
  917. /*
  918. * zone->lru_lock is heavily contended. Some of the functions that
  919. * shrink the lists perform better by taking out a batch of pages
  920. * and working on them outside the LRU lock.
  921. *
  922. * For pagecache intensive workloads, this function is the hottest
  923. * spot in the kernel (apart from copy_*_user functions).
  924. *
  925. * Appropriate locks must be held before calling this function.
  926. *
  927. * @nr_to_scan: The number of pages to look through on the list.
  928. * @src: The LRU list to pull pages off.
  929. * @dst: The temp list to put pages on to.
  930. * @scanned: The number of pages that were scanned.
  931. *
  932. * returns how many pages were moved onto *@dst.
  933. */
  934. static int isolate_lru_pages(int nr_to_scan, struct list_head *src,
  935. struct list_head *dst, int *scanned)
  936. {
  937. int nr_taken = 0;
  938. struct page *page;
  939. int scan = 0;
  940. while (scan++ < nr_to_scan && !list_empty(src)) {
  941. page = lru_to_page(src);
  942. prefetchw_prev_lru_page(page, src, flags);
  943. if (!TestClearPageLRU(page))
  944. BUG();
  945. list_del(&page->lru);
  946. if (get_page_testone(page)) {
  947. /*
  948. * It is being freed elsewhere
  949. */
  950. __put_page(page);
  951. SetPageLRU(page);
  952. list_add(&page->lru, src);
  953. continue;
  954. } else {
  955. list_add(&page->lru, dst);
  956. nr_taken++;
  957. }
  958. }
  959. *scanned = scan;
  960. return nr_taken;
  961. }
  962. /*
  963. * shrink_cache() adds the number of pages reclaimed to sc->nr_reclaimed
  964. */
  965. static void shrink_cache(struct zone *zone, struct scan_control *sc)
  966. {
  967. LIST_HEAD(page_list);
  968. struct pagevec pvec;
  969. int max_scan = sc->nr_to_scan;
  970. pagevec_init(&pvec, 1);
  971. lru_add_drain();
  972. spin_lock_irq(&zone->lru_lock);
  973. while (max_scan > 0) {
  974. struct page *page;
  975. int nr_taken;
  976. int nr_scan;
  977. int nr_freed;
  978. nr_taken = isolate_lru_pages(sc->swap_cluster_max,
  979. &zone->inactive_list,
  980. &page_list, &nr_scan);
  981. zone->nr_inactive -= nr_taken;
  982. zone->pages_scanned += nr_scan;
  983. spin_unlock_irq(&zone->lru_lock);
  984. if (nr_taken == 0)
  985. goto done;
  986. max_scan -= nr_scan;
  987. nr_freed = shrink_list(&page_list, sc);
  988. local_irq_disable();
  989. if (current_is_kswapd()) {
  990. __mod_page_state_zone(zone, pgscan_kswapd, nr_scan);
  991. __mod_page_state(kswapd_steal, nr_freed);
  992. } else
  993. __mod_page_state_zone(zone, pgscan_direct, nr_scan);
  994. __mod_page_state_zone(zone, pgsteal, nr_freed);
  995. spin_lock(&zone->lru_lock);
  996. /*
  997. * Put back any unfreeable pages.
  998. */
  999. while (!list_empty(&page_list)) {
  1000. page = lru_to_page(&page_list);
  1001. if (TestSetPageLRU(page))
  1002. BUG();
  1003. list_del(&page->lru);
  1004. if (PageActive(page))
  1005. add_page_to_active_list(zone, page);
  1006. else
  1007. add_page_to_inactive_list(zone, page);
  1008. if (!pagevec_add(&pvec, page)) {
  1009. spin_unlock_irq(&zone->lru_lock);
  1010. __pagevec_release(&pvec);
  1011. spin_lock_irq(&zone->lru_lock);
  1012. }
  1013. }
  1014. }
  1015. spin_unlock_irq(&zone->lru_lock);
  1016. done:
  1017. pagevec_release(&pvec);
  1018. }
  1019. /*
  1020. * This moves pages from the active list to the inactive list.
  1021. *
  1022. * We move them the other way if the page is referenced by one or more
  1023. * processes, from rmap.
  1024. *
  1025. * If the pages are mostly unmapped, the processing is fast and it is
  1026. * appropriate to hold zone->lru_lock across the whole operation. But if
  1027. * the pages are mapped, the processing is slow (page_referenced()) so we
  1028. * should drop zone->lru_lock around each page. It's impossible to balance
  1029. * this, so instead we remove the pages from the LRU while processing them.
  1030. * It is safe to rely on PG_active against the non-LRU pages in here because
  1031. * nobody will play with that bit on a non-LRU page.
  1032. *
  1033. * The downside is that we have to touch page->_count against each page.
  1034. * But we had to alter page->flags anyway.
  1035. */
  1036. static void
  1037. refill_inactive_zone(struct zone *zone, struct scan_control *sc)
  1038. {
  1039. int pgmoved;
  1040. int pgdeactivate = 0;
  1041. int pgscanned;
  1042. int nr_pages = sc->nr_to_scan;
  1043. LIST_HEAD(l_hold); /* The pages which were snipped off */
  1044. LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
  1045. LIST_HEAD(l_active); /* Pages to go onto the active_list */
  1046. struct page *page;
  1047. struct pagevec pvec;
  1048. int reclaim_mapped = 0;
  1049. long mapped_ratio;
  1050. long distress;
  1051. long swap_tendency;
  1052. lru_add_drain();
  1053. spin_lock_irq(&zone->lru_lock);
  1054. pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
  1055. &l_hold, &pgscanned);
  1056. zone->pages_scanned += pgscanned;
  1057. zone->nr_active -= pgmoved;
  1058. spin_unlock_irq(&zone->lru_lock);
  1059. /*
  1060. * `distress' is a measure of how much trouble we're having reclaiming
  1061. * pages. 0 -> no problems. 100 -> great trouble.
  1062. */
  1063. distress = 100 >> zone->prev_priority;
  1064. /*
  1065. * The point of this algorithm is to decide when to start reclaiming
  1066. * mapped memory instead of just pagecache. Work out how much memory
  1067. * is mapped.
  1068. */
  1069. mapped_ratio = (sc->nr_mapped * 100) / total_memory;
  1070. /*
  1071. * Now decide how much we really want to unmap some pages. The mapped
  1072. * ratio is downgraded - just because there's a lot of mapped memory
  1073. * doesn't necessarily mean that page reclaim isn't succeeding.
  1074. *
  1075. * The distress ratio is important - we don't want to start going oom.
  1076. *
  1077. * A 100% value of vm_swappiness overrides this algorithm altogether.
  1078. */
  1079. swap_tendency = mapped_ratio / 2 + distress + vm_swappiness;
  1080. /*
  1081. * Now use this metric to decide whether to start moving mapped memory
  1082. * onto the inactive list.
  1083. */
  1084. if (swap_tendency >= 100)
  1085. reclaim_mapped = 1;
  1086. while (!list_empty(&l_hold)) {
  1087. cond_resched();
  1088. page = lru_to_page(&l_hold);
  1089. list_del(&page->lru);
  1090. if (page_mapped(page)) {
  1091. if (!reclaim_mapped ||
  1092. (total_swap_pages == 0 && PageAnon(page)) ||
  1093. page_referenced(page, 0)) {
  1094. list_add(&page->lru, &l_active);
  1095. continue;
  1096. }
  1097. }
  1098. list_add(&page->lru, &l_inactive);
  1099. }
  1100. pagevec_init(&pvec, 1);
  1101. pgmoved = 0;
  1102. spin_lock_irq(&zone->lru_lock);
  1103. while (!list_empty(&l_inactive)) {
  1104. page = lru_to_page(&l_inactive);
  1105. prefetchw_prev_lru_page(page, &l_inactive, flags);
  1106. if (TestSetPageLRU(page))
  1107. BUG();
  1108. if (!TestClearPageActive(page))
  1109. BUG();
  1110. list_move(&page->lru, &zone->inactive_list);
  1111. pgmoved++;
  1112. if (!pagevec_add(&pvec, page)) {
  1113. zone->nr_inactive += pgmoved;
  1114. spin_unlock_irq(&zone->lru_lock);
  1115. pgdeactivate += pgmoved;
  1116. pgmoved = 0;
  1117. if (buffer_heads_over_limit)
  1118. pagevec_strip(&pvec);
  1119. __pagevec_release(&pvec);
  1120. spin_lock_irq(&zone->lru_lock);
  1121. }
  1122. }
  1123. zone->nr_inactive += pgmoved;
  1124. pgdeactivate += pgmoved;
  1125. if (buffer_heads_over_limit) {
  1126. spin_unlock_irq(&zone->lru_lock);
  1127. pagevec_strip(&pvec);
  1128. spin_lock_irq(&zone->lru_lock);
  1129. }
  1130. pgmoved = 0;
  1131. while (!list_empty(&l_active)) {
  1132. page = lru_to_page(&l_active);
  1133. prefetchw_prev_lru_page(page, &l_active, flags);
  1134. if (TestSetPageLRU(page))
  1135. BUG();
  1136. BUG_ON(!PageActive(page));
  1137. list_move(&page->lru, &zone->active_list);
  1138. pgmoved++;
  1139. if (!pagevec_add(&pvec, page)) {
  1140. zone->nr_active += pgmoved;
  1141. pgmoved = 0;
  1142. spin_unlock_irq(&zone->lru_lock);
  1143. __pagevec_release(&pvec);
  1144. spin_lock_irq(&zone->lru_lock);
  1145. }
  1146. }
  1147. zone->nr_active += pgmoved;
  1148. spin_unlock(&zone->lru_lock);
  1149. __mod_page_state_zone(zone, pgrefill, pgscanned);
  1150. __mod_page_state(pgdeactivate, pgdeactivate);
  1151. local_irq_enable();
  1152. pagevec_release(&pvec);
  1153. }
  1154. /*
  1155. * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
  1156. */
  1157. static void
  1158. shrink_zone(struct zone *zone, struct scan_control *sc)
  1159. {
  1160. unsigned long nr_active;
  1161. unsigned long nr_inactive;
  1162. atomic_inc(&zone->reclaim_in_progress);
  1163. /*
  1164. * Add one to `nr_to_scan' just to make sure that the kernel will
  1165. * slowly sift through the active list.
  1166. */
  1167. zone->nr_scan_active += (zone->nr_active >> sc->priority) + 1;
  1168. nr_active = zone->nr_scan_active;
  1169. if (nr_active >= sc->swap_cluster_max)
  1170. zone->nr_scan_active = 0;
  1171. else
  1172. nr_active = 0;
  1173. zone->nr_scan_inactive += (zone->nr_inactive >> sc->priority) + 1;
  1174. nr_inactive = zone->nr_scan_inactive;
  1175. if (nr_inactive >= sc->swap_cluster_max)
  1176. zone->nr_scan_inactive = 0;
  1177. else
  1178. nr_inactive = 0;
  1179. while (nr_active || nr_inactive) {
  1180. if (nr_active) {
  1181. sc->nr_to_scan = min(nr_active,
  1182. (unsigned long)sc->swap_cluster_max);
  1183. nr_active -= sc->nr_to_scan;
  1184. refill_inactive_zone(zone, sc);
  1185. }
  1186. if (nr_inactive) {
  1187. sc->nr_to_scan = min(nr_inactive,
  1188. (unsigned long)sc->swap_cluster_max);
  1189. nr_inactive -= sc->nr_to_scan;
  1190. shrink_cache(zone, sc);
  1191. }
  1192. }
  1193. throttle_vm_writeout();
  1194. atomic_dec(&zone->reclaim_in_progress);
  1195. }
  1196. /*
  1197. * This is the direct reclaim path, for page-allocating processes. We only
  1198. * try to reclaim pages from zones which will satisfy the caller's allocation
  1199. * request.
  1200. *
  1201. * We reclaim from a zone even if that zone is over pages_high. Because:
  1202. * a) The caller may be trying to free *extra* pages to satisfy a higher-order
  1203. * allocation or
  1204. * b) The zones may be over pages_high but they must go *over* pages_high to
  1205. * satisfy the `incremental min' zone defense algorithm.
  1206. *
  1207. * Returns the number of reclaimed pages.
  1208. *
  1209. * If a zone is deemed to be full of pinned pages then just give it a light
  1210. * scan then give up on it.
  1211. */
  1212. static void
  1213. shrink_caches(struct zone **zones, struct scan_control *sc)
  1214. {
  1215. int i;
  1216. for (i = 0; zones[i] != NULL; i++) {
  1217. struct zone *zone = zones[i];
  1218. if (!populated_zone(zone))
  1219. continue;
  1220. if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
  1221. continue;
  1222. zone->temp_priority = sc->priority;
  1223. if (zone->prev_priority > sc->priority)
  1224. zone->prev_priority = sc->priority;
  1225. if (zone->all_unreclaimable && sc->priority != DEF_PRIORITY)
  1226. continue; /* Let kswapd poll it */
  1227. shrink_zone(zone, sc);
  1228. }
  1229. }
  1230. /*
  1231. * This is the main entry point to direct page reclaim.
  1232. *
  1233. * If a full scan of the inactive list fails to free enough memory then we
  1234. * are "out of memory" and something needs to be killed.
  1235. *
  1236. * If the caller is !__GFP_FS then the probability of a failure is reasonably
  1237. * high - the zone may be full of dirty or under-writeback pages, which this
  1238. * caller can't do much about. We kick pdflush and take explicit naps in the
  1239. * hope that some of these pages can be written. But if the allocating task
  1240. * holds filesystem locks which prevent writeout this might not work, and the
  1241. * allocation attempt will fail.
  1242. */
  1243. int try_to_free_pages(struct zone **zones, gfp_t gfp_mask)
  1244. {
  1245. int priority;
  1246. int ret = 0;
  1247. int total_scanned = 0, total_reclaimed = 0;
  1248. struct reclaim_state *reclaim_state = current->reclaim_state;
  1249. struct scan_control sc;
  1250. unsigned long lru_pages = 0;
  1251. int i;
  1252. sc.gfp_mask = gfp_mask;
  1253. sc.may_writepage = !laptop_mode;
  1254. sc.may_swap = 1;
  1255. inc_page_state(allocstall);
  1256. for (i = 0; zones[i] != NULL; i++) {
  1257. struct zone *zone = zones[i];
  1258. if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
  1259. continue;
  1260. zone->temp_priority = DEF_PRIORITY;
  1261. lru_pages += zone->nr_active + zone->nr_inactive;
  1262. }
  1263. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  1264. sc.nr_mapped = read_page_state(nr_mapped);
  1265. sc.nr_scanned = 0;
  1266. sc.nr_reclaimed = 0;
  1267. sc.priority = priority;
  1268. sc.swap_cluster_max = SWAP_CLUSTER_MAX;
  1269. if (!priority)
  1270. disable_swap_token();
  1271. shrink_caches(zones, &sc);
  1272. shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
  1273. if (reclaim_state) {
  1274. sc.nr_reclaimed += reclaim_state->reclaimed_slab;
  1275. reclaim_state->reclaimed_slab = 0;
  1276. }
  1277. total_scanned += sc.nr_scanned;
  1278. total_reclaimed += sc.nr_reclaimed;
  1279. if (total_reclaimed >= sc.swap_cluster_max) {
  1280. ret = 1;
  1281. goto out;
  1282. }
  1283. /*
  1284. * Try to write back as many pages as we just scanned. This
  1285. * tends to cause slow streaming writers to write data to the
  1286. * disk smoothly, at the dirtying rate, which is nice. But
  1287. * that's undesirable in laptop mode, where we *want* lumpy
  1288. * writeout. So in laptop mode, write out the whole world.
  1289. */
  1290. if (total_scanned > sc.swap_cluster_max + sc.swap_cluster_max/2) {
  1291. wakeup_pdflush(laptop_mode ? 0 : total_scanned);
  1292. sc.may_writepage = 1;
  1293. }
  1294. /* Take a nap, wait for some writeback to complete */
  1295. if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
  1296. blk_congestion_wait(WRITE, HZ/10);
  1297. }
  1298. out:
  1299. for (i = 0; zones[i] != 0; i++) {
  1300. struct zone *zone = zones[i];
  1301. if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
  1302. continue;
  1303. zone->prev_priority = zone->temp_priority;
  1304. }
  1305. return ret;
  1306. }
  1307. /*
  1308. * For kswapd, balance_pgdat() will work across all this node's zones until
  1309. * they are all at pages_high.
  1310. *
  1311. * If `nr_pages' is non-zero then it is the number of pages which are to be
  1312. * reclaimed, regardless of the zone occupancies. This is a software suspend
  1313. * special.
  1314. *
  1315. * Returns the number of pages which were actually freed.
  1316. *
  1317. * There is special handling here for zones which are full of pinned pages.
  1318. * This can happen if the pages are all mlocked, or if they are all used by
  1319. * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
  1320. * What we do is to detect the case where all pages in the zone have been
  1321. * scanned twice and there has been zero successful reclaim. Mark the zone as
  1322. * dead and from now on, only perform a short scan. Basically we're polling
  1323. * the zone for when the problem goes away.
  1324. *
  1325. * kswapd scans the zones in the highmem->normal->dma direction. It skips
  1326. * zones which have free_pages > pages_high, but once a zone is found to have
  1327. * free_pages <= pages_high, we scan that zone and the lower zones regardless
  1328. * of the number of free pages in the lower zones. This interoperates with
  1329. * the page allocator fallback scheme to ensure that aging of pages is balanced
  1330. * across the zones.
  1331. */
  1332. static int balance_pgdat(pg_data_t *pgdat, int nr_pages, int order)
  1333. {
  1334. int to_free = nr_pages;
  1335. int all_zones_ok;
  1336. int priority;
  1337. int i;
  1338. int total_scanned, total_reclaimed;
  1339. struct reclaim_state *reclaim_state = current->reclaim_state;
  1340. struct scan_control sc;
  1341. loop_again:
  1342. total_scanned = 0;
  1343. total_reclaimed = 0;
  1344. sc.gfp_mask = GFP_KERNEL;
  1345. sc.may_writepage = !laptop_mode;
  1346. sc.may_swap = 1;
  1347. sc.nr_mapped = read_page_state(nr_mapped);
  1348. inc_page_state(pageoutrun);
  1349. for (i = 0; i < pgdat->nr_zones; i++) {
  1350. struct zone *zone = pgdat->node_zones + i;
  1351. zone->temp_priority = DEF_PRIORITY;
  1352. }
  1353. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  1354. int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
  1355. unsigned long lru_pages = 0;
  1356. /* The swap token gets in the way of swapout... */
  1357. if (!priority)
  1358. disable_swap_token();
  1359. all_zones_ok = 1;
  1360. if (nr_pages == 0) {
  1361. /*
  1362. * Scan in the highmem->dma direction for the highest
  1363. * zone which needs scanning
  1364. */
  1365. for (i = pgdat->nr_zones - 1; i >= 0; i--) {
  1366. struct zone *zone = pgdat->node_zones + i;
  1367. if (!populated_zone(zone))
  1368. continue;
  1369. if (zone->all_unreclaimable &&
  1370. priority != DEF_PRIORITY)
  1371. continue;
  1372. if (!zone_watermark_ok(zone, order,
  1373. zone->pages_high, 0, 0)) {
  1374. end_zone = i;
  1375. goto scan;
  1376. }
  1377. }
  1378. goto out;
  1379. } else {
  1380. end_zone = pgdat->nr_zones - 1;
  1381. }
  1382. scan:
  1383. for (i = 0; i <= end_zone; i++) {
  1384. struct zone *zone = pgdat->node_zones + i;
  1385. lru_pages += zone->nr_active + zone->nr_inactive;
  1386. }
  1387. /*
  1388. * Now scan the zone in the dma->highmem direction, stopping
  1389. * at the last zone which needs scanning.
  1390. *
  1391. * We do this because the page allocator works in the opposite
  1392. * direction. This prevents the page allocator from allocating
  1393. * pages behind kswapd's direction of progress, which would
  1394. * cause too much scanning of the lower zones.
  1395. */
  1396. for (i = 0; i <= end_zone; i++) {
  1397. struct zone *zone = pgdat->node_zones + i;
  1398. int nr_slab;
  1399. if (!populated_zone(zone))
  1400. continue;
  1401. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  1402. continue;
  1403. if (nr_pages == 0) { /* Not software suspend */
  1404. if (!zone_watermark_ok(zone, order,
  1405. zone->pages_high, end_zone, 0))
  1406. all_zones_ok = 0;
  1407. }
  1408. zone->temp_priority = priority;
  1409. if (zone->prev_priority > priority)
  1410. zone->prev_priority = priority;
  1411. sc.nr_scanned = 0;
  1412. sc.nr_reclaimed = 0;
  1413. sc.priority = priority;
  1414. sc.swap_cluster_max = nr_pages? nr_pages : SWAP_CLUSTER_MAX;
  1415. atomic_inc(&zone->reclaim_in_progress);
  1416. shrink_zone(zone, &sc);
  1417. atomic_dec(&zone->reclaim_in_progress);
  1418. reclaim_state->reclaimed_slab = 0;
  1419. nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
  1420. lru_pages);
  1421. sc.nr_reclaimed += reclaim_state->reclaimed_slab;
  1422. total_reclaimed += sc.nr_reclaimed;
  1423. total_scanned += sc.nr_scanned;
  1424. if (zone->all_unreclaimable)
  1425. continue;
  1426. if (nr_slab == 0 && zone->pages_scanned >=
  1427. (zone->nr_active + zone->nr_inactive) * 4)
  1428. zone->all_unreclaimable = 1;
  1429. /*
  1430. * If we've done a decent amount of scanning and
  1431. * the reclaim ratio is low, start doing writepage
  1432. * even in laptop mode
  1433. */
  1434. if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
  1435. total_scanned > total_reclaimed+total_reclaimed/2)
  1436. sc.may_writepage = 1;
  1437. }
  1438. if (nr_pages && to_free > total_reclaimed)
  1439. continue; /* swsusp: need to do more work */
  1440. if (all_zones_ok)
  1441. break; /* kswapd: all done */
  1442. /*
  1443. * OK, kswapd is getting into trouble. Take a nap, then take
  1444. * another pass across the zones.
  1445. */
  1446. if (total_scanned && priority < DEF_PRIORITY - 2)
  1447. blk_congestion_wait(WRITE, HZ/10);
  1448. /*
  1449. * We do this so kswapd doesn't build up large priorities for
  1450. * example when it is freeing in parallel with allocators. It
  1451. * matches the direct reclaim path behaviour in terms of impact
  1452. * on zone->*_priority.
  1453. */
  1454. if ((total_reclaimed >= SWAP_CLUSTER_MAX) && (!nr_pages))
  1455. break;
  1456. }
  1457. out:
  1458. for (i = 0; i < pgdat->nr_zones; i++) {
  1459. struct zone *zone = pgdat->node_zones + i;
  1460. zone->prev_priority = zone->temp_priority;
  1461. }
  1462. if (!all_zones_ok) {
  1463. cond_resched();
  1464. goto loop_again;
  1465. }
  1466. return total_reclaimed;
  1467. }
  1468. /*
  1469. * The background pageout daemon, started as a kernel thread
  1470. * from the init process.
  1471. *
  1472. * This basically trickles out pages so that we have _some_
  1473. * free memory available even if there is no other activity
  1474. * that frees anything up. This is needed for things like routing
  1475. * etc, where we otherwise might have all activity going on in
  1476. * asynchronous contexts that cannot page things out.
  1477. *
  1478. * If there are applications that are active memory-allocators
  1479. * (most normal use), this basically shouldn't matter.
  1480. */
  1481. static int kswapd(void *p)
  1482. {
  1483. unsigned long order;
  1484. pg_data_t *pgdat = (pg_data_t*)p;
  1485. struct task_struct *tsk = current;
  1486. DEFINE_WAIT(wait);
  1487. struct reclaim_state reclaim_state = {
  1488. .reclaimed_slab = 0,
  1489. };
  1490. cpumask_t cpumask;
  1491. daemonize("kswapd%d", pgdat->node_id);
  1492. cpumask = node_to_cpumask(pgdat->node_id);
  1493. if (!cpus_empty(cpumask))
  1494. set_cpus_allowed(tsk, cpumask);
  1495. current->reclaim_state = &reclaim_state;
  1496. /*
  1497. * Tell the memory management that we're a "memory allocator",
  1498. * and that if we need more memory we should get access to it
  1499. * regardless (see "__alloc_pages()"). "kswapd" should
  1500. * never get caught in the normal page freeing logic.
  1501. *
  1502. * (Kswapd normally doesn't need memory anyway, but sometimes
  1503. * you need a small amount of memory in order to be able to
  1504. * page out something else, and this flag essentially protects
  1505. * us from recursively trying to free more memory as we're
  1506. * trying to free the first piece of memory in the first place).
  1507. */
  1508. tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
  1509. order = 0;
  1510. for ( ; ; ) {
  1511. unsigned long new_order;
  1512. try_to_freeze();
  1513. prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
  1514. new_order = pgdat->kswapd_max_order;
  1515. pgdat->kswapd_max_order = 0;
  1516. if (order < new_order) {
  1517. /*
  1518. * Don't sleep if someone wants a larger 'order'
  1519. * allocation
  1520. */
  1521. order = new_order;
  1522. } else {
  1523. schedule();
  1524. order = pgdat->kswapd_max_order;
  1525. }
  1526. finish_wait(&pgdat->kswapd_wait, &wait);
  1527. balance_pgdat(pgdat, 0, order);
  1528. }
  1529. return 0;
  1530. }
  1531. /*
  1532. * A zone is low on free memory, so wake its kswapd task to service it.
  1533. */
  1534. void wakeup_kswapd(struct zone *zone, int order)
  1535. {
  1536. pg_data_t *pgdat;
  1537. if (!populated_zone(zone))
  1538. return;
  1539. pgdat = zone->zone_pgdat;
  1540. if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
  1541. return;
  1542. if (pgdat->kswapd_max_order < order)
  1543. pgdat->kswapd_max_order = order;
  1544. if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
  1545. return;
  1546. if (!waitqueue_active(&pgdat->kswapd_wait))
  1547. return;
  1548. wake_up_interruptible(&pgdat->kswapd_wait);
  1549. }
  1550. #ifdef CONFIG_PM
  1551. /*
  1552. * Try to free `nr_pages' of memory, system-wide. Returns the number of freed
  1553. * pages.
  1554. */
  1555. int shrink_all_memory(int nr_pages)
  1556. {
  1557. pg_data_t *pgdat;
  1558. int nr_to_free = nr_pages;
  1559. int ret = 0;
  1560. struct reclaim_state reclaim_state = {
  1561. .reclaimed_slab = 0,
  1562. };
  1563. current->reclaim_state = &reclaim_state;
  1564. for_each_pgdat(pgdat) {
  1565. int freed;
  1566. freed = balance_pgdat(pgdat, nr_to_free, 0);
  1567. ret += freed;
  1568. nr_to_free -= freed;
  1569. if (nr_to_free <= 0)
  1570. break;
  1571. }
  1572. current->reclaim_state = NULL;
  1573. return ret;
  1574. }
  1575. #endif
  1576. #ifdef CONFIG_HOTPLUG_CPU
  1577. /* It's optimal to keep kswapds on the same CPUs as their memory, but
  1578. not required for correctness. So if the last cpu in a node goes
  1579. away, we get changed to run anywhere: as the first one comes back,
  1580. restore their cpu bindings. */
  1581. static int __devinit cpu_callback(struct notifier_block *nfb,
  1582. unsigned long action,
  1583. void *hcpu)
  1584. {
  1585. pg_data_t *pgdat;
  1586. cpumask_t mask;
  1587. if (action == CPU_ONLINE) {
  1588. for_each_pgdat(pgdat) {
  1589. mask = node_to_cpumask(pgdat->node_id);
  1590. if (any_online_cpu(mask) != NR_CPUS)
  1591. /* One of our CPUs online: restore mask */
  1592. set_cpus_allowed(pgdat->kswapd, mask);
  1593. }
  1594. }
  1595. return NOTIFY_OK;
  1596. }
  1597. #endif /* CONFIG_HOTPLUG_CPU */
  1598. static int __init kswapd_init(void)
  1599. {
  1600. pg_data_t *pgdat;
  1601. swap_setup();
  1602. for_each_pgdat(pgdat)
  1603. pgdat->kswapd
  1604. = find_task_by_pid(kernel_thread(kswapd, pgdat, CLONE_KERNEL));
  1605. total_memory = nr_free_pagecache_pages();
  1606. hotcpu_notifier(cpu_callback, 0);
  1607. return 0;
  1608. }
  1609. module_init(kswapd_init)
  1610. #ifdef CONFIG_NUMA
  1611. /*
  1612. * Zone reclaim mode
  1613. *
  1614. * If non-zero call zone_reclaim when the number of free pages falls below
  1615. * the watermarks.
  1616. *
  1617. * In the future we may add flags to the mode. However, the page allocator
  1618. * should only have to check that zone_reclaim_mode != 0 before calling
  1619. * zone_reclaim().
  1620. */
  1621. int zone_reclaim_mode __read_mostly;
  1622. #define RECLAIM_OFF 0
  1623. #define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
  1624. #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
  1625. #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
  1626. #define RECLAIM_SLAB (1<<3) /* Do a global slab shrink if the zone is out of memory */
  1627. /*
  1628. * Mininum time between zone reclaim scans
  1629. */
  1630. int zone_reclaim_interval __read_mostly = 30*HZ;
  1631. /*
  1632. * Priority for ZONE_RECLAIM. This determines the fraction of pages
  1633. * of a node considered for each zone_reclaim. 4 scans 1/16th of
  1634. * a zone.
  1635. */
  1636. #define ZONE_RECLAIM_PRIORITY 4
  1637. /*
  1638. * Try to free up some pages from this zone through reclaim.
  1639. */
  1640. int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  1641. {
  1642. int nr_pages;
  1643. struct task_struct *p = current;
  1644. struct reclaim_state reclaim_state;
  1645. struct scan_control sc;
  1646. cpumask_t mask;
  1647. int node_id;
  1648. if (time_before(jiffies,
  1649. zone->last_unsuccessful_zone_reclaim + zone_reclaim_interval))
  1650. return 0;
  1651. if (!(gfp_mask & __GFP_WAIT) ||
  1652. zone->all_unreclaimable ||
  1653. atomic_read(&zone->reclaim_in_progress) > 0)
  1654. return 0;
  1655. node_id = zone->zone_pgdat->node_id;
  1656. mask = node_to_cpumask(node_id);
  1657. if (!cpus_empty(mask) && node_id != numa_node_id())
  1658. return 0;
  1659. sc.may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE);
  1660. sc.may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP);
  1661. sc.nr_scanned = 0;
  1662. sc.nr_reclaimed = 0;
  1663. sc.priority = ZONE_RECLAIM_PRIORITY + 1;
  1664. sc.nr_mapped = read_page_state(nr_mapped);
  1665. sc.gfp_mask = gfp_mask;
  1666. disable_swap_token();
  1667. nr_pages = 1 << order;
  1668. if (nr_pages > SWAP_CLUSTER_MAX)
  1669. sc.swap_cluster_max = nr_pages;
  1670. else
  1671. sc.swap_cluster_max = SWAP_CLUSTER_MAX;
  1672. cond_resched();
  1673. p->flags |= PF_MEMALLOC;
  1674. reclaim_state.reclaimed_slab = 0;
  1675. p->reclaim_state = &reclaim_state;
  1676. /*
  1677. * Free memory by calling shrink zone with increasing priorities
  1678. * until we have enough memory freed.
  1679. */
  1680. do {
  1681. sc.priority--;
  1682. shrink_zone(zone, &sc);
  1683. } while (sc.nr_reclaimed < nr_pages && sc.priority > 0);
  1684. if (sc.nr_reclaimed < nr_pages && (zone_reclaim_mode & RECLAIM_SLAB)) {
  1685. /*
  1686. * shrink_slab does not currently allow us to determine
  1687. * how many pages were freed in the zone. So we just
  1688. * shake the slab and then go offnode for a single allocation.
  1689. *
  1690. * shrink_slab will free memory on all zones and may take
  1691. * a long time.
  1692. */
  1693. shrink_slab(sc.nr_scanned, gfp_mask, order);
  1694. sc.nr_reclaimed = 1; /* Avoid getting the off node timeout */
  1695. }
  1696. p->reclaim_state = NULL;
  1697. current->flags &= ~PF_MEMALLOC;
  1698. if (sc.nr_reclaimed == 0)
  1699. zone->last_unsuccessful_zone_reclaim = jiffies;
  1700. return sc.nr_reclaimed >= nr_pages;
  1701. }
  1702. #endif