vmscan.c 61 KB

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