Skip to content
Snippets Groups Projects
vmscan.c 57.9 KiB
Newer Older
  • Learn to ignore specific revisions
  • 	 */
    	if (scan_global_lru(sc))
    		mapped_ratio = ((global_page_state(NR_FILE_MAPPED) +
    				global_page_state(NR_ANON_PAGES)) * 100) /
    					vm_total_pages;
    	else
    		mapped_ratio = mem_cgroup_calc_mapped_ratio(sc->mem_cgroup);
    
    	/*
    	 * Now decide how much we really want to unmap some pages.  The
    	 * mapped ratio is downgraded - just because there's a lot of
    	 * mapped memory doesn't necessarily mean that page reclaim
    	 * isn't succeeding.
    	 *
    	 * The distress ratio is important - we don't want to start
    	 * going oom.
    	 *
    	 * A 100% value of vm_swappiness overrides this algorithm
    	 * altogether.
    	 */
    	swap_tendency = mapped_ratio / 2 + distress + sc->swappiness;
    
    	/*
    	 * If there's huge imbalance between active and inactive
    	 * (think active 100 times larger than inactive) we should
    	 * become more permissive, or the system will take too much
    	 * cpu before it start swapping during memory pressure.
    	 * Distress is about avoiding early-oom, this is about
    	 * making swappiness graceful despite setting it to low
    	 * values.
    	 *
    	 * Avoid div by zero with nr_inactive+1, and max resulting
    	 * value is vm_total_pages.
    	 */
    	if (scan_global_lru(sc)) {
    		imbalance  = zone_page_state(zone, NR_ACTIVE);
    		imbalance /= zone_page_state(zone, NR_INACTIVE) + 1;
    	} else
    		imbalance = mem_cgroup_reclaim_imbalance(sc->mem_cgroup);
    
    	/*
    	 * Reduce the effect of imbalance if swappiness is low,
    	 * this means for a swappiness very low, the imbalance
    	 * must be much higher than 100 for this logic to make
    	 * the difference.
    	 *
    	 * Max temporary value is vm_total_pages*100.
    	 */
    	imbalance *= (vm_swappiness + 1);
    	imbalance /= 100;
    
    	/*
    	 * If not much of the ram is mapped, makes the imbalance
    	 * less relevant, it's high priority we refill the inactive
    	 * list with mapped pages only in presence of high ratio of
    	 * mapped pages.
    	 *
    	 * Max temporary value is vm_total_pages*100.
    	 */
    	imbalance *= mapped_ratio;
    	imbalance /= 100;
    
    	/* apply imbalance feedback to swap_tendency */
    	swap_tendency += imbalance;
    
    	/*
    	 * Now use this metric to decide whether to start moving mapped
    	 * memory onto the inactive list.
    	 */
    	if (swap_tendency >= 100)
    		reclaim_mapped = 1;
    
    	return reclaim_mapped;
    }
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /*
     * This moves pages from the active list to the inactive list.
     *
     * We move them the other way if the page is referenced by one or more
     * processes, from rmap.
     *
     * If the pages are mostly unmapped, the processing is fast and it is
     * appropriate to hold zone->lru_lock across the whole operation.  But if
     * the pages are mapped, the processing is slow (page_referenced()) so we
     * should drop zone->lru_lock around each page.  It's impossible to balance
     * this, so instead we remove the pages from the LRU while processing them.
     * It is safe to rely on PG_active against the non-LRU pages in here because
     * nobody will play with that bit on a non-LRU page.
     *
     * The downside is that we have to touch page->_count against each page.
     * But we had to alter page->flags anyway.
     */
    
    static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
    
    				struct scan_control *sc, int priority)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	unsigned long pgmoved;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	int pgdeactivate = 0;
    
    	unsigned long pgscanned;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	LIST_HEAD(l_hold);	/* The pages which were snipped off */
    	LIST_HEAD(l_inactive);	/* Pages to go onto the inactive_list */
    	LIST_HEAD(l_active);	/* Pages to go onto the active_list */
    	struct page *page;
    	struct pagevec pvec;
    	int reclaim_mapped = 0;
    
    	if (sc->may_swap)
    		reclaim_mapped = calc_reclaim_mapped(sc, zone, priority);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	lru_add_drain();
    	spin_lock_irq(&zone->lru_lock);
    
    	pgmoved = sc->isolate_pages(nr_pages, &l_hold, &pgscanned, sc->order,
    					ISOLATE_ACTIVE, zone,
    					sc->mem_cgroup, 1);
    
    	/*
    	 * zone->pages_scanned is used for detect zone's oom
    	 * mem_cgroup remembers nr_scan by itself.
    	 */
    	if (scan_global_lru(sc))
    		zone->pages_scanned += pgscanned;
    
    
    	__mod_zone_page_state(zone, NR_ACTIVE, -pgmoved);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	spin_unlock_irq(&zone->lru_lock);
    
    	while (!list_empty(&l_hold)) {
    		cond_resched();
    		page = lru_to_page(&l_hold);
    		list_del(&page->lru);
    		if (page_mapped(page)) {
    			if (!reclaim_mapped ||
    			    (total_swap_pages == 0 && PageAnon(page)) ||
    
    			    page_referenced(page, 0, sc->mem_cgroup)) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				list_add(&page->lru, &l_active);
    				continue;
    			}
    		}
    		list_add(&page->lru, &l_inactive);
    	}
    
    	pagevec_init(&pvec, 1);
    	pgmoved = 0;
    	spin_lock_irq(&zone->lru_lock);
    	while (!list_empty(&l_inactive)) {
    		page = lru_to_page(&l_inactive);
    		prefetchw_prev_lru_page(page, &l_inactive, flags);
    
    Nicholas Piggin's avatar
    Nicholas Piggin committed
    		VM_BUG_ON(PageLRU(page));
    
    		SetPageLRU(page);
    
    Nicholas Piggin's avatar
    Nicholas Piggin committed
    		VM_BUG_ON(!PageActive(page));
    
    		ClearPageActive(page);
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		list_move(&page->lru, &zone->inactive_list);
    
    		mem_cgroup_move_lists(page, false);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		pgmoved++;
    		if (!pagevec_add(&pvec, page)) {
    
    			__mod_zone_page_state(zone, NR_INACTIVE, pgmoved);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			spin_unlock_irq(&zone->lru_lock);
    			pgdeactivate += pgmoved;
    			pgmoved = 0;
    			if (buffer_heads_over_limit)
    				pagevec_strip(&pvec);
    			__pagevec_release(&pvec);
    			spin_lock_irq(&zone->lru_lock);
    		}
    	}
    
    	__mod_zone_page_state(zone, NR_INACTIVE, pgmoved);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	pgdeactivate += pgmoved;
    	if (buffer_heads_over_limit) {
    		spin_unlock_irq(&zone->lru_lock);
    		pagevec_strip(&pvec);
    		spin_lock_irq(&zone->lru_lock);
    	}
    
    	pgmoved = 0;
    	while (!list_empty(&l_active)) {
    		page = lru_to_page(&l_active);
    		prefetchw_prev_lru_page(page, &l_active, flags);
    
    Nicholas Piggin's avatar
    Nicholas Piggin committed
    		VM_BUG_ON(PageLRU(page));
    
    		SetPageLRU(page);
    
    Nicholas Piggin's avatar
    Nicholas Piggin committed
    		VM_BUG_ON(!PageActive(page));
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		list_move(&page->lru, &zone->active_list);
    
    		mem_cgroup_move_lists(page, true);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		pgmoved++;
    		if (!pagevec_add(&pvec, page)) {
    
    			__mod_zone_page_state(zone, NR_ACTIVE, pgmoved);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			pgmoved = 0;
    			spin_unlock_irq(&zone->lru_lock);
    			__pagevec_release(&pvec);
    			spin_lock_irq(&zone->lru_lock);
    		}
    	}
    
    	__mod_zone_page_state(zone, NR_ACTIVE, pgmoved);
    
    	__count_zone_vm_events(PGREFILL, zone, pgscanned);
    	__count_vm_events(PGDEACTIVATE, pgdeactivate);
    	spin_unlock_irq(&zone->lru_lock);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	pagevec_release(&pvec);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    
    /*
     * This is a basic per-zone page freer.  Used by both kswapd and direct reclaim.
     */
    
    static unsigned long shrink_zone(int priority, struct zone *zone,
    				struct scan_control *sc)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	unsigned long nr_active;
    	unsigned long nr_inactive;
    
    	unsigned long nr_reclaimed = 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (scan_global_lru(sc)) {
    		/*
    		 * Add one to nr_to_scan just to make sure that the kernel
    		 * will slowly sift through the active list.
    		 */
    		zone->nr_scan_active +=
    			(zone_page_state(zone, NR_ACTIVE) >> priority) + 1;
    		nr_active = zone->nr_scan_active;
    		zone->nr_scan_inactive +=
    			(zone_page_state(zone, NR_INACTIVE) >> priority) + 1;
    		nr_inactive = zone->nr_scan_inactive;
    		if (nr_inactive >= sc->swap_cluster_max)
    			zone->nr_scan_inactive = 0;
    		else
    			nr_inactive = 0;
    
    		if (nr_active >= sc->swap_cluster_max)
    			zone->nr_scan_active = 0;
    		else
    			nr_active = 0;
    	} else {
    		/*
    		 * This reclaim occurs not because zone memory shortage but
    		 * because memory controller hits its limit.
    		 * Then, don't modify zone reclaim related data.
    		 */
    		nr_active = mem_cgroup_calc_reclaim_active(sc->mem_cgroup,
    					zone, priority);
    
    		nr_inactive = mem_cgroup_calc_reclaim_inactive(sc->mem_cgroup,
    					zone, priority);
    	}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	while (nr_active || nr_inactive) {
    		if (nr_active) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    					(unsigned long)sc->swap_cluster_max);
    
    			shrink_active_list(nr_to_scan, zone, sc, priority);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		}
    
    		if (nr_inactive) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    					(unsigned long)sc->swap_cluster_max);
    
    			nr_reclaimed += shrink_inactive_list(nr_to_scan, zone,
    								sc);
    
    	return nr_reclaimed;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    
    /*
     * This is the direct reclaim path, for page-allocating processes.  We only
     * try to reclaim pages from zones which will satisfy the caller's allocation
     * request.
     *
     * We reclaim from a zone even if that zone is over pages_high.  Because:
     * a) The caller may be trying to free *extra* pages to satisfy a higher-order
     *    allocation or
     * b) The zones may be over pages_high but they must go *over* pages_high to
     *    satisfy the `incremental min' zone defense algorithm.
     *
     * Returns the number of reclaimed pages.
     *
     * If a zone is deemed to be full of pinned pages then just give it a light
     * scan then give up on it.
     */
    
    static unsigned long shrink_zones(int priority, struct zonelist *zonelist,
    
    					struct scan_control *sc)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	enum zone_type high_zoneidx = gfp_zone(sc->gfp_mask);
    
    	unsigned long nr_reclaimed = 0;
    
    	sc->all_unreclaimable = 1;
    
    	for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
    
    		if (!populated_zone(zone))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			continue;
    
    		/*
    		 * Take care memory controller reclaiming has small influence
    		 * to global LRU.
    		 */
    		if (scan_global_lru(sc)) {
    			if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
    				continue;
    			note_zone_scanning_priority(zone, priority);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    			if (zone_is_all_unreclaimable(zone) &&
    						priority != DEF_PRIORITY)
    				continue;	/* Let kswapd poll it */
    			sc->all_unreclaimable = 0;
    		} else {
    			/*
    			 * Ignore cpuset limitation here. We just want to reduce
    			 * # of used pages by us regardless of memory shortage.
    			 */
    			sc->all_unreclaimable = 0;
    			mem_cgroup_note_reclaim_priority(sc->mem_cgroup,
    							priority);
    		}
    
    		nr_reclaimed += shrink_zone(priority, zone, sc);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    
    	return nr_reclaimed;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
     
    /*
     * This is the main entry point to direct page reclaim.
     *
     * If a full scan of the inactive list fails to free enough memory then we
     * are "out of memory" and something needs to be killed.
     *
     * If the caller is !__GFP_FS then the probability of a failure is reasonably
     * high - the zone may be full of dirty or under-writeback pages, which this
     * caller can't do much about.  We kick pdflush and take explicit naps in the
     * hope that some of these pages can be written.  But if the allocating task
     * holds filesystem locks which prevent writeout this might not work, and the
     * allocation attempt will fail.
    
     *
     * returns:	0, if no pages reclaimed
     * 		else, the number of pages reclaimed
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     */
    
    static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	int priority;
    
    	unsigned long total_scanned = 0;
    
    	unsigned long nr_reclaimed = 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	struct reclaim_state *reclaim_state = current->reclaim_state;
    	unsigned long lru_pages = 0;
    
    	enum zone_type high_zoneidx = gfp_zone(sc->gfp_mask);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	delayacct_freepages_start();
    
    
    	if (scan_global_lru(sc))
    		count_vm_event(ALLOCSTALL);
    	/*
    	 * mem_cgroup will not do shrink_slab.
    	 */
    	if (scan_global_lru(sc)) {
    
    		for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    			if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
    				continue;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    			lru_pages += zone_page_state(zone, NR_ACTIVE)
    					+ zone_page_state(zone, NR_INACTIVE);
    		}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    
    	for (priority = DEF_PRIORITY; priority >= 0; priority--) {
    
    		sc->nr_scanned = 0;
    
    		if (!priority)
    			disable_swap_token();
    
    		nr_reclaimed += shrink_zones(priority, zonelist, sc);
    
    		/*
    		 * Don't shrink slabs when reclaiming memory from
    		 * over limit cgroups
    		 */
    
    			shrink_slab(sc->nr_scanned, sc->gfp_mask, lru_pages);
    
    			if (reclaim_state) {
    				nr_reclaimed += reclaim_state->reclaimed_slab;
    				reclaim_state->reclaimed_slab = 0;
    			}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		}
    
    		total_scanned += sc->nr_scanned;
    		if (nr_reclaimed >= sc->swap_cluster_max) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			goto out;
    		}
    
    		/*
    		 * Try to write back as many pages as we just scanned.  This
    		 * tends to cause slow streaming writers to write data to the
    		 * disk smoothly, at the dirtying rate, which is nice.   But
    		 * that's undesirable in laptop mode, where we *want* lumpy
    		 * writeout.  So in laptop mode, write out the whole world.
    		 */
    
    		if (total_scanned > sc->swap_cluster_max +
    					sc->swap_cluster_max / 2) {
    
    			wakeup_pdflush(laptop_mode ? 0 : total_scanned);
    
    			sc->may_writepage = 1;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		}
    
    		/* Take a nap, wait for some writeback to complete */
    
    		if (sc->nr_scanned && priority < DEF_PRIORITY - 2)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    
    	/* top priority shrink_caches still had more to do? don't OOM, then */
    
    	if (!sc->all_unreclaimable && scan_global_lru(sc))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    out:
    
    	/*
    	 * Now that we've scanned all the zones at this priority level, note
    	 * that level within the zone so that the next thread which performs
    	 * scanning of this zone will immediately start out at this priority
    	 * level.  This affects only the decision whether or not to bring
    	 * mapped pages onto the inactive list.
    	 */
    	if (priority < 0)
    		priority = 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
    
    
    			if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
    				continue;
    
    			zone->prev_priority = priority;
    		}
    	} else
    		mem_cgroup_record_reclaim_priority(sc->mem_cgroup, priority);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return ret;
    }
    
    
    unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
    								gfp_t gfp_mask)
    
    {
    	struct scan_control sc = {
    		.gfp_mask = gfp_mask,
    		.may_writepage = !laptop_mode,
    		.swap_cluster_max = SWAP_CLUSTER_MAX,
    		.may_swap = 1,
    		.swappiness = vm_swappiness,
    		.order = order,
    		.mem_cgroup = NULL,
    		.isolate_pages = isolate_pages_global,
    	};
    
    
    	return do_try_to_free_pages(zonelist, &sc);
    
    #ifdef CONFIG_CGROUP_MEM_RES_CTLR
    
    unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *mem_cont,
    						gfp_t gfp_mask)
    
    {
    	struct scan_control sc = {
    		.may_writepage = !laptop_mode,
    		.may_swap = 1,
    		.swap_cluster_max = SWAP_CLUSTER_MAX,
    		.swappiness = vm_swappiness,
    		.order = 0,
    		.mem_cgroup = mem_cont,
    		.isolate_pages = mem_cgroup_isolate_pages,
    	};
    
    	sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
    			(GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
    	zonelist = NODE_DATA(numa_node_id())->node_zonelists;
    	return do_try_to_free_pages(zonelist, &sc);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /*
     * For kswapd, balance_pgdat() will work across all this node's zones until
     * they are all at pages_high.
     *
     * Returns the number of pages which were actually freed.
     *
     * There is special handling here for zones which are full of pinned pages.
     * This can happen if the pages are all mlocked, or if they are all used by
     * device drivers (say, ZONE_DMA).  Or if they are all in use by hugetlb.
     * What we do is to detect the case where all pages in the zone have been
     * scanned twice and there has been zero successful reclaim.  Mark the zone as
     * dead and from now on, only perform a short scan.  Basically we're polling
     * the zone for when the problem goes away.
     *
     * kswapd scans the zones in the highmem->normal->dma direction.  It skips
     * zones which have free_pages > pages_high, but once a zone is found to have
     * free_pages <= pages_high, we scan that zone and the lower zones regardless
     * of the number of free pages in the lower zones.  This interoperates with
     * the page allocator fallback scheme to ensure that aging of pages is balanced
     * across the zones.
     */
    
    static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	int all_zones_ok;
    	int priority;
    	int i;
    
    	unsigned long total_scanned;
    
    	unsigned long nr_reclaimed;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	struct reclaim_state *reclaim_state = current->reclaim_state;
    
    	struct scan_control sc = {
    		.gfp_mask = GFP_KERNEL,
    		.may_swap = 1,
    
    		.swap_cluster_max = SWAP_CLUSTER_MAX,
    		.swappiness = vm_swappiness,
    
    Andy Whitcroft's avatar
    Andy Whitcroft committed
    		.order = order,
    
    		.mem_cgroup = NULL,
    		.isolate_pages = isolate_pages_global,
    
    	/*
    	 * temp_priority is used to remember the scanning priority at which
    	 * this zone was successfully refilled to free_pages == pages_high.
    	 */
    	int temp_priority[MAX_NR_ZONES];
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    loop_again:
    	total_scanned = 0;
    
    	nr_reclaimed = 0;
    
    	sc.may_writepage = !laptop_mode;
    
    	count_vm_event(PAGEOUTRUN);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	for (i = 0; i < pgdat->nr_zones; i++)
    		temp_priority[i] = DEF_PRIORITY;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	for (priority = DEF_PRIORITY; priority >= 0; priority--) {
    		int end_zone = 0;	/* Inclusive.  0 = ZONE_DMA */
    		unsigned long lru_pages = 0;
    
    
    		/* The swap token gets in the way of swapout... */
    		if (!priority)
    			disable_swap_token();
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		all_zones_ok = 1;
    
    
    		/*
    		 * Scan in the highmem->dma direction for the highest
    		 * zone which needs scanning
    		 */
    		for (i = pgdat->nr_zones - 1; i >= 0; i--) {
    			struct zone *zone = pgdat->node_zones + i;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    			if (!populated_zone(zone))
    				continue;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    			if (zone_is_all_unreclaimable(zone) &&
    			    priority != DEF_PRIORITY)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    			if (!zone_watermark_ok(zone, order, zone->pages_high,
    					       0, 0)) {
    				end_zone = i;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			}
    		}
    
    		if (i < 0)
    			goto out;
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		for (i = 0; i <= end_zone; i++) {
    			struct zone *zone = pgdat->node_zones + i;
    
    
    			lru_pages += zone_page_state(zone, NR_ACTIVE)
    					+ zone_page_state(zone, NR_INACTIVE);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		}
    
    		/*
    		 * Now scan the zone in the dma->highmem direction, stopping
    		 * at the last zone which needs scanning.
    		 *
    		 * We do this because the page allocator works in the opposite
    		 * direction.  This prevents the page allocator from allocating
    		 * pages behind kswapd's direction of progress, which would
    		 * cause too much scanning of the lower zones.
    		 */
    		for (i = 0; i <= end_zone; i++) {
    			struct zone *zone = pgdat->node_zones + i;
    
    			int nr_slab;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    			if (!populated_zone(zone))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				continue;
    
    
    			if (zone_is_all_unreclaimable(zone) &&
    					priority != DEF_PRIORITY)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				continue;
    
    
    			if (!zone_watermark_ok(zone, order, zone->pages_high,
    					       end_zone, 0))
    				all_zones_ok = 0;
    
    			temp_priority[i] = priority;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			sc.nr_scanned = 0;
    
    			note_zone_scanning_priority(zone, priority);
    
    			/*
    			 * We put equal pressure on every zone, unless one
    			 * zone has way too many pages free already.
    			 */
    			if (!zone_watermark_ok(zone, order, 8*zone->pages_high,
    						end_zone, 0))
    				nr_reclaimed += shrink_zone(priority, zone, &sc);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			reclaim_state->reclaimed_slab = 0;
    
    			nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
    						lru_pages);
    
    			nr_reclaimed += reclaim_state->reclaimed_slab;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			total_scanned += sc.nr_scanned;
    
    			if (zone_is_all_unreclaimable(zone))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				continue;
    
    			if (nr_slab == 0 && zone->pages_scanned >=
    
    				(zone_page_state(zone, NR_ACTIVE)
    				+ zone_page_state(zone, NR_INACTIVE)) * 6)
    
    					zone_set_flag(zone,
    						      ZONE_ALL_UNRECLAIMABLE);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			/*
    			 * If we've done a decent amount of scanning and
    			 * the reclaim ratio is low, start doing writepage
    			 * even in laptop mode
    			 */
    			if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
    
    			    total_scanned > nr_reclaimed + nr_reclaimed / 2)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				sc.may_writepage = 1;
    		}
    		if (all_zones_ok)
    			break;		/* kswapd: all done */
    		/*
    		 * OK, kswapd is getting into trouble.  Take a nap, then take
    		 * another pass across the zones.
    		 */
    
    		if (total_scanned && priority < DEF_PRIORITY - 2)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    		/*
    		 * We do this so kswapd doesn't build up large priorities for
    		 * example when it is freeing in parallel with allocators. It
    		 * matches the direct reclaim path behaviour in terms of impact
    		 * on zone->*_priority.
    		 */
    
    		if (nr_reclaimed >= SWAP_CLUSTER_MAX)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			break;
    	}
    out:
    
    	/*
    	 * Note within each zone the priority level at which this zone was
    	 * brought into a happy state.  So that the next thread which scans this
    	 * zone will start out at that priority level.
    	 */
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	for (i = 0; i < pgdat->nr_zones; i++) {
    		struct zone *zone = pgdat->node_zones + i;
    
    
    		zone->prev_priority = temp_priority[i];
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    	if (!all_zones_ok) {
    		cond_resched();
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		goto loop_again;
    	}
    
    
    	return nr_reclaimed;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    
    /*
     * The background pageout daemon, started as a kernel thread
     * from the init process. 
     *
     * This basically trickles out pages so that we have _some_
     * free memory available even if there is no other activity
     * that frees anything up. This is needed for things like routing
     * etc, where we otherwise might have all activity going on in
     * asynchronous contexts that cannot page things out.
     *
     * If there are applications that are active memory-allocators
     * (most normal use), this basically shouldn't matter.
     */
    static int kswapd(void *p)
    {
    	unsigned long order;
    	pg_data_t *pgdat = (pg_data_t*)p;
    	struct task_struct *tsk = current;
    	DEFINE_WAIT(wait);
    	struct reclaim_state reclaim_state = {
    		.reclaimed_slab = 0,
    	};
    
    	node_to_cpumask_ptr(cpumask, pgdat->node_id);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (!cpus_empty(*cpumask))
    		set_cpus_allowed_ptr(tsk, cpumask);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	current->reclaim_state = &reclaim_state;
    
    	/*
    	 * Tell the memory management that we're a "memory allocator",
    	 * and that if we need more memory we should get access to it
    	 * regardless (see "__alloc_pages()"). "kswapd" should
    	 * never get caught in the normal page freeing logic.
    	 *
    	 * (Kswapd normally doesn't need memory anyway, but sometimes
    	 * you need a small amount of memory in order to be able to
    	 * page out something else, and this flag essentially protects
    	 * us from recursively trying to free more memory as we're
    	 * trying to free the first piece of memory in the first place).
    	 */
    
    	tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	order = 0;
    	for ( ; ; ) {
    		unsigned long new_order;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
    		new_order = pgdat->kswapd_max_order;
    		pgdat->kswapd_max_order = 0;
    		if (order < new_order) {
    			/*
    			 * Don't sleep if someone wants a larger 'order'
    			 * allocation
    			 */
    			order = new_order;
    		} else {
    
    			if (!freezing(current))
    				schedule();
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			order = pgdat->kswapd_max_order;
    		}
    		finish_wait(&pgdat->kswapd_wait, &wait);
    
    
    		if (!try_to_freeze()) {
    			/* We can speed up thawing tasks if we don't call
    			 * balance_pgdat after returning from the refrigerator
    			 */
    			balance_pgdat(pgdat, order);
    		}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    	return 0;
    }
    
    /*
     * A zone is low on free memory, so wake its kswapd task to service it.
     */
    void wakeup_kswapd(struct zone *zone, int order)
    {
    	pg_data_t *pgdat;
    
    
    	if (!populated_zone(zone))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return;
    
    	pgdat = zone->zone_pgdat;
    
    	if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return;
    	if (pgdat->kswapd_max_order < order)
    		pgdat->kswapd_max_order = order;
    
    	if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return;
    
    	if (!waitqueue_active(&pgdat->kswapd_wait))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return;
    
    	wake_up_interruptible(&pgdat->kswapd_wait);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    
    #ifdef CONFIG_PM
    /*
    
     * Helper function for shrink_all_memory().  Tries to reclaim 'nr_pages' pages
     * from LRU lists system-wide, for given pass and priority, and returns the
     * number of reclaimed pages
     *
     * For pass > 3 we also try to shrink the LRU lists that contain a few pages
     */
    
    static unsigned long shrink_all_zones(unsigned long nr_pages, int prio,
    				      int pass, struct scan_control *sc)
    
    {
    	struct zone *zone;
    	unsigned long nr_to_scan, ret = 0;
    
    	for_each_zone(zone) {
    
    		if (!populated_zone(zone))
    			continue;
    
    
    		if (zone_is_all_unreclaimable(zone) && prio != DEF_PRIORITY)
    
    			continue;
    
    		/* For pass = 0 we don't shrink the active list */
    		if (pass > 0) {
    
    			zone->nr_scan_active +=
    				(zone_page_state(zone, NR_ACTIVE) >> prio) + 1;
    
    			if (zone->nr_scan_active >= nr_pages || pass > 3) {
    				zone->nr_scan_active = 0;
    
    				nr_to_scan = min(nr_pages,
    					zone_page_state(zone, NR_ACTIVE));
    
    				shrink_active_list(nr_to_scan, zone, sc, prio);
    
    		zone->nr_scan_inactive +=
    			(zone_page_state(zone, NR_INACTIVE) >> prio) + 1;
    
    		if (zone->nr_scan_inactive >= nr_pages || pass > 3) {
    			zone->nr_scan_inactive = 0;
    
    			nr_to_scan = min(nr_pages,
    				zone_page_state(zone, NR_INACTIVE));
    
    			ret += shrink_inactive_list(nr_to_scan, zone, sc);
    			if (ret >= nr_pages)
    				return ret;
    		}
    	}
    
    	return ret;
    }
    
    
    static unsigned long count_lru_pages(void)
    {
    
    	return global_page_state(NR_ACTIVE) + global_page_state(NR_INACTIVE);
    
    /*
     * Try to free `nr_pages' of memory, system-wide, and return the number of
     * freed pages.
     *
     * Rather than trying to age LRUs the aim is to preserve the overall
     * LRU order by reclaiming preferentially
     * inactive > active > active referenced > active mapped
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     */
    
    unsigned long shrink_all_memory(unsigned long nr_pages)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	unsigned long lru_pages, nr_slab;
    
    	unsigned long ret = 0;
    
    	int pass;
    	struct reclaim_state reclaim_state;
    	struct scan_control sc = {
    		.gfp_mask = GFP_KERNEL,
    		.may_swap = 0,
    		.swap_cluster_max = nr_pages,
    		.may_writepage = 1,
    		.swappiness = vm_swappiness,
    
    		.isolate_pages = isolate_pages_global,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	};
    
    	current->reclaim_state = &reclaim_state;
    
    	lru_pages = count_lru_pages();
    
    	nr_slab = global_page_state(NR_SLAB_RECLAIMABLE);
    
    	/* If slab caches are huge, it's better to hit them first */
    	while (nr_slab >= lru_pages) {
    		reclaim_state.reclaimed_slab = 0;
    		shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
    		if (!reclaim_state.reclaimed_slab)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			break;
    
    
    		ret += reclaim_state.reclaimed_slab;
    		if (ret >= nr_pages)
    			goto out;
    
    		nr_slab -= reclaim_state.reclaimed_slab;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    
    
    	/*
    	 * We try to shrink LRUs in 5 passes:
    	 * 0 = Reclaim from inactive_list only
    	 * 1 = Reclaim from active list but don't reclaim mapped
    	 * 2 = 2nd pass of type 1
    	 * 3 = Reclaim mapped (normal reclaim)
    	 * 4 = 2nd pass of type 3
    	 */
    	for (pass = 0; pass < 5; pass++) {
    		int prio;
    
    		/* Force reclaiming mapped pages in the passes #3 and #4 */
    		if (pass > 2) {
    			sc.may_swap = 1;
    			sc.swappiness = 100;
    		}
    
    		for (prio = DEF_PRIORITY; prio >= 0; prio--) {
    			unsigned long nr_to_scan = nr_pages - ret;
    
    			sc.nr_scanned = 0;
    			ret += shrink_all_zones(nr_to_scan, prio, pass, &sc);
    			if (ret >= nr_pages)
    				goto out;
    
    			reclaim_state.reclaimed_slab = 0;
    
    			shrink_slab(sc.nr_scanned, sc.gfp_mask,
    					count_lru_pages());
    
    			ret += reclaim_state.reclaimed_slab;
    			if (ret >= nr_pages)
    				goto out;
    
    			if (sc.nr_scanned && prio < DEF_PRIORITY - 2)
    
    
    	/*
    	 * If ret = 0, we could not shrink LRUs, but there may be something
    	 * in slab caches
    	 */
    
    		do {
    			reclaim_state.reclaimed_slab = 0;
    
    			shrink_slab(nr_pages, sc.gfp_mask, count_lru_pages());
    
    			ret += reclaim_state.reclaimed_slab;
    		} while (ret < nr_pages && reclaim_state.reclaimed_slab > 0);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	current->reclaim_state = NULL;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return ret;
    }
    #endif
    
    /* It's optimal to keep kswapds on the same CPUs as their memory, but
       not required for correctness.  So if the last cpu in a node goes
       away, we get changed to run anywhere: as the first one comes back,
       restore their cpu bindings. */
    
    static int __devinit cpu_callback(struct notifier_block *nfb,
    
    				  unsigned long action, void *hcpu)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
    
    		for_each_node_state(nid, N_HIGH_MEMORY) {
    
    			pg_data_t *pgdat = NODE_DATA(nid);
    			node_to_cpumask_ptr(mask, pgdat->node_id);
    
    			if (any_online_cpu(*mask) < nr_cpu_ids)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				/* One of our CPUs online: restore mask */
    
    				set_cpus_allowed_ptr(pgdat->kswapd, mask);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		}
    	}
    	return NOTIFY_OK;
    }
    
    
    /*
     * This kswapd start function will be called by init and node-hot-add.
     * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
     */
    int kswapd_run(int nid)
    {
    	pg_data_t *pgdat = NODE_DATA(nid);
    	int ret = 0;
    
    	if (pgdat->kswapd)
    		return 0;
    
    	pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
    	if (IS_ERR(pgdat->kswapd)) {
    		/* failure at boot is fatal */
    		BUG_ON(system_state == SYSTEM_BOOTING);
    		printk("Failed to start kswapd on node %d\n",nid);
    		ret = -1;
    	}
    	return ret;
    }
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    static int __init kswapd_init(void)
    {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	swap_setup();
    
    	for_each_node_state(nid, N_HIGH_MEMORY)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	hotcpu_notifier(cpu_callback, 0);
    	return 0;
    }
    
    module_init(kswapd_init)
    
    
    #ifdef CONFIG_NUMA
    /*
     * Zone reclaim mode
     *
     * If non-zero call zone_reclaim when the number of free pages falls below
     * the watermarks.
     */
    int zone_reclaim_mode __read_mostly;
    
    
    #define RECLAIM_OFF 0
    #define RECLAIM_ZONE (1<<0)	/* Run shrink_cache on the zone */
    #define RECLAIM_WRITE (1<<1)	/* Writeout pages during reclaim */
    #define RECLAIM_SWAP (1<<2)	/* Swap pages out during reclaim */
    
    
    /*
     * Priority for ZONE_RECLAIM. This determines the fraction of pages
     * of a node considered for each zone_reclaim. 4 scans 1/16th of
     * a zone.
     */
    #define ZONE_RECLAIM_PRIORITY 4
    
    
    /*
     * Percentage of pages in a zone that must be unmapped for zone_reclaim to
     * occur.
     */
    int sysctl_min_unmapped_ratio = 1;
    
    
    /*
     * If the number of slab pages in a zone grows beyond this percentage then