vmscan.c 51 KB

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