vmscan.c 55 KB

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