vmscan.c 55 KB

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