4 * Copyright (C) 2013 Red Hat, Inc., Johannes Weiner
7 #include <linux/memcontrol.h>
8 #include <linux/writeback.h>
9 #include <linux/pagemap.h>
10 #include <linux/atomic.h>
11 #include <linux/module.h>
12 #include <linux/swap.h>
19 * Per zone, two clock lists are maintained for file pages: the
20 * inactive and the active list. Freshly faulted pages start out at
21 * the head of the inactive list and page reclaim scans pages from the
22 * tail. Pages that are accessed multiple times on the inactive list
23 * are promoted to the active list, to protect them from reclaim,
24 * whereas active pages are demoted to the inactive list when the
25 * active list grows too big.
27 * fault ------------------------+
29 * +--------------+ | +-------------+
30 * reclaim <- | inactive | <-+-- demotion | active | <--+
31 * +--------------+ +-------------+ |
33 * +-------------- promotion ------------------+
36 * Access frequency and refault distance
38 * A workload is thrashing when its pages are frequently used but they
39 * are evicted from the inactive list every time before another access
40 * would have promoted them to the active list.
42 * In cases where the average access distance between thrashing pages
43 * is bigger than the size of memory there is nothing that can be
44 * done - the thrashing set could never fit into memory under any
47 * However, the average access distance could be bigger than the
48 * inactive list, yet smaller than the size of memory. In this case,
49 * the set could fit into memory if it weren't for the currently
50 * active pages - which may be used more, hopefully less frequently:
52 * +-memory available to cache-+
54 * +-inactive------+-active----+
55 * a b | c d e f g h i | J K L M N |
56 * +---------------+-----------+
58 * It is prohibitively expensive to accurately track access frequency
59 * of pages. But a reasonable approximation can be made to measure
60 * thrashing on the inactive list, after which refaulting pages can be
61 * activated optimistically to compete with the existing active pages.
63 * Approximating inactive page access frequency - Observations:
65 * 1. When a page is accessed for the first time, it is added to the
66 * head of the inactive list, slides every existing inactive page
67 * towards the tail by one slot, and pushes the current tail page
70 * 2. When a page is accessed for the second time, it is promoted to
71 * the active list, shrinking the inactive list by one slot. This
72 * also slides all inactive pages that were faulted into the cache
73 * more recently than the activated page towards the tail of the
78 * 1. The sum of evictions and activations between any two points in
79 * time indicate the minimum number of inactive pages accessed in
82 * 2. Moving one inactive page N page slots towards the tail of the
83 * list requires at least N inactive page accesses.
87 * 1. When a page is finally evicted from memory, the number of
88 * inactive pages accessed while the page was in cache is at least
89 * the number of page slots on the inactive list.
91 * 2. In addition, measuring the sum of evictions and activations (E)
92 * at the time of a page's eviction, and comparing it to another
93 * reading (R) at the time the page faults back into memory tells
94 * the minimum number of accesses while the page was not cached.
95 * This is called the refault distance.
97 * Because the first access of the page was the fault and the second
98 * access the refault, we combine the in-cache distance with the
99 * out-of-cache distance to get the complete minimum access distance
102 * NR_inactive + (R - E)
104 * And knowing the minimum access distance of a page, we can easily
105 * tell if the page would be able to stay in cache assuming all page
106 * slots in the cache were available:
108 * NR_inactive + (R - E) <= NR_inactive + NR_active
110 * which can be further simplified to
112 * (R - E) <= NR_active
114 * Put into words, the refault distance (out-of-cache) can be seen as
115 * a deficit in inactive list space (in-cache). If the inactive list
116 * had (R - E) more page slots, the page would not have been evicted
117 * in between accesses, but activated instead. And on a full system,
118 * the only thing eating into inactive list space is active pages.
121 * Activating refaulting pages
123 * All that is known about the active list is that the pages have been
124 * accessed more than once in the past. This means that at any given
125 * time there is actually a good chance that pages on the active list
126 * are no longer in active use.
128 * So when a refault distance of (R - E) is observed and there are at
129 * least (R - E) active pages, the refaulting page is activated
130 * optimistically in the hope that (R - E) active pages are actually
131 * used less frequently than the refaulting page - or even not used at
134 * If this is wrong and demotion kicks in, the pages which are truly
135 * used more frequently will be reactivated while the less frequently
136 * used once will be evicted from memory.
138 * But if this is right, the stale pages will be pushed out of memory
139 * and the used pages get to stay in cache.
144 * For each zone's file LRU lists, a counter for inactive evictions
145 * and activations is maintained (zone->inactive_age).
147 * On eviction, a snapshot of this counter (along with some bits to
148 * identify the zone) is stored in the now empty page cache radix tree
149 * slot of the evicted page. This is called a shadow entry.
151 * On cache misses for which there are shadow entries, an eligible
152 * refault distance will immediately activate the refaulting page.
155 static void *pack_shadow(unsigned long eviction, struct zone *zone)
157 eviction = (eviction << NODES_SHIFT) | zone_to_nid(zone);
158 eviction = (eviction << ZONES_SHIFT) | zone_idx(zone);
159 eviction = (eviction << RADIX_TREE_EXCEPTIONAL_SHIFT);
161 return (void *)(eviction | RADIX_TREE_EXCEPTIONAL_ENTRY);
164 static void unpack_shadow(void *shadow,
166 unsigned long *distance)
168 unsigned long entry = (unsigned long)shadow;
169 unsigned long eviction;
170 unsigned long refault;
174 entry >>= RADIX_TREE_EXCEPTIONAL_SHIFT;
175 zid = entry & ((1UL << ZONES_SHIFT) - 1);
176 entry >>= ZONES_SHIFT;
177 nid = entry & ((1UL << NODES_SHIFT) - 1);
178 entry >>= NODES_SHIFT;
181 *zone = NODE_DATA(nid)->node_zones + zid;
183 refault = atomic_long_read(&(*zone)->inactive_age);
184 mask = ~0UL >> (NODES_SHIFT + ZONES_SHIFT +
185 RADIX_TREE_EXCEPTIONAL_SHIFT);
187 * The unsigned subtraction here gives an accurate distance
188 * across inactive_age overflows in most cases.
190 * There is a special case: usually, shadow entries have a
191 * short lifetime and are either refaulted or reclaimed along
192 * with the inode before they get too old. But it is not
193 * impossible for the inactive_age to lap a shadow entry in
194 * the field, which can then can result in a false small
195 * refault distance, leading to a false activation should this
196 * old entry actually refault again. However, earlier kernels
197 * used to deactivate unconditionally with *every* reclaim
198 * invocation for the longest time, so the occasional
199 * inappropriate activation leading to pressure on the active
200 * list is not a problem.
202 *distance = (refault - eviction) & mask;
206 * workingset_eviction - note the eviction of a page from memory
207 * @mapping: address space the page was backing
208 * @page: the page being evicted
210 * Returns a shadow entry to be stored in @mapping->page_tree in place
211 * of the evicted @page so that a later refault can be detected.
213 void *workingset_eviction(struct address_space *mapping, struct page *page)
215 struct zone *zone = page_zone(page);
216 unsigned long eviction;
218 eviction = atomic_long_inc_return(&zone->inactive_age);
219 return pack_shadow(eviction, zone);
223 * workingset_refault - evaluate the refault of a previously evicted page
224 * @shadow: shadow entry of the evicted page
226 * Calculates and evaluates the refault distance of the previously
227 * evicted page in the context of the zone it was allocated in.
229 * Returns %true if the page should be activated, %false otherwise.
231 bool workingset_refault(void *shadow)
233 unsigned long refault_distance;
236 unpack_shadow(shadow, &zone, &refault_distance);
237 inc_zone_state(zone, WORKINGSET_REFAULT);
239 if (refault_distance <= zone_page_state(zone, NR_ACTIVE_FILE)) {
240 inc_zone_state(zone, WORKINGSET_ACTIVATE);
247 * workingset_activation - note a page activation
248 * @page: page that is being activated
250 void workingset_activation(struct page *page)
252 atomic_long_inc(&page_zone(page)->inactive_age);