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