]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | ||
14 | #include <linux/mm.h> | |
15 | #include <linux/module.h> | |
16 | #include <linux/slab.h> | |
17 | #include <linux/kernel_stat.h> | |
18 | #include <linux/swap.h> | |
19 | #include <linux/pagemap.h> | |
20 | #include <linux/init.h> | |
21 | #include <linux/highmem.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 | ||
37 | #include <asm/tlbflush.h> | |
38 | #include <asm/div64.h> | |
39 | ||
40 | #include <linux/swapops.h> | |
41 | ||
42 | /* possible outcome of pageout() */ | |
43 | typedef enum { | |
44 | /* failed to write page out, page is locked */ | |
45 | PAGE_KEEP, | |
46 | /* move page to the active list, page is locked */ | |
47 | PAGE_ACTIVATE, | |
48 | /* page has been sent to the disk successfully, page is unlocked */ | |
49 | PAGE_SUCCESS, | |
50 | /* page is clean and locked */ | |
51 | PAGE_CLEAN, | |
52 | } pageout_t; | |
53 | ||
54 | struct scan_control { | |
55 | /* Ask refill_inactive_zone, or shrink_cache to scan this many pages */ | |
56 | unsigned long nr_to_scan; | |
57 | ||
58 | /* Incremented by the number of inactive pages that were scanned */ | |
59 | unsigned long nr_scanned; | |
60 | ||
61 | /* Incremented by the number of pages reclaimed */ | |
62 | unsigned long nr_reclaimed; | |
63 | ||
64 | unsigned long nr_mapped; /* From page_state */ | |
65 | ||
66 | /* How many pages shrink_cache() should reclaim */ | |
67 | int nr_to_reclaim; | |
68 | ||
69 | /* Ask shrink_caches, or shrink_zone to scan at this priority */ | |
70 | unsigned int priority; | |
71 | ||
72 | /* This context's GFP mask */ | |
73 | unsigned int gfp_mask; | |
74 | ||
75 | int may_writepage; | |
76 | ||
77 | /* This context's SWAP_CLUSTER_MAX. If freeing memory for | |
78 | * suspend, we effectively ignore SWAP_CLUSTER_MAX. | |
79 | * In this context, it doesn't matter that we scan the | |
80 | * whole list at once. */ | |
81 | int swap_cluster_max; | |
82 | }; | |
83 | ||
84 | /* | |
85 | * The list of shrinker callbacks used by to apply pressure to | |
86 | * ageable caches. | |
87 | */ | |
88 | struct shrinker { | |
89 | shrinker_t shrinker; | |
90 | struct list_head list; | |
91 | int seeks; /* seeks to recreate an obj */ | |
92 | long nr; /* objs pending delete */ | |
93 | }; | |
94 | ||
95 | #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru)) | |
96 | ||
97 | #ifdef ARCH_HAS_PREFETCH | |
98 | #define prefetch_prev_lru_page(_page, _base, _field) \ | |
99 | do { \ | |
100 | if ((_page)->lru.prev != _base) { \ | |
101 | struct page *prev; \ | |
102 | \ | |
103 | prev = lru_to_page(&(_page->lru)); \ | |
104 | prefetch(&prev->_field); \ | |
105 | } \ | |
106 | } while (0) | |
107 | #else | |
108 | #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0) | |
109 | #endif | |
110 | ||
111 | #ifdef ARCH_HAS_PREFETCHW | |
112 | #define prefetchw_prev_lru_page(_page, _base, _field) \ | |
113 | do { \ | |
114 | if ((_page)->lru.prev != _base) { \ | |
115 | struct page *prev; \ | |
116 | \ | |
117 | prev = lru_to_page(&(_page->lru)); \ | |
118 | prefetchw(&prev->_field); \ | |
119 | } \ | |
120 | } while (0) | |
121 | #else | |
122 | #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) | |
123 | #endif | |
124 | ||
125 | /* | |
126 | * From 0 .. 100. Higher means more swappy. | |
127 | */ | |
128 | int vm_swappiness = 60; | |
129 | static long total_memory; | |
130 | ||
131 | static LIST_HEAD(shrinker_list); | |
132 | static DECLARE_RWSEM(shrinker_rwsem); | |
133 | ||
134 | /* | |
135 | * Add a shrinker callback to be called from the vm | |
136 | */ | |
137 | struct shrinker *set_shrinker(int seeks, shrinker_t theshrinker) | |
138 | { | |
139 | struct shrinker *shrinker; | |
140 | ||
141 | shrinker = kmalloc(sizeof(*shrinker), GFP_KERNEL); | |
142 | if (shrinker) { | |
143 | shrinker->shrinker = theshrinker; | |
144 | shrinker->seeks = seeks; | |
145 | shrinker->nr = 0; | |
146 | down_write(&shrinker_rwsem); | |
147 | list_add_tail(&shrinker->list, &shrinker_list); | |
148 | up_write(&shrinker_rwsem); | |
149 | } | |
150 | return shrinker; | |
151 | } | |
152 | EXPORT_SYMBOL(set_shrinker); | |
153 | ||
154 | /* | |
155 | * Remove one | |
156 | */ | |
157 | void remove_shrinker(struct shrinker *shrinker) | |
158 | { | |
159 | down_write(&shrinker_rwsem); | |
160 | list_del(&shrinker->list); | |
161 | up_write(&shrinker_rwsem); | |
162 | kfree(shrinker); | |
163 | } | |
164 | EXPORT_SYMBOL(remove_shrinker); | |
165 | ||
166 | #define SHRINK_BATCH 128 | |
167 | /* | |
168 | * Call the shrink functions to age shrinkable caches | |
169 | * | |
170 | * Here we assume it costs one seek to replace a lru page and that it also | |
171 | * takes a seek to recreate a cache object. With this in mind we age equal | |
172 | * percentages of the lru and ageable caches. This should balance the seeks | |
173 | * generated by these structures. | |
174 | * | |
175 | * If the vm encounted mapped pages on the LRU it increase the pressure on | |
176 | * slab to avoid swapping. | |
177 | * | |
178 | * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits. | |
179 | * | |
180 | * `lru_pages' represents the number of on-LRU pages in all the zones which | |
181 | * are eligible for the caller's allocation attempt. It is used for balancing | |
182 | * slab reclaim versus page reclaim. | |
b15e0905 | 183 | * |
184 | * Returns the number of slab objects which we shrunk. | |
1da177e4 LT |
185 | */ |
186 | static int shrink_slab(unsigned long scanned, unsigned int gfp_mask, | |
187 | unsigned long lru_pages) | |
188 | { | |
189 | struct shrinker *shrinker; | |
b15e0905 | 190 | int ret = 0; |
1da177e4 LT |
191 | |
192 | if (scanned == 0) | |
193 | scanned = SWAP_CLUSTER_MAX; | |
194 | ||
195 | if (!down_read_trylock(&shrinker_rwsem)) | |
b15e0905 | 196 | return 1; /* Assume we'll be able to shrink next time */ |
1da177e4 LT |
197 | |
198 | list_for_each_entry(shrinker, &shrinker_list, list) { | |
199 | unsigned long long delta; | |
200 | unsigned long total_scan; | |
201 | ||
202 | delta = (4 * scanned) / shrinker->seeks; | |
203 | delta *= (*shrinker->shrinker)(0, gfp_mask); | |
204 | do_div(delta, lru_pages + 1); | |
205 | shrinker->nr += delta; | |
206 | if (shrinker->nr < 0) | |
207 | shrinker->nr = LONG_MAX; /* It wrapped! */ | |
208 | ||
209 | total_scan = shrinker->nr; | |
210 | shrinker->nr = 0; | |
211 | ||
212 | while (total_scan >= SHRINK_BATCH) { | |
213 | long this_scan = SHRINK_BATCH; | |
214 | int shrink_ret; | |
b15e0905 | 215 | int nr_before; |
1da177e4 | 216 | |
b15e0905 | 217 | nr_before = (*shrinker->shrinker)(0, gfp_mask); |
1da177e4 LT |
218 | shrink_ret = (*shrinker->shrinker)(this_scan, gfp_mask); |
219 | if (shrink_ret == -1) | |
220 | break; | |
b15e0905 | 221 | if (shrink_ret < nr_before) |
222 | ret += nr_before - shrink_ret; | |
1da177e4 LT |
223 | mod_page_state(slabs_scanned, this_scan); |
224 | total_scan -= this_scan; | |
225 | ||
226 | cond_resched(); | |
227 | } | |
228 | ||
229 | shrinker->nr += total_scan; | |
230 | } | |
231 | up_read(&shrinker_rwsem); | |
b15e0905 | 232 | return ret; |
1da177e4 LT |
233 | } |
234 | ||
235 | /* Called without lock on whether page is mapped, so answer is unstable */ | |
236 | static inline int page_mapping_inuse(struct page *page) | |
237 | { | |
238 | struct address_space *mapping; | |
239 | ||
240 | /* Page is in somebody's page tables. */ | |
241 | if (page_mapped(page)) | |
242 | return 1; | |
243 | ||
244 | /* Be more reluctant to reclaim swapcache than pagecache */ | |
245 | if (PageSwapCache(page)) | |
246 | return 1; | |
247 | ||
248 | mapping = page_mapping(page); | |
249 | if (!mapping) | |
250 | return 0; | |
251 | ||
252 | /* File is mmap'd by somebody? */ | |
253 | return mapping_mapped(mapping); | |
254 | } | |
255 | ||
256 | static inline int is_page_cache_freeable(struct page *page) | |
257 | { | |
258 | return page_count(page) - !!PagePrivate(page) == 2; | |
259 | } | |
260 | ||
261 | static int may_write_to_queue(struct backing_dev_info *bdi) | |
262 | { | |
263 | if (current_is_kswapd()) | |
264 | return 1; | |
265 | if (current_is_pdflush()) /* This is unlikely, but why not... */ | |
266 | return 1; | |
267 | if (!bdi_write_congested(bdi)) | |
268 | return 1; | |
269 | if (bdi == current->backing_dev_info) | |
270 | return 1; | |
271 | return 0; | |
272 | } | |
273 | ||
274 | /* | |
275 | * We detected a synchronous write error writing a page out. Probably | |
276 | * -ENOSPC. We need to propagate that into the address_space for a subsequent | |
277 | * fsync(), msync() or close(). | |
278 | * | |
279 | * The tricky part is that after writepage we cannot touch the mapping: nothing | |
280 | * prevents it from being freed up. But we have a ref on the page and once | |
281 | * that page is locked, the mapping is pinned. | |
282 | * | |
283 | * We're allowed to run sleeping lock_page() here because we know the caller has | |
284 | * __GFP_FS. | |
285 | */ | |
286 | static void handle_write_error(struct address_space *mapping, | |
287 | struct page *page, int error) | |
288 | { | |
289 | lock_page(page); | |
290 | if (page_mapping(page) == mapping) { | |
291 | if (error == -ENOSPC) | |
292 | set_bit(AS_ENOSPC, &mapping->flags); | |
293 | else | |
294 | set_bit(AS_EIO, &mapping->flags); | |
295 | } | |
296 | unlock_page(page); | |
297 | } | |
298 | ||
299 | /* | |
300 | * pageout is called by shrink_list() for each dirty page. Calls ->writepage(). | |
301 | */ | |
302 | static pageout_t pageout(struct page *page, struct address_space *mapping) | |
303 | { | |
304 | /* | |
305 | * If the page is dirty, only perform writeback if that write | |
306 | * will be non-blocking. To prevent this allocation from being | |
307 | * stalled by pagecache activity. But note that there may be | |
308 | * stalls if we need to run get_block(). We could test | |
309 | * PagePrivate for that. | |
310 | * | |
311 | * If this process is currently in generic_file_write() against | |
312 | * this page's queue, we can perform writeback even if that | |
313 | * will block. | |
314 | * | |
315 | * If the page is swapcache, write it back even if that would | |
316 | * block, for some throttling. This happens by accident, because | |
317 | * swap_backing_dev_info is bust: it doesn't reflect the | |
318 | * congestion state of the swapdevs. Easy to fix, if needed. | |
319 | * See swapfile.c:page_queue_congested(). | |
320 | */ | |
321 | if (!is_page_cache_freeable(page)) | |
322 | return PAGE_KEEP; | |
323 | if (!mapping) { | |
324 | /* | |
325 | * Some data journaling orphaned pages can have | |
326 | * page->mapping == NULL while being dirty with clean buffers. | |
327 | */ | |
323aca6c | 328 | if (PagePrivate(page)) { |
1da177e4 LT |
329 | if (try_to_free_buffers(page)) { |
330 | ClearPageDirty(page); | |
331 | printk("%s: orphaned page\n", __FUNCTION__); | |
332 | return PAGE_CLEAN; | |
333 | } | |
334 | } | |
335 | return PAGE_KEEP; | |
336 | } | |
337 | if (mapping->a_ops->writepage == NULL) | |
338 | return PAGE_ACTIVATE; | |
339 | if (!may_write_to_queue(mapping->backing_dev_info)) | |
340 | return PAGE_KEEP; | |
341 | ||
342 | if (clear_page_dirty_for_io(page)) { | |
343 | int res; | |
344 | struct writeback_control wbc = { | |
345 | .sync_mode = WB_SYNC_NONE, | |
346 | .nr_to_write = SWAP_CLUSTER_MAX, | |
347 | .nonblocking = 1, | |
348 | .for_reclaim = 1, | |
349 | }; | |
350 | ||
351 | SetPageReclaim(page); | |
352 | res = mapping->a_ops->writepage(page, &wbc); | |
353 | if (res < 0) | |
354 | handle_write_error(mapping, page, res); | |
355 | if (res == WRITEPAGE_ACTIVATE) { | |
356 | ClearPageReclaim(page); | |
357 | return PAGE_ACTIVATE; | |
358 | } | |
359 | if (!PageWriteback(page)) { | |
360 | /* synchronous write or broken a_ops? */ | |
361 | ClearPageReclaim(page); | |
362 | } | |
363 | ||
364 | return PAGE_SUCCESS; | |
365 | } | |
366 | ||
367 | return PAGE_CLEAN; | |
368 | } | |
369 | ||
370 | /* | |
371 | * shrink_list adds the number of reclaimed pages to sc->nr_reclaimed | |
372 | */ | |
373 | static int shrink_list(struct list_head *page_list, struct scan_control *sc) | |
374 | { | |
375 | LIST_HEAD(ret_pages); | |
376 | struct pagevec freed_pvec; | |
377 | int pgactivate = 0; | |
378 | int reclaimed = 0; | |
379 | ||
380 | cond_resched(); | |
381 | ||
382 | pagevec_init(&freed_pvec, 1); | |
383 | while (!list_empty(page_list)) { | |
384 | struct address_space *mapping; | |
385 | struct page *page; | |
386 | int may_enter_fs; | |
387 | int referenced; | |
388 | ||
389 | cond_resched(); | |
390 | ||
391 | page = lru_to_page(page_list); | |
392 | list_del(&page->lru); | |
393 | ||
394 | if (TestSetPageLocked(page)) | |
395 | goto keep; | |
396 | ||
397 | BUG_ON(PageActive(page)); | |
398 | ||
399 | sc->nr_scanned++; | |
400 | /* Double the slab pressure for mapped and swapcache pages */ | |
401 | if (page_mapped(page) || PageSwapCache(page)) | |
402 | sc->nr_scanned++; | |
403 | ||
404 | if (PageWriteback(page)) | |
405 | goto keep_locked; | |
406 | ||
407 | referenced = page_referenced(page, 1, sc->priority <= 0); | |
408 | /* In active use or really unfreeable? Activate it. */ | |
409 | if (referenced && page_mapping_inuse(page)) | |
410 | goto activate_locked; | |
411 | ||
412 | #ifdef CONFIG_SWAP | |
413 | /* | |
414 | * Anonymous process memory has backing store? | |
415 | * Try to allocate it some swap space here. | |
416 | */ | |
417 | if (PageAnon(page) && !PageSwapCache(page)) { | |
418 | if (!add_to_swap(page)) | |
419 | goto activate_locked; | |
420 | } | |
421 | #endif /* CONFIG_SWAP */ | |
422 | ||
423 | mapping = page_mapping(page); | |
424 | may_enter_fs = (sc->gfp_mask & __GFP_FS) || | |
425 | (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); | |
426 | ||
427 | /* | |
428 | * The page is mapped into the page tables of one or more | |
429 | * processes. Try to unmap it here. | |
430 | */ | |
431 | if (page_mapped(page) && mapping) { | |
432 | switch (try_to_unmap(page)) { | |
433 | case SWAP_FAIL: | |
434 | goto activate_locked; | |
435 | case SWAP_AGAIN: | |
436 | goto keep_locked; | |
437 | case SWAP_SUCCESS: | |
438 | ; /* try to free the page below */ | |
439 | } | |
440 | } | |
441 | ||
442 | if (PageDirty(page)) { | |
443 | if (referenced) | |
444 | goto keep_locked; | |
445 | if (!may_enter_fs) | |
446 | goto keep_locked; | |
447 | if (laptop_mode && !sc->may_writepage) | |
448 | goto keep_locked; | |
449 | ||
450 | /* Page is dirty, try to write it out here */ | |
451 | switch(pageout(page, mapping)) { | |
452 | case PAGE_KEEP: | |
453 | goto keep_locked; | |
454 | case PAGE_ACTIVATE: | |
455 | goto activate_locked; | |
456 | case PAGE_SUCCESS: | |
457 | if (PageWriteback(page) || PageDirty(page)) | |
458 | goto keep; | |
459 | /* | |
460 | * A synchronous write - probably a ramdisk. Go | |
461 | * ahead and try to reclaim the page. | |
462 | */ | |
463 | if (TestSetPageLocked(page)) | |
464 | goto keep; | |
465 | if (PageDirty(page) || PageWriteback(page)) | |
466 | goto keep_locked; | |
467 | mapping = page_mapping(page); | |
468 | case PAGE_CLEAN: | |
469 | ; /* try to free the page below */ | |
470 | } | |
471 | } | |
472 | ||
473 | /* | |
474 | * If the page has buffers, try to free the buffer mappings | |
475 | * associated with this page. If we succeed we try to free | |
476 | * the page as well. | |
477 | * | |
478 | * We do this even if the page is PageDirty(). | |
479 | * try_to_release_page() does not perform I/O, but it is | |
480 | * possible for a page to have PageDirty set, but it is actually | |
481 | * clean (all its buffers are clean). This happens if the | |
482 | * buffers were written out directly, with submit_bh(). ext3 | |
483 | * will do this, as well as the blockdev mapping. | |
484 | * try_to_release_page() will discover that cleanness and will | |
485 | * drop the buffers and mark the page clean - it can be freed. | |
486 | * | |
487 | * Rarely, pages can have buffers and no ->mapping. These are | |
488 | * the pages which were not successfully invalidated in | |
489 | * truncate_complete_page(). We try to drop those buffers here | |
490 | * and if that worked, and the page is no longer mapped into | |
491 | * process address space (page_count == 1) it can be freed. | |
492 | * Otherwise, leave the page on the LRU so it is swappable. | |
493 | */ | |
494 | if (PagePrivate(page)) { | |
495 | if (!try_to_release_page(page, sc->gfp_mask)) | |
496 | goto activate_locked; | |
497 | if (!mapping && page_count(page) == 1) | |
498 | goto free_it; | |
499 | } | |
500 | ||
501 | if (!mapping) | |
502 | goto keep_locked; /* truncate got there first */ | |
503 | ||
504 | write_lock_irq(&mapping->tree_lock); | |
505 | ||
506 | /* | |
507 | * The non-racy check for busy page. It is critical to check | |
508 | * PageDirty _after_ making sure that the page is freeable and | |
509 | * not in use by anybody. (pagecache + us == 2) | |
510 | */ | |
511 | if (page_count(page) != 2 || PageDirty(page)) { | |
512 | write_unlock_irq(&mapping->tree_lock); | |
513 | goto keep_locked; | |
514 | } | |
515 | ||
516 | #ifdef CONFIG_SWAP | |
517 | if (PageSwapCache(page)) { | |
518 | swp_entry_t swap = { .val = page->private }; | |
519 | __delete_from_swap_cache(page); | |
520 | write_unlock_irq(&mapping->tree_lock); | |
521 | swap_free(swap); | |
522 | __put_page(page); /* The pagecache ref */ | |
523 | goto free_it; | |
524 | } | |
525 | #endif /* CONFIG_SWAP */ | |
526 | ||
527 | __remove_from_page_cache(page); | |
528 | write_unlock_irq(&mapping->tree_lock); | |
529 | __put_page(page); | |
530 | ||
531 | free_it: | |
532 | unlock_page(page); | |
533 | reclaimed++; | |
534 | if (!pagevec_add(&freed_pvec, page)) | |
535 | __pagevec_release_nonlru(&freed_pvec); | |
536 | continue; | |
537 | ||
538 | activate_locked: | |
539 | SetPageActive(page); | |
540 | pgactivate++; | |
541 | keep_locked: | |
542 | unlock_page(page); | |
543 | keep: | |
544 | list_add(&page->lru, &ret_pages); | |
545 | BUG_ON(PageLRU(page)); | |
546 | } | |
547 | list_splice(&ret_pages, page_list); | |
548 | if (pagevec_count(&freed_pvec)) | |
549 | __pagevec_release_nonlru(&freed_pvec); | |
550 | mod_page_state(pgactivate, pgactivate); | |
551 | sc->nr_reclaimed += reclaimed; | |
552 | return reclaimed; | |
553 | } | |
554 | ||
555 | /* | |
556 | * zone->lru_lock is heavily contended. Some of the functions that | |
557 | * shrink the lists perform better by taking out a batch of pages | |
558 | * and working on them outside the LRU lock. | |
559 | * | |
560 | * For pagecache intensive workloads, this function is the hottest | |
561 | * spot in the kernel (apart from copy_*_user functions). | |
562 | * | |
563 | * Appropriate locks must be held before calling this function. | |
564 | * | |
565 | * @nr_to_scan: The number of pages to look through on the list. | |
566 | * @src: The LRU list to pull pages off. | |
567 | * @dst: The temp list to put pages on to. | |
568 | * @scanned: The number of pages that were scanned. | |
569 | * | |
570 | * returns how many pages were moved onto *@dst. | |
571 | */ | |
572 | static int isolate_lru_pages(int nr_to_scan, struct list_head *src, | |
573 | struct list_head *dst, int *scanned) | |
574 | { | |
575 | int nr_taken = 0; | |
576 | struct page *page; | |
577 | int scan = 0; | |
578 | ||
579 | while (scan++ < nr_to_scan && !list_empty(src)) { | |
580 | page = lru_to_page(src); | |
581 | prefetchw_prev_lru_page(page, src, flags); | |
582 | ||
583 | if (!TestClearPageLRU(page)) | |
584 | BUG(); | |
585 | list_del(&page->lru); | |
586 | if (get_page_testone(page)) { | |
587 | /* | |
588 | * It is being freed elsewhere | |
589 | */ | |
590 | __put_page(page); | |
591 | SetPageLRU(page); | |
592 | list_add(&page->lru, src); | |
593 | continue; | |
594 | } else { | |
595 | list_add(&page->lru, dst); | |
596 | nr_taken++; | |
597 | } | |
598 | } | |
599 | ||
600 | *scanned = scan; | |
601 | return nr_taken; | |
602 | } | |
603 | ||
604 | /* | |
605 | * shrink_cache() adds the number of pages reclaimed to sc->nr_reclaimed | |
606 | */ | |
607 | static void shrink_cache(struct zone *zone, struct scan_control *sc) | |
608 | { | |
609 | LIST_HEAD(page_list); | |
610 | struct pagevec pvec; | |
611 | int max_scan = sc->nr_to_scan; | |
612 | ||
613 | pagevec_init(&pvec, 1); | |
614 | ||
615 | lru_add_drain(); | |
616 | spin_lock_irq(&zone->lru_lock); | |
617 | while (max_scan > 0) { | |
618 | struct page *page; | |
619 | int nr_taken; | |
620 | int nr_scan; | |
621 | int nr_freed; | |
622 | ||
623 | nr_taken = isolate_lru_pages(sc->swap_cluster_max, | |
624 | &zone->inactive_list, | |
625 | &page_list, &nr_scan); | |
626 | zone->nr_inactive -= nr_taken; | |
627 | zone->pages_scanned += nr_scan; | |
628 | spin_unlock_irq(&zone->lru_lock); | |
629 | ||
630 | if (nr_taken == 0) | |
631 | goto done; | |
632 | ||
633 | max_scan -= nr_scan; | |
634 | if (current_is_kswapd()) | |
635 | mod_page_state_zone(zone, pgscan_kswapd, nr_scan); | |
636 | else | |
637 | mod_page_state_zone(zone, pgscan_direct, nr_scan); | |
638 | nr_freed = shrink_list(&page_list, sc); | |
639 | if (current_is_kswapd()) | |
640 | mod_page_state(kswapd_steal, nr_freed); | |
641 | mod_page_state_zone(zone, pgsteal, nr_freed); | |
642 | sc->nr_to_reclaim -= nr_freed; | |
643 | ||
644 | spin_lock_irq(&zone->lru_lock); | |
645 | /* | |
646 | * Put back any unfreeable pages. | |
647 | */ | |
648 | while (!list_empty(&page_list)) { | |
649 | page = lru_to_page(&page_list); | |
650 | if (TestSetPageLRU(page)) | |
651 | BUG(); | |
652 | list_del(&page->lru); | |
653 | if (PageActive(page)) | |
654 | add_page_to_active_list(zone, page); | |
655 | else | |
656 | add_page_to_inactive_list(zone, page); | |
657 | if (!pagevec_add(&pvec, page)) { | |
658 | spin_unlock_irq(&zone->lru_lock); | |
659 | __pagevec_release(&pvec); | |
660 | spin_lock_irq(&zone->lru_lock); | |
661 | } | |
662 | } | |
663 | } | |
664 | spin_unlock_irq(&zone->lru_lock); | |
665 | done: | |
666 | pagevec_release(&pvec); | |
667 | } | |
668 | ||
669 | /* | |
670 | * This moves pages from the active list to the inactive list. | |
671 | * | |
672 | * We move them the other way if the page is referenced by one or more | |
673 | * processes, from rmap. | |
674 | * | |
675 | * If the pages are mostly unmapped, the processing is fast and it is | |
676 | * appropriate to hold zone->lru_lock across the whole operation. But if | |
677 | * the pages are mapped, the processing is slow (page_referenced()) so we | |
678 | * should drop zone->lru_lock around each page. It's impossible to balance | |
679 | * this, so instead we remove the pages from the LRU while processing them. | |
680 | * It is safe to rely on PG_active against the non-LRU pages in here because | |
681 | * nobody will play with that bit on a non-LRU page. | |
682 | * | |
683 | * The downside is that we have to touch page->_count against each page. | |
684 | * But we had to alter page->flags anyway. | |
685 | */ | |
686 | static void | |
687 | refill_inactive_zone(struct zone *zone, struct scan_control *sc) | |
688 | { | |
689 | int pgmoved; | |
690 | int pgdeactivate = 0; | |
691 | int pgscanned; | |
692 | int nr_pages = sc->nr_to_scan; | |
693 | LIST_HEAD(l_hold); /* The pages which were snipped off */ | |
694 | LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */ | |
695 | LIST_HEAD(l_active); /* Pages to go onto the active_list */ | |
696 | struct page *page; | |
697 | struct pagevec pvec; | |
698 | int reclaim_mapped = 0; | |
699 | long mapped_ratio; | |
700 | long distress; | |
701 | long swap_tendency; | |
702 | ||
703 | lru_add_drain(); | |
704 | spin_lock_irq(&zone->lru_lock); | |
705 | pgmoved = isolate_lru_pages(nr_pages, &zone->active_list, | |
706 | &l_hold, &pgscanned); | |
707 | zone->pages_scanned += pgscanned; | |
708 | zone->nr_active -= pgmoved; | |
709 | spin_unlock_irq(&zone->lru_lock); | |
710 | ||
711 | /* | |
712 | * `distress' is a measure of how much trouble we're having reclaiming | |
713 | * pages. 0 -> no problems. 100 -> great trouble. | |
714 | */ | |
715 | distress = 100 >> zone->prev_priority; | |
716 | ||
717 | /* | |
718 | * The point of this algorithm is to decide when to start reclaiming | |
719 | * mapped memory instead of just pagecache. Work out how much memory | |
720 | * is mapped. | |
721 | */ | |
722 | mapped_ratio = (sc->nr_mapped * 100) / total_memory; | |
723 | ||
724 | /* | |
725 | * Now decide how much we really want to unmap some pages. The mapped | |
726 | * ratio is downgraded - just because there's a lot of mapped memory | |
727 | * doesn't necessarily mean that page reclaim isn't succeeding. | |
728 | * | |
729 | * The distress ratio is important - we don't want to start going oom. | |
730 | * | |
731 | * A 100% value of vm_swappiness overrides this algorithm altogether. | |
732 | */ | |
733 | swap_tendency = mapped_ratio / 2 + distress + vm_swappiness; | |
734 | ||
735 | /* | |
736 | * Now use this metric to decide whether to start moving mapped memory | |
737 | * onto the inactive list. | |
738 | */ | |
739 | if (swap_tendency >= 100) | |
740 | reclaim_mapped = 1; | |
741 | ||
742 | while (!list_empty(&l_hold)) { | |
743 | cond_resched(); | |
744 | page = lru_to_page(&l_hold); | |
745 | list_del(&page->lru); | |
746 | if (page_mapped(page)) { | |
747 | if (!reclaim_mapped || | |
748 | (total_swap_pages == 0 && PageAnon(page)) || | |
749 | page_referenced(page, 0, sc->priority <= 0)) { | |
750 | list_add(&page->lru, &l_active); | |
751 | continue; | |
752 | } | |
753 | } | |
754 | list_add(&page->lru, &l_inactive); | |
755 | } | |
756 | ||
757 | pagevec_init(&pvec, 1); | |
758 | pgmoved = 0; | |
759 | spin_lock_irq(&zone->lru_lock); | |
760 | while (!list_empty(&l_inactive)) { | |
761 | page = lru_to_page(&l_inactive); | |
762 | prefetchw_prev_lru_page(page, &l_inactive, flags); | |
763 | if (TestSetPageLRU(page)) | |
764 | BUG(); | |
765 | if (!TestClearPageActive(page)) | |
766 | BUG(); | |
767 | list_move(&page->lru, &zone->inactive_list); | |
768 | pgmoved++; | |
769 | if (!pagevec_add(&pvec, page)) { | |
770 | zone->nr_inactive += pgmoved; | |
771 | spin_unlock_irq(&zone->lru_lock); | |
772 | pgdeactivate += pgmoved; | |
773 | pgmoved = 0; | |
774 | if (buffer_heads_over_limit) | |
775 | pagevec_strip(&pvec); | |
776 | __pagevec_release(&pvec); | |
777 | spin_lock_irq(&zone->lru_lock); | |
778 | } | |
779 | } | |
780 | zone->nr_inactive += pgmoved; | |
781 | pgdeactivate += pgmoved; | |
782 | if (buffer_heads_over_limit) { | |
783 | spin_unlock_irq(&zone->lru_lock); | |
784 | pagevec_strip(&pvec); | |
785 | spin_lock_irq(&zone->lru_lock); | |
786 | } | |
787 | ||
788 | pgmoved = 0; | |
789 | while (!list_empty(&l_active)) { | |
790 | page = lru_to_page(&l_active); | |
791 | prefetchw_prev_lru_page(page, &l_active, flags); | |
792 | if (TestSetPageLRU(page)) | |
793 | BUG(); | |
794 | BUG_ON(!PageActive(page)); | |
795 | list_move(&page->lru, &zone->active_list); | |
796 | pgmoved++; | |
797 | if (!pagevec_add(&pvec, page)) { | |
798 | zone->nr_active += pgmoved; | |
799 | pgmoved = 0; | |
800 | spin_unlock_irq(&zone->lru_lock); | |
801 | __pagevec_release(&pvec); | |
802 | spin_lock_irq(&zone->lru_lock); | |
803 | } | |
804 | } | |
805 | zone->nr_active += pgmoved; | |
806 | spin_unlock_irq(&zone->lru_lock); | |
807 | pagevec_release(&pvec); | |
808 | ||
809 | mod_page_state_zone(zone, pgrefill, pgscanned); | |
810 | mod_page_state(pgdeactivate, pgdeactivate); | |
811 | } | |
812 | ||
813 | /* | |
814 | * This is a basic per-zone page freer. Used by both kswapd and direct reclaim. | |
815 | */ | |
816 | static void | |
817 | shrink_zone(struct zone *zone, struct scan_control *sc) | |
818 | { | |
819 | unsigned long nr_active; | |
820 | unsigned long nr_inactive; | |
821 | ||
822 | /* | |
823 | * Add one to `nr_to_scan' just to make sure that the kernel will | |
824 | * slowly sift through the active list. | |
825 | */ | |
826 | zone->nr_scan_active += (zone->nr_active >> sc->priority) + 1; | |
827 | nr_active = zone->nr_scan_active; | |
828 | if (nr_active >= sc->swap_cluster_max) | |
829 | zone->nr_scan_active = 0; | |
830 | else | |
831 | nr_active = 0; | |
832 | ||
833 | zone->nr_scan_inactive += (zone->nr_inactive >> sc->priority) + 1; | |
834 | nr_inactive = zone->nr_scan_inactive; | |
835 | if (nr_inactive >= sc->swap_cluster_max) | |
836 | zone->nr_scan_inactive = 0; | |
837 | else | |
838 | nr_inactive = 0; | |
839 | ||
840 | sc->nr_to_reclaim = sc->swap_cluster_max; | |
841 | ||
842 | while (nr_active || nr_inactive) { | |
843 | if (nr_active) { | |
844 | sc->nr_to_scan = min(nr_active, | |
845 | (unsigned long)sc->swap_cluster_max); | |
846 | nr_active -= sc->nr_to_scan; | |
847 | refill_inactive_zone(zone, sc); | |
848 | } | |
849 | ||
850 | if (nr_inactive) { | |
851 | sc->nr_to_scan = min(nr_inactive, | |
852 | (unsigned long)sc->swap_cluster_max); | |
853 | nr_inactive -= sc->nr_to_scan; | |
854 | shrink_cache(zone, sc); | |
855 | if (sc->nr_to_reclaim <= 0) | |
856 | break; | |
857 | } | |
858 | } | |
859 | ||
860 | throttle_vm_writeout(); | |
861 | } | |
862 | ||
863 | /* | |
864 | * This is the direct reclaim path, for page-allocating processes. We only | |
865 | * try to reclaim pages from zones which will satisfy the caller's allocation | |
866 | * request. | |
867 | * | |
868 | * We reclaim from a zone even if that zone is over pages_high. Because: | |
869 | * a) The caller may be trying to free *extra* pages to satisfy a higher-order | |
870 | * allocation or | |
871 | * b) The zones may be over pages_high but they must go *over* pages_high to | |
872 | * satisfy the `incremental min' zone defense algorithm. | |
873 | * | |
874 | * Returns the number of reclaimed pages. | |
875 | * | |
876 | * If a zone is deemed to be full of pinned pages then just give it a light | |
877 | * scan then give up on it. | |
878 | */ | |
879 | static void | |
880 | shrink_caches(struct zone **zones, struct scan_control *sc) | |
881 | { | |
882 | int i; | |
883 | ||
884 | for (i = 0; zones[i] != NULL; i++) { | |
885 | struct zone *zone = zones[i]; | |
886 | ||
887 | if (zone->present_pages == 0) | |
888 | continue; | |
889 | ||
890 | if (!cpuset_zone_allowed(zone)) | |
891 | continue; | |
892 | ||
893 | zone->temp_priority = sc->priority; | |
894 | if (zone->prev_priority > sc->priority) | |
895 | zone->prev_priority = sc->priority; | |
896 | ||
897 | if (zone->all_unreclaimable && sc->priority != DEF_PRIORITY) | |
898 | continue; /* Let kswapd poll it */ | |
899 | ||
900 | shrink_zone(zone, sc); | |
901 | } | |
902 | } | |
903 | ||
904 | /* | |
905 | * This is the main entry point to direct page reclaim. | |
906 | * | |
907 | * If a full scan of the inactive list fails to free enough memory then we | |
908 | * are "out of memory" and something needs to be killed. | |
909 | * | |
910 | * If the caller is !__GFP_FS then the probability of a failure is reasonably | |
911 | * high - the zone may be full of dirty or under-writeback pages, which this | |
912 | * caller can't do much about. We kick pdflush and take explicit naps in the | |
913 | * hope that some of these pages can be written. But if the allocating task | |
914 | * holds filesystem locks which prevent writeout this might not work, and the | |
915 | * allocation attempt will fail. | |
916 | */ | |
917 | int try_to_free_pages(struct zone **zones, | |
918 | unsigned int gfp_mask, unsigned int order) | |
919 | { | |
920 | int priority; | |
921 | int ret = 0; | |
922 | int total_scanned = 0, total_reclaimed = 0; | |
923 | struct reclaim_state *reclaim_state = current->reclaim_state; | |
924 | struct scan_control sc; | |
925 | unsigned long lru_pages = 0; | |
926 | int i; | |
927 | ||
928 | sc.gfp_mask = gfp_mask; | |
929 | sc.may_writepage = 0; | |
930 | ||
931 | inc_page_state(allocstall); | |
932 | ||
933 | for (i = 0; zones[i] != NULL; i++) { | |
934 | struct zone *zone = zones[i]; | |
935 | ||
936 | if (!cpuset_zone_allowed(zone)) | |
937 | continue; | |
938 | ||
939 | zone->temp_priority = DEF_PRIORITY; | |
940 | lru_pages += zone->nr_active + zone->nr_inactive; | |
941 | } | |
942 | ||
943 | for (priority = DEF_PRIORITY; priority >= 0; priority--) { | |
944 | sc.nr_mapped = read_page_state(nr_mapped); | |
945 | sc.nr_scanned = 0; | |
946 | sc.nr_reclaimed = 0; | |
947 | sc.priority = priority; | |
948 | sc.swap_cluster_max = SWAP_CLUSTER_MAX; | |
949 | shrink_caches(zones, &sc); | |
950 | shrink_slab(sc.nr_scanned, gfp_mask, lru_pages); | |
951 | if (reclaim_state) { | |
952 | sc.nr_reclaimed += reclaim_state->reclaimed_slab; | |
953 | reclaim_state->reclaimed_slab = 0; | |
954 | } | |
955 | total_scanned += sc.nr_scanned; | |
956 | total_reclaimed += sc.nr_reclaimed; | |
957 | if (total_reclaimed >= sc.swap_cluster_max) { | |
958 | ret = 1; | |
959 | goto out; | |
960 | } | |
961 | ||
962 | /* | |
963 | * Try to write back as many pages as we just scanned. This | |
964 | * tends to cause slow streaming writers to write data to the | |
965 | * disk smoothly, at the dirtying rate, which is nice. But | |
966 | * that's undesirable in laptop mode, where we *want* lumpy | |
967 | * writeout. So in laptop mode, write out the whole world. | |
968 | */ | |
969 | if (total_scanned > sc.swap_cluster_max + sc.swap_cluster_max/2) { | |
970 | wakeup_bdflush(laptop_mode ? 0 : total_scanned); | |
971 | sc.may_writepage = 1; | |
972 | } | |
973 | ||
974 | /* Take a nap, wait for some writeback to complete */ | |
975 | if (sc.nr_scanned && priority < DEF_PRIORITY - 2) | |
976 | blk_congestion_wait(WRITE, HZ/10); | |
977 | } | |
978 | out: | |
979 | for (i = 0; zones[i] != 0; i++) { | |
980 | struct zone *zone = zones[i]; | |
981 | ||
982 | if (!cpuset_zone_allowed(zone)) | |
983 | continue; | |
984 | ||
985 | zone->prev_priority = zone->temp_priority; | |
986 | } | |
987 | return ret; | |
988 | } | |
989 | ||
990 | /* | |
991 | * For kswapd, balance_pgdat() will work across all this node's zones until | |
992 | * they are all at pages_high. | |
993 | * | |
994 | * If `nr_pages' is non-zero then it is the number of pages which are to be | |
995 | * reclaimed, regardless of the zone occupancies. This is a software suspend | |
996 | * special. | |
997 | * | |
998 | * Returns the number of pages which were actually freed. | |
999 | * | |
1000 | * There is special handling here for zones which are full of pinned pages. | |
1001 | * This can happen if the pages are all mlocked, or if they are all used by | |
1002 | * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb. | |
1003 | * What we do is to detect the case where all pages in the zone have been | |
1004 | * scanned twice and there has been zero successful reclaim. Mark the zone as | |
1005 | * dead and from now on, only perform a short scan. Basically we're polling | |
1006 | * the zone for when the problem goes away. | |
1007 | * | |
1008 | * kswapd scans the zones in the highmem->normal->dma direction. It skips | |
1009 | * zones which have free_pages > pages_high, but once a zone is found to have | |
1010 | * free_pages <= pages_high, we scan that zone and the lower zones regardless | |
1011 | * of the number of free pages in the lower zones. This interoperates with | |
1012 | * the page allocator fallback scheme to ensure that aging of pages is balanced | |
1013 | * across the zones. | |
1014 | */ | |
1015 | static int balance_pgdat(pg_data_t *pgdat, int nr_pages, int order) | |
1016 | { | |
1017 | int to_free = nr_pages; | |
1018 | int all_zones_ok; | |
1019 | int priority; | |
1020 | int i; | |
1021 | int total_scanned, total_reclaimed; | |
1022 | struct reclaim_state *reclaim_state = current->reclaim_state; | |
1023 | struct scan_control sc; | |
1024 | ||
1025 | loop_again: | |
1026 | total_scanned = 0; | |
1027 | total_reclaimed = 0; | |
1028 | sc.gfp_mask = GFP_KERNEL; | |
1029 | sc.may_writepage = 0; | |
1030 | sc.nr_mapped = read_page_state(nr_mapped); | |
1031 | ||
1032 | inc_page_state(pageoutrun); | |
1033 | ||
1034 | for (i = 0; i < pgdat->nr_zones; i++) { | |
1035 | struct zone *zone = pgdat->node_zones + i; | |
1036 | ||
1037 | zone->temp_priority = DEF_PRIORITY; | |
1038 | } | |
1039 | ||
1040 | for (priority = DEF_PRIORITY; priority >= 0; priority--) { | |
1041 | int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */ | |
1042 | unsigned long lru_pages = 0; | |
1043 | ||
1044 | all_zones_ok = 1; | |
1045 | ||
1046 | if (nr_pages == 0) { | |
1047 | /* | |
1048 | * Scan in the highmem->dma direction for the highest | |
1049 | * zone which needs scanning | |
1050 | */ | |
1051 | for (i = pgdat->nr_zones - 1; i >= 0; i--) { | |
1052 | struct zone *zone = pgdat->node_zones + i; | |
1053 | ||
1054 | if (zone->present_pages == 0) | |
1055 | continue; | |
1056 | ||
1057 | if (zone->all_unreclaimable && | |
1058 | priority != DEF_PRIORITY) | |
1059 | continue; | |
1060 | ||
1061 | if (!zone_watermark_ok(zone, order, | |
1062 | zone->pages_high, 0, 0, 0)) { | |
1063 | end_zone = i; | |
1064 | goto scan; | |
1065 | } | |
1066 | } | |
1067 | goto out; | |
1068 | } else { | |
1069 | end_zone = pgdat->nr_zones - 1; | |
1070 | } | |
1071 | scan: | |
1072 | for (i = 0; i <= end_zone; i++) { | |
1073 | struct zone *zone = pgdat->node_zones + i; | |
1074 | ||
1075 | lru_pages += zone->nr_active + zone->nr_inactive; | |
1076 | } | |
1077 | ||
1078 | /* | |
1079 | * Now scan the zone in the dma->highmem direction, stopping | |
1080 | * at the last zone which needs scanning. | |
1081 | * | |
1082 | * We do this because the page allocator works in the opposite | |
1083 | * direction. This prevents the page allocator from allocating | |
1084 | * pages behind kswapd's direction of progress, which would | |
1085 | * cause too much scanning of the lower zones. | |
1086 | */ | |
1087 | for (i = 0; i <= end_zone; i++) { | |
1088 | struct zone *zone = pgdat->node_zones + i; | |
b15e0905 | 1089 | int nr_slab; |
1da177e4 LT |
1090 | |
1091 | if (zone->present_pages == 0) | |
1092 | continue; | |
1093 | ||
1094 | if (zone->all_unreclaimable && priority != DEF_PRIORITY) | |
1095 | continue; | |
1096 | ||
1097 | if (nr_pages == 0) { /* Not software suspend */ | |
1098 | if (!zone_watermark_ok(zone, order, | |
1099 | zone->pages_high, end_zone, 0, 0)) | |
1100 | all_zones_ok = 0; | |
1101 | } | |
1102 | zone->temp_priority = priority; | |
1103 | if (zone->prev_priority > priority) | |
1104 | zone->prev_priority = priority; | |
1105 | sc.nr_scanned = 0; | |
1106 | sc.nr_reclaimed = 0; | |
1107 | sc.priority = priority; | |
1108 | sc.swap_cluster_max = nr_pages? nr_pages : SWAP_CLUSTER_MAX; | |
1109 | shrink_zone(zone, &sc); | |
1110 | reclaim_state->reclaimed_slab = 0; | |
b15e0905 | 1111 | nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL, |
1112 | lru_pages); | |
1da177e4 LT |
1113 | sc.nr_reclaimed += reclaim_state->reclaimed_slab; |
1114 | total_reclaimed += sc.nr_reclaimed; | |
1115 | total_scanned += sc.nr_scanned; | |
1116 | if (zone->all_unreclaimable) | |
1117 | continue; | |
b15e0905 | 1118 | if (nr_slab == 0 && zone->pages_scanned >= |
1119 | (zone->nr_active + zone->nr_inactive) * 4) | |
1da177e4 LT |
1120 | zone->all_unreclaimable = 1; |
1121 | /* | |
1122 | * If we've done a decent amount of scanning and | |
1123 | * the reclaim ratio is low, start doing writepage | |
1124 | * even in laptop mode | |
1125 | */ | |
1126 | if (total_scanned > SWAP_CLUSTER_MAX * 2 && | |
1127 | total_scanned > total_reclaimed+total_reclaimed/2) | |
1128 | sc.may_writepage = 1; | |
1129 | } | |
1130 | if (nr_pages && to_free > total_reclaimed) | |
1131 | continue; /* swsusp: need to do more work */ | |
1132 | if (all_zones_ok) | |
1133 | break; /* kswapd: all done */ | |
1134 | /* | |
1135 | * OK, kswapd is getting into trouble. Take a nap, then take | |
1136 | * another pass across the zones. | |
1137 | */ | |
1138 | if (total_scanned && priority < DEF_PRIORITY - 2) | |
1139 | blk_congestion_wait(WRITE, HZ/10); | |
1140 | ||
1141 | /* | |
1142 | * We do this so kswapd doesn't build up large priorities for | |
1143 | * example when it is freeing in parallel with allocators. It | |
1144 | * matches the direct reclaim path behaviour in terms of impact | |
1145 | * on zone->*_priority. | |
1146 | */ | |
1147 | if ((total_reclaimed >= SWAP_CLUSTER_MAX) && (!nr_pages)) | |
1148 | break; | |
1149 | } | |
1150 | out: | |
1151 | for (i = 0; i < pgdat->nr_zones; i++) { | |
1152 | struct zone *zone = pgdat->node_zones + i; | |
1153 | ||
1154 | zone->prev_priority = zone->temp_priority; | |
1155 | } | |
1156 | if (!all_zones_ok) { | |
1157 | cond_resched(); | |
1158 | goto loop_again; | |
1159 | } | |
1160 | ||
1161 | return total_reclaimed; | |
1162 | } | |
1163 | ||
1164 | /* | |
1165 | * The background pageout daemon, started as a kernel thread | |
1166 | * from the init process. | |
1167 | * | |
1168 | * This basically trickles out pages so that we have _some_ | |
1169 | * free memory available even if there is no other activity | |
1170 | * that frees anything up. This is needed for things like routing | |
1171 | * etc, where we otherwise might have all activity going on in | |
1172 | * asynchronous contexts that cannot page things out. | |
1173 | * | |
1174 | * If there are applications that are active memory-allocators | |
1175 | * (most normal use), this basically shouldn't matter. | |
1176 | */ | |
1177 | static int kswapd(void *p) | |
1178 | { | |
1179 | unsigned long order; | |
1180 | pg_data_t *pgdat = (pg_data_t*)p; | |
1181 | struct task_struct *tsk = current; | |
1182 | DEFINE_WAIT(wait); | |
1183 | struct reclaim_state reclaim_state = { | |
1184 | .reclaimed_slab = 0, | |
1185 | }; | |
1186 | cpumask_t cpumask; | |
1187 | ||
1188 | daemonize("kswapd%d", pgdat->node_id); | |
1189 | cpumask = node_to_cpumask(pgdat->node_id); | |
1190 | if (!cpus_empty(cpumask)) | |
1191 | set_cpus_allowed(tsk, cpumask); | |
1192 | current->reclaim_state = &reclaim_state; | |
1193 | ||
1194 | /* | |
1195 | * Tell the memory management that we're a "memory allocator", | |
1196 | * and that if we need more memory we should get access to it | |
1197 | * regardless (see "__alloc_pages()"). "kswapd" should | |
1198 | * never get caught in the normal page freeing logic. | |
1199 | * | |
1200 | * (Kswapd normally doesn't need memory anyway, but sometimes | |
1201 | * you need a small amount of memory in order to be able to | |
1202 | * page out something else, and this flag essentially protects | |
1203 | * us from recursively trying to free more memory as we're | |
1204 | * trying to free the first piece of memory in the first place). | |
1205 | */ | |
1206 | tsk->flags |= PF_MEMALLOC|PF_KSWAPD; | |
1207 | ||
1208 | order = 0; | |
1209 | for ( ; ; ) { | |
1210 | unsigned long new_order; | |
1211 | if (current->flags & PF_FREEZE) | |
1212 | refrigerator(PF_FREEZE); | |
1213 | ||
1214 | prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); | |
1215 | new_order = pgdat->kswapd_max_order; | |
1216 | pgdat->kswapd_max_order = 0; | |
1217 | if (order < new_order) { | |
1218 | /* | |
1219 | * Don't sleep if someone wants a larger 'order' | |
1220 | * allocation | |
1221 | */ | |
1222 | order = new_order; | |
1223 | } else { | |
1224 | schedule(); | |
1225 | order = pgdat->kswapd_max_order; | |
1226 | } | |
1227 | finish_wait(&pgdat->kswapd_wait, &wait); | |
1228 | ||
1229 | balance_pgdat(pgdat, 0, order); | |
1230 | } | |
1231 | return 0; | |
1232 | } | |
1233 | ||
1234 | /* | |
1235 | * A zone is low on free memory, so wake its kswapd task to service it. | |
1236 | */ | |
1237 | void wakeup_kswapd(struct zone *zone, int order) | |
1238 | { | |
1239 | pg_data_t *pgdat; | |
1240 | ||
1241 | if (zone->present_pages == 0) | |
1242 | return; | |
1243 | ||
1244 | pgdat = zone->zone_pgdat; | |
1245 | if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0, 0)) | |
1246 | return; | |
1247 | if (pgdat->kswapd_max_order < order) | |
1248 | pgdat->kswapd_max_order = order; | |
1249 | if (!cpuset_zone_allowed(zone)) | |
1250 | return; | |
1251 | if (!waitqueue_active(&zone->zone_pgdat->kswapd_wait)) | |
1252 | return; | |
1253 | wake_up_interruptible(&zone->zone_pgdat->kswapd_wait); | |
1254 | } | |
1255 | ||
1256 | #ifdef CONFIG_PM | |
1257 | /* | |
1258 | * Try to free `nr_pages' of memory, system-wide. Returns the number of freed | |
1259 | * pages. | |
1260 | */ | |
1261 | int shrink_all_memory(int nr_pages) | |
1262 | { | |
1263 | pg_data_t *pgdat; | |
1264 | int nr_to_free = nr_pages; | |
1265 | int ret = 0; | |
1266 | struct reclaim_state reclaim_state = { | |
1267 | .reclaimed_slab = 0, | |
1268 | }; | |
1269 | ||
1270 | current->reclaim_state = &reclaim_state; | |
1271 | for_each_pgdat(pgdat) { | |
1272 | int freed; | |
1273 | freed = balance_pgdat(pgdat, nr_to_free, 0); | |
1274 | ret += freed; | |
1275 | nr_to_free -= freed; | |
1276 | if (nr_to_free <= 0) | |
1277 | break; | |
1278 | } | |
1279 | current->reclaim_state = NULL; | |
1280 | return ret; | |
1281 | } | |
1282 | #endif | |
1283 | ||
1284 | #ifdef CONFIG_HOTPLUG_CPU | |
1285 | /* It's optimal to keep kswapds on the same CPUs as their memory, but | |
1286 | not required for correctness. So if the last cpu in a node goes | |
1287 | away, we get changed to run anywhere: as the first one comes back, | |
1288 | restore their cpu bindings. */ | |
1289 | static int __devinit cpu_callback(struct notifier_block *nfb, | |
1290 | unsigned long action, | |
1291 | void *hcpu) | |
1292 | { | |
1293 | pg_data_t *pgdat; | |
1294 | cpumask_t mask; | |
1295 | ||
1296 | if (action == CPU_ONLINE) { | |
1297 | for_each_pgdat(pgdat) { | |
1298 | mask = node_to_cpumask(pgdat->node_id); | |
1299 | if (any_online_cpu(mask) != NR_CPUS) | |
1300 | /* One of our CPUs online: restore mask */ | |
1301 | set_cpus_allowed(pgdat->kswapd, mask); | |
1302 | } | |
1303 | } | |
1304 | return NOTIFY_OK; | |
1305 | } | |
1306 | #endif /* CONFIG_HOTPLUG_CPU */ | |
1307 | ||
1308 | static int __init kswapd_init(void) | |
1309 | { | |
1310 | pg_data_t *pgdat; | |
1311 | swap_setup(); | |
1312 | for_each_pgdat(pgdat) | |
1313 | pgdat->kswapd | |
1314 | = find_task_by_pid(kernel_thread(kswapd, pgdat, CLONE_KERNEL)); | |
1315 | total_memory = nr_free_pagecache_pages(); | |
1316 | hotcpu_notifier(cpu_callback, 0); | |
1317 | return 0; | |
1318 | } | |
1319 | ||
1320 | module_init(kswapd_init) |