15 #include <linux/module.h>
42 #include <linux/sysctl.h>
43 #include <linux/oom.h>
44 #include <linux/prefetch.h>
46 #include <asm/tlbflush.h>
47 #include <asm/div64.h>
53 #define CREATE_TRACE_POINTS
97 #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
99 #ifdef ARCH_HAS_PREFETCH
100 #define prefetch_prev_lru_page(_page, _base, _field) \
102 if ((_page)->lru.prev != _base) { \
105 prev = lru_to_page(&(_page->lru)); \
106 prefetch(&prev->_field); \
110 #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
113 #ifdef ARCH_HAS_PREFETCHW
114 #define prefetchw_prev_lru_page(_page, _base, _field) \
116 if ((_page)->lru.prev != _base) { \
119 prev = lru_to_page(&(_page->lru)); \
120 prefetchw(&prev->_field); \
124 #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
150 if (!mem_cgroup_disabled())
153 return zone_page_state(lruvec_zone(lruvec),
NR_LRU_BASE + lru);
181 unsigned long nr_to_scan)
184 return (*shrinker->
shrink)(shrinker,
sc);
187 #define SHRINK_BATCH 128
208 unsigned long nr_pages_scanned,
209 unsigned long lru_pages)
211 struct shrinker *shrinker;
212 unsigned long ret = 0;
214 if (nr_pages_scanned == 0)
215 nr_pages_scanned = SWAP_CLUSTER_MAX;
224 unsigned long long delta;
230 long batch_size = shrinker->
batch ? shrinker->
batch
233 max_pass = do_shrinker_shrink(shrinker, shrink, 0);
245 delta = (4 * nr_pages_scanned) / shrinker->
seeks;
247 do_div(delta, lru_pages + 1);
249 if (total_scan < 0) {
252 shrinker->
shrink, total_scan);
253 total_scan = max_pass;
268 if (delta < max_pass / 4)
269 total_scan =
min(total_scan, max_pass / 2);
276 if (total_scan > max_pass * 2)
277 total_scan = max_pass * 2;
279 trace_mm_shrink_slab_start(shrinker, shrink, nr,
280 nr_pages_scanned, lru_pages,
281 max_pass, delta, total_scan);
283 while (total_scan >= batch_size) {
286 nr_before = do_shrinker_shrink(shrinker, shrink, 0);
287 shrink_ret = do_shrinker_shrink(shrinker, shrink,
289 if (shrink_ret == -1)
291 if (shrink_ret < nr_before)
292 ret += nr_before - shrink_ret;
294 total_scan -= batch_size;
305 new_nr = atomic_long_add_return(total_scan,
310 trace_mm_shrink_slab_end(shrinker, shrink_ret, nr, new_nr);
318 static inline int is_page_cache_freeable(
struct page *
page)
325 return page_count(page) - page_has_private(page) == 2;
333 if (!bdi_write_congested(bdi))
335 if (bdi ==
current->backing_dev_info)
356 if (page_mapping(page) == mapping)
357 mapping_set_error(mapping, error);
396 if (!is_page_cache_freeable(page))
403 if (page_has_private(page)) {
405 ClearPageDirty(page);
406 printk(
"%s: orphaned page\n", __func__);
421 .nr_to_write = SWAP_CLUSTER_MAX,
427 SetPageReclaim(page);
428 res = mapping->
a_ops->writepage(page, &wbc);
430 handle_write_error(mapping, page, res);
432 ClearPageReclaim(page);
436 if (!PageWriteback(page)) {
438 ClearPageReclaim(page);
452 static int __remove_mapping(
struct address_space *mapping,
struct page *page)
454 BUG_ON(!PageLocked(page));
455 BUG_ON(mapping != page_mapping(page));
483 if (!page_freeze_refs(page, 2))
487 page_unfreeze_refs(page, 2);
491 if (PageSwapCache(page)) {
497 void (*freepage)(
struct page *);
499 freepage = mapping->
a_ops->freepage;
505 if (freepage !=
NULL)
524 if (__remove_mapping(mapping, page)) {
530 page_unfreeze_refs(page, 1);
548 int active = !!TestClearPageActive(page);
549 int was_unevictable = PageUnevictable(page);
554 ClearPageUnevictable(page);
556 if (page_evictable(page)) {
563 lru = active + page_lru_base_type(page);
619 int referenced_ptes, referenced_page;
624 referenced_page = TestClearPageReferenced(page);
630 if (vm_flags & VM_LOCKED)
633 if (referenced_ptes) {
634 if (PageSwapBacked(page))
650 SetPageReferenced(page);
652 if (referenced_page || referenced_ptes > 1)
658 if (vm_flags & VM_EXEC)
665 if (referenced_page && !PageSwapBacked(page))
674 static unsigned long shrink_page_list(
struct list_head *page_list,
678 unsigned long *ret_nr_dirty,
679 unsigned long *ret_nr_writeback,
685 unsigned long nr_dirty = 0;
686 unsigned long nr_congested = 0;
687 unsigned long nr_reclaimed = 0;
688 unsigned long nr_writeback = 0;
693 while (!list_empty(page_list)) {
704 if (!trylock_page(page))
712 if (
unlikely(!page_evictable(page)))
719 if (page_mapped(page) || PageSwapCache(page))
725 if (PageWriteback(page)) {
743 if (global_reclaim(sc) ||
756 SetPageReclaim(page);
760 wait_on_page_writeback(page);
764 references = page_check_references(page, sc);
766 switch (references) {
768 goto activate_locked;
780 if (PageAnon(page) && !PageSwapCache(page)) {
784 goto activate_locked;
788 mapping = page_mapping(page);
794 if (page_mapped(page) && mapping) {
797 goto activate_locked;
807 if (PageDirty(page)) {
815 if (page_is_file_cache(page) &&
816 (!current_is_kswapd() ||
825 SetPageReclaim(page);
838 switch (pageout(page, mapping, sc)) {
843 goto activate_locked;
845 if (PageWriteback(page))
854 if (!trylock_page(page))
856 if (PageDirty(page) || PageWriteback(page))
858 mapping = page_mapping(page);
885 if (page_has_private(page)) {
887 goto activate_locked;
888 if (!mapping && page_count(page) == 1) {
890 if (put_page_testzero(page))
906 if (!mapping || !__remove_mapping(mapping, page))
916 __clear_page_locked(page);
928 if (PageSwapCache(page))
936 if (PageSwapCache(page) && vm_swap_full())
944 list_add(&page->
lru, &ret_pages);
945 VM_BUG_ON(PageLRU(page) || PageUnevictable(page));
954 if (nr_dirty && nr_dirty == nr_congested && global_reclaim(sc))
959 list_splice(&ret_pages, page_list);
962 *ret_nr_dirty += nr_dirty;
963 *ret_nr_writeback += nr_writeback;
975 unsigned long ret, dummy1, dummy2;
980 if (page_is_file_cache(page) && !PageDirty(page)) {
981 ClearPageActive(page);
982 list_move(&page->
lru, &clean_pages);
986 ret = shrink_page_list(&clean_pages, zone, &sc,
988 &dummy1, &dummy2,
true);
989 list_splice(&clean_pages, page_list);
1031 if (PageWriteback(page))
1034 if (PageDirty(page)) {
1046 mapping = page_mapping(page);
1047 if (mapping && !mapping->
a_ops->migratepage)
1055 if (
likely(get_page_unless_zero(page))) {
1088 static unsigned long isolate_lru_pages(
unsigned long nr_to_scan,
1094 unsigned long nr_taken = 0;
1097 for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
1110 list_move(&page->
lru, dst);
1111 nr_taken += nr_pages;
1116 list_move(&page->
lru, src);
1125 trace_mm_vmscan_lru_isolate(sc->
order, nr_to_scan, scan,
1126 nr_taken, mode, is_file_lru(lru));
1161 if (PageLRU(page)) {
1162 struct zone *zone = page_zone(page);
1163 struct lruvec *lruvec;
1167 if (PageLRU(page)) {
1168 int lru = page_lru(page);
1171 del_page_from_lru_list(page, lruvec, lru);
1182 static int too_many_isolated(
struct zone *zone,
int file,
1187 if (current_is_kswapd())
1190 if (!global_reclaim(sc))
1204 static noinline_for_stack
void
1205 putback_inactive_pages(
struct lruvec *lruvec,
struct list_head *page_list)
1208 struct zone *zone = lruvec_zone(lruvec);
1214 while (!list_empty(page_list)) {
1220 if (
unlikely(!page_evictable(page))) {
1230 lru = page_lru(page);
1231 add_page_to_lru_list(page, lruvec, lru);
1233 if (is_active_lru(lru)) {
1234 int file = is_file_lru(lru);
1238 if (put_page_testzero(page)) {
1239 __ClearPageLRU(page);
1240 __ClearPageActive(page);
1241 del_page_from_lru_list(page, lruvec, lru);
1243 if (
unlikely(PageCompound(page))) {
1245 (*get_compound_page_dtor(page))(page);
1248 list_add(&page->
lru, &pages_to_free);
1255 list_splice(&pages_to_free, page_list);
1262 static noinline_for_stack
unsigned long
1263 shrink_inactive_list(
unsigned long nr_to_scan,
struct lruvec *lruvec,
1267 unsigned long nr_scanned;
1268 unsigned long nr_reclaimed = 0;
1269 unsigned long nr_taken;
1270 unsigned long nr_dirty = 0;
1271 unsigned long nr_writeback = 0;
1273 int file = is_file_lru(lru);
1274 struct zone *zone = lruvec_zone(lruvec);
1277 while (
unlikely(too_many_isolated(zone, file, sc))) {
1281 if (fatal_signal_pending(
current))
1282 return SWAP_CLUSTER_MAX;
1294 nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list,
1295 &nr_scanned, sc, isolate_mode, lru);
1297 __mod_zone_page_state(zone,
NR_LRU_BASE + lru, -nr_taken);
1300 if (global_reclaim(sc)) {
1302 if (current_is_kswapd())
1312 nr_reclaimed = shrink_page_list(&page_list, zone, sc,
TTU_UNMAP,
1313 &nr_dirty, &nr_writeback,
false);
1319 if (global_reclaim(sc)) {
1320 if (current_is_kswapd())
1328 putback_inactive_pages(lruvec, &page_list);
1359 if (nr_writeback && nr_writeback >=
1363 trace_mm_vmscan_lru_shrink_inactive(zone->
zone_pgdat->node_id,
1365 nr_scanned, nr_reclaimed,
1368 return nr_reclaimed;
1389 static void move_active_pages_to_lru(
struct lruvec *lruvec,
1394 struct zone *zone = lruvec_zone(lruvec);
1395 unsigned long pgmoved = 0;
1399 while (!list_empty(list)) {
1408 list_move(&page->
lru, &lruvec->
lists[lru]);
1409 pgmoved += nr_pages;
1411 if (put_page_testzero(page)) {
1412 __ClearPageLRU(page);
1413 __ClearPageActive(page);
1414 del_page_from_lru_list(page, lruvec, lru);
1416 if (
unlikely(PageCompound(page))) {
1418 (*get_compound_page_dtor(page))(page);
1421 list_add(&page->
lru, pages_to_free);
1424 __mod_zone_page_state(zone,
NR_LRU_BASE + lru, pgmoved);
1425 if (!is_active_lru(lru))
1429 static void shrink_active_list(
unsigned long nr_to_scan,
1430 struct lruvec *lruvec,
1434 unsigned long nr_taken;
1435 unsigned long nr_scanned;
1442 unsigned long nr_rotated = 0;
1444 int file = is_file_lru(lru);
1445 struct zone *zone = lruvec_zone(lruvec);
1456 nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold,
1457 &nr_scanned, sc, isolate_mode, lru);
1458 if (global_reclaim(sc))
1464 __mod_zone_page_state(zone,
NR_LRU_BASE + lru, -nr_taken);
1468 while (!list_empty(&l_hold)) {
1473 if (
unlikely(!page_evictable(page))) {
1479 if (page_has_private(page) && trylock_page(page)) {
1480 if (page_has_private(page))
1498 if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) {
1499 list_add(&page->
lru, &l_active);
1504 ClearPageActive(page);
1505 list_add(&page->
lru, &l_inactive);
1520 move_active_pages_to_lru(lruvec, &l_active, &l_hold, lru);
1521 move_active_pages_to_lru(lruvec, &l_inactive, &l_hold, lru -
LRU_ACTIVE);
1529 static int inactive_anon_is_low_global(
struct zone *zone)
1549 static int inactive_anon_is_low(
struct lruvec *lruvec)
1558 if (!mem_cgroup_disabled())
1561 return inactive_anon_is_low_global(lruvec_zone(lruvec));
1564 static inline int inactive_anon_is_low(
struct lruvec *lruvec)
1570 static int inactive_file_is_low_global(
struct zone *zone)
1577 return (active > inactive);
1594 static int inactive_file_is_low(
struct lruvec *lruvec)
1596 if (!mem_cgroup_disabled())
1599 return inactive_file_is_low_global(lruvec_zone(lruvec));
1602 static int inactive_list_is_low(
struct lruvec *lruvec,
enum lru_list lru)
1604 if (is_file_lru(lru))
1605 return inactive_file_is_low(lruvec);
1607 return inactive_anon_is_low(lruvec);
1610 static unsigned long shrink_list(
enum lru_list lru,
unsigned long nr_to_scan,
1613 if (is_active_lru(lru)) {
1614 if (inactive_list_is_low(lruvec, lru))
1615 shrink_active_list(nr_to_scan, lruvec, sc, lru);
1619 return shrink_inactive_list(nr_to_scan, lruvec, sc, lru);
1624 if (global_reclaim(sc))
1638 static void get_scan_count(
struct lruvec *lruvec,
struct scan_control *sc,
1642 unsigned long anon_prio, file_prio;
1643 unsigned long ap,
fp;
1645 u64 fraction[2], denominator;
1648 bool force_scan =
false;
1649 struct zone *zone = lruvec_zone(lruvec);
1663 if (!global_reclaim(sc))
1680 if (global_reclaim(sc)) {
1696 anon_prio = vmscan_swappiness(sc);
1697 file_prio = 200 - anon_prio;
1735 denominator = ap + fp + 1;
1738 int file = is_file_lru(lru);
1741 scan = get_lru_size(lruvec, lru);
1742 if (sc->
priority || noswap || !vmscan_swappiness(sc)) {
1744 if (!scan && force_scan)
1745 scan = SWAP_CLUSTER_MAX;
1746 scan = div64_u64(scan * fraction[file], denominator);
1753 static bool in_reclaim_compaction(
struct scan_control *sc)
1770 static inline bool should_continue_reclaim(
struct lruvec *lruvec,
1771 unsigned long nr_reclaimed,
1772 unsigned long nr_scanned,
1775 unsigned long pages_for_compaction;
1776 unsigned long inactive_lru_pages;
1779 if (!in_reclaim_compaction(sc))
1790 if (!nr_reclaimed && !nr_scanned)
1809 pages_for_compaction = (2
UL << sc->
order);
1814 inactive_lru_pages > pages_for_compaction)
1818 switch (compaction_suitable(lruvec_zone(lruvec), sc->
order)) {
1830 static void shrink_lruvec(
struct lruvec *lruvec,
struct scan_control *sc)
1833 unsigned long nr_to_scan;
1835 unsigned long nr_reclaimed, nr_scanned;
1842 get_scan_count(lruvec, sc, nr);
1849 nr_to_scan =
min_t(
unsigned long,
1850 nr[lru], SWAP_CLUSTER_MAX);
1851 nr[
lru] -= nr_to_scan;
1853 nr_reclaimed += shrink_list(lru, nr_to_scan,
1865 if (nr_reclaimed >= nr_to_reclaim &&
1876 if (inactive_anon_is_low(lruvec))
1877 shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
1881 if (should_continue_reclaim(lruvec, nr_reclaimed,
1888 static void shrink_zone(
struct zone *zone,
struct scan_control *sc)
1901 shrink_lruvec(lruvec, sc);
1913 if (!global_reclaim(sc)) {
1922 static inline bool compaction_ready(
struct zone *zone,
struct scan_control *sc)
1924 unsigned long balance_gap, watermark;
1939 KSWAPD_ZONE_BALANCE_GAP_RATIO);
1947 if (compaction_deferred(zone, sc->
order))
1948 return watermark_ok;
1951 if (!compaction_suitable(zone, sc->
order))
1954 return watermark_ok;
1982 unsigned long nr_soft_reclaimed;
1983 unsigned long nr_soft_scanned;
1984 bool aborted_reclaim =
false;
1996 if (!populated_zone(zone))
2002 if (global_reclaim(sc)) {
2003 if (!cpuset_zone_allowed_hardwall(zone,
GFP_KERNEL))
2018 if (compaction_ready(zone, sc)) {
2019 aborted_reclaim =
true;
2029 nr_soft_scanned = 0;
2038 shrink_zone(zone, sc);
2041 return aborted_reclaim;
2044 static bool zone_reclaimable(
struct zone *zone)
2058 if (!populated_zone(zone))
2060 if (!cpuset_zone_allowed_hardwall(zone,
GFP_KERNEL))
2085 static unsigned long do_try_to_free_pages(
struct zonelist *zonelist,
2089 unsigned long total_scanned = 0;
2093 unsigned long writeback_threshold;
2094 bool aborted_reclaim;
2096 delayacct_freepages_start();
2098 if (global_reclaim(sc))
2103 aborted_reclaim = shrink_zones(zonelist, sc);
2109 if (global_reclaim(sc)) {
2110 unsigned long lru_pages = 0;
2113 if (!cpuset_zone_allowed_hardwall(zone,
GFP_KERNEL))
2120 if (reclaim_state) {
2137 if (total_scanned > writeback_threshold) {
2146 struct zone *preferred_zone;
2148 first_zones_zonelist(zonelist, gfp_zone(sc->
gfp_mask),
2156 delayacct_freepages_end();
2166 if (oom_killer_disabled)
2170 if (aborted_reclaim)
2180 static bool pfmemalloc_watermark_ok(
pg_data_t *pgdat)
2183 unsigned long pfmemalloc_reserve = 0;
2194 wmark_ok = free_pages > pfmemalloc_reserve / 2;
2197 if (!wmark_ok && waitqueue_active(&pgdat->
kswapd_wait)) {
2215 static bool throttle_direct_reclaim(
gfp_t gfp_mask,
struct zonelist *zonelist,
2219 int high_zoneidx = gfp_zone(gfp_mask);
2236 if (fatal_signal_pending(
current))
2240 first_zones_zonelist(zonelist, high_zoneidx,
NULL, &zone);
2242 if (pfmemalloc_watermark_ok(pgdat))
2258 pfmemalloc_watermark_ok(pgdat),
HZ);
2265 pfmemalloc_watermark_ok(pgdat));
2268 if (fatal_signal_pending(
current))
2278 unsigned long nr_reclaimed;
2282 .nr_to_reclaim = SWAP_CLUSTER_MAX,
2287 .target_mem_cgroup =
NULL,
2299 if (throttle_direct_reclaim(gfp_mask, zonelist, nodemask))
2302 trace_mm_vmscan_direct_reclaim_begin(order,
2306 nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink);
2308 trace_mm_vmscan_direct_reclaim_end(nr_reclaimed);
2310 return nr_reclaimed;
2315 unsigned long mem_cgroup_shrink_node_zone(
struct mem_cgroup *memcg,
2316 gfp_t gfp_mask,
bool noswap,
2318 unsigned long *nr_scanned)
2322 .nr_to_reclaim = SWAP_CLUSTER_MAX,
2325 .may_swap = !noswap,
2328 .target_mem_cgroup = memcg,
2335 trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.
order,
2346 shrink_lruvec(lruvec, &sc);
2348 trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.
nr_reclaimed);
2354 unsigned long try_to_free_mem_cgroup_pages(
struct mem_cgroup *memcg,
2358 struct zonelist *zonelist;
2359 unsigned long nr_reclaimed;
2364 .may_swap = !noswap,
2365 .nr_to_reclaim = SWAP_CLUSTER_MAX,
2368 .target_mem_cgroup = memcg,
2384 zonelist =
NODE_DATA(nid)->node_zonelists;
2386 trace_mm_vmscan_memcg_reclaim_begin(0,
2390 nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink);
2392 trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed);
2394 return nr_reclaimed;
2398 static void age_active_anon(
struct zone *zone,
struct scan_control *sc)
2409 if (inactive_anon_is_low(lruvec))
2410 shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
2417 static bool zone_balanced(
struct zone *zone,
int order,
2418 unsigned long balance_gap,
int classzone_idx)
2421 balance_gap, classzone_idx, 0))
2446 static bool pgdat_balanced(
pg_data_t *pgdat,
unsigned long balanced_pages,
2449 unsigned long present_pages = 0;
2452 for (i = 0; i <= classzone_idx; i++)
2453 present_pages += pgdat->
node_zones[i].present_pages;
2456 return balanced_pages >= (present_pages >> 2);
2465 static bool prepare_kswapd_sleep(
pg_data_t *pgdat,
int order,
long remaining,
2469 unsigned long balanced = 0;
2470 bool all_zones_ok =
true;
2491 for (i = 0; i <= classzone_idx; i++) {
2494 if (!populated_zone(zone))
2508 if (!zone_balanced(zone, order, 0, i))
2509 all_zones_ok =
false;
2520 return pgdat_balanced(pgdat, balanced, classzone_idx);
2522 return all_zones_ok;
2546 static unsigned long balance_pgdat(
pg_data_t *pgdat,
int order,
2550 unsigned long balanced;
2553 unsigned long total_scanned;
2554 struct reclaim_state *reclaim_state =
current->reclaim_state;
2555 unsigned long nr_soft_reclaimed;
2556 unsigned long nr_soft_scanned;
2567 .target_mem_cgroup =
NULL,
2580 unsigned long lru_pages = 0;
2581 int has_under_min_watermark_zone = 0;
2590 for (i = pgdat->
nr_zones - 1; i >= 0; i--) {
2593 if (!populated_zone(zone))
2604 age_active_anon(zone, &sc);
2617 if (!zone_balanced(zone, order, 0, 0)) {
2628 for (i = 0; i <= end_zone; i++) {
2643 for (i = 0; i <= end_zone; i++) {
2645 int nr_slab, testorder;
2646 unsigned long balance_gap;
2648 if (!populated_zone(zone))
2657 nr_soft_scanned = 0;
2665 total_scanned += nr_soft_scanned;
2677 KSWAPD_ZONE_BALANCE_GAP_RATIO-1) /
2678 KSWAPD_ZONE_BALANCE_GAP_RATIO);
2688 compaction_suitable(zone, order) !=
2693 !zone_balanced(zone, testorder,
2694 balance_gap, end_zone)) {
2695 shrink_zone(zone, &sc);
2702 if (nr_slab == 0 && !zone_reclaimable(zone))
2711 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
2716 if (end_zone && end_zone == i)
2721 if (!zone_balanced(zone, testorder, 0, end_zone)) {
2730 has_under_min_watermark_zone = 1;
2740 if (i <= *classzone_idx)
2752 pfmemalloc_watermark_ok(pgdat))
2755 if (all_zones_ok || (order && pgdat_balanced(pgdat, balanced, *classzone_idx)))
2762 if (has_under_min_watermark_zone)
2784 if (!(all_zones_ok || (order && pgdat_balanced(pgdat, balanced, *classzone_idx)))) {
2804 order = sc.
order = 0;
2818 int zones_need_compaction = 1;
2820 for (i = 0; i <= end_zone; i++) {
2823 if (!populated_zone(zone))
2829 zones_need_compaction = 0;
2832 if (zones_need_compaction)
2833 compact_pgdat(pgdat, order);
2842 *classzone_idx = end_zone;
2846 static void kswapd_try_to_sleep(
pg_data_t *pgdat,
int order,
int classzone_idx)
2857 if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) {
2867 if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) {
2868 trace_mm_vmscan_kswapd_sleep(pgdat->
node_id);
2886 reset_isolation_suitable(pgdat);
2914 static int kswapd(
void *
p)
2916 unsigned long order, new_order;
2917 unsigned balanced_order;
2918 int classzone_idx, new_classzone_idx;
2919 int balanced_classzone_idx;
2923 struct reclaim_state reclaim_state = {
2930 if (!cpumask_empty(cpumask))
2931 set_cpus_allowed_ptr(tsk, cpumask);
2932 current->reclaim_state = &reclaim_state;
2949 order = new_order = 0;
2951 classzone_idx = new_classzone_idx = pgdat->
nr_zones - 1;
2952 balanced_classzone_idx = classzone_idx;
2961 if (balanced_classzone_idx >= new_classzone_idx &&
2962 balanced_order == new_order) {
2969 if (order < new_order || classzone_idx > new_classzone_idx) {
2975 classzone_idx = new_classzone_idx;
2977 kswapd_try_to_sleep(pgdat, balanced_order,
2978 balanced_classzone_idx);
2982 new_classzone_idx = classzone_idx;
2987 ret = try_to_freeze();
2996 trace_mm_vmscan_kswapd_wake(pgdat->
node_id, order);
2997 balanced_classzone_idx = classzone_idx;
2998 balanced_order = balance_pgdat(pgdat, order,
2999 &balanced_classzone_idx);
3014 if (!populated_zone(zone))
3017 if (!cpuset_zone_allowed_hardwall(zone,
GFP_KERNEL))
3068 #ifdef CONFIG_HIBERNATION
3077 unsigned long shrink_all_memory(
unsigned long nr_to_reclaim)
3079 struct reclaim_state reclaim_state;
3086 .hibernation_mode = 1,
3095 unsigned long nr_reclaimed;
3102 nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink);
3108 return nr_reclaimed;
3117 unsigned long action,
void *hcpu)
3124 const struct cpumask *
mask;
3130 set_cpus_allowed_ptr(pgdat->
kswapd, mask);
3149 if (IS_ERR(pgdat->
kswapd)) {
3153 pr_err(
"Failed to start kswapd on node %d\n", nid);
3154 ret = PTR_ERR(pgdat->
kswapd);
3173 static int __init kswapd_init(
void)
3195 #define RECLAIM_OFF 0
3196 #define RECLAIM_ZONE (1<<0)
3197 #define RECLAIM_WRITE (1<<1)
3198 #define RECLAIM_SWAP (1<<2)
3205 #define ZONE_RECLAIM_PRIORITY 4
3211 int sysctl_min_unmapped_ratio = 1;
3217 int sysctl_min_slab_ratio = 5;
3219 static inline unsigned long zone_unmapped_file_pages(
struct zone *zone)
3221 unsigned long file_mapped = zone_page_state(zone,
NR_FILE_MAPPED);
3230 return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0;
3234 static long zone_pagecache_reclaimable(
struct zone *zone)
3236 long nr_pagecache_reclaimable;
3245 if (zone_reclaim_mode & RECLAIM_SWAP)
3246 nr_pagecache_reclaimable = zone_page_state(zone,
NR_FILE_PAGES);
3248 nr_pagecache_reclaimable = zone_unmapped_file_pages(zone);
3251 if (!(zone_reclaim_mode & RECLAIM_WRITE))
3255 if (
unlikely(delta > nr_pagecache_reclaimable))
3256 delta = nr_pagecache_reclaimable;
3258 return nr_pagecache_reclaimable -
delta;
3264 static int __zone_reclaim(
struct zone *zone,
gfp_t gfp_mask,
unsigned int order)
3267 const unsigned long nr_pages = 1 <<
order;
3269 struct reclaim_state reclaim_state;
3272 .
may_unmap = !!(zone_reclaim_mode & RECLAIM_SWAP),
3274 .nr_to_reclaim =
max_t(
unsigned long, nr_pages,
3278 .priority = ZONE_RECLAIM_PRIORITY,
3283 unsigned long nr_slab_pages0, nr_slab_pages1;
3296 if (zone_pagecache_reclaimable(zone) > zone->min_unmapped_pages) {
3302 shrink_zone(zone, &sc);
3307 if (nr_slab_pages0 > zone->min_slab_pages) {
3326 nr_slab_pages1 = zone_page_state(zone,
3328 if (nr_slab_pages1 + nr_pages <= nr_slab_pages0)
3337 if (nr_slab_pages1 < nr_slab_pages0)
3347 int zone_reclaim(
struct zone *zone,
gfp_t gfp_mask,
unsigned int order)
3362 if (zone_pagecache_reclaimable(zone) <= zone->min_unmapped_pages &&
3381 node_id = zone_to_nid(zone);
3388 ret = __zone_reclaim(zone, gfp_mask, order);
3392 count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED);
3410 int page_evictable(
struct page *page)
3412 return !mapping_unevictable(page_mapping(page)) && !PageMlocked(page);
3425 void check_move_unevictable_pages(
struct page **
pages,
int nr_pages)
3427 struct lruvec *lruvec;
3428 struct zone *zone =
NULL;
3433 for (i = 0; i < nr_pages; i++) {
3434 struct page *page = pages[
i];
3435 struct zone *pagezone;
3438 pagezone = page_zone(page);
3439 if (pagezone != zone) {
3447 if (!PageLRU(page) || !PageUnevictable(page))
3450 if (page_evictable(page)) {
3451 enum lru_list lru = page_lru_base_type(page);
3454 ClearPageUnevictable(page);
3456 add_page_to_lru_list(page, lruvec, lru);
3469 static void warn_scan_unevictable_pages(
void)
3472 "%s: The scan_unevictable_pages sysctl/node-interface has been "
3473 "disabled for lack of a legitimate use case. If you have "
3486 size_t *
length, loff_t *ppos)
3488 warn_scan_unevictable_pages();
3490 scan_unevictable_pages = 0;
3504 warn_scan_unevictable_pages();
3512 warn_scan_unevictable_pages();
3518 read_scan_unevictable_node,
3519 write_scan_unevictable_node);
3521 int scan_unevictable_register_node(
struct node *
node)
3526 void scan_unevictable_unregister_node(
struct node *
node)