vmscan.c 57 KB

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