blob: f874b2c663e389ad190b052c8f2acf7509f4a643 [file] [log] [blame]
Johannes Weinera5289102014-04-03 14:47:51 -07001/*
2 * Workingset detection
3 *
4 * Copyright (C) 2013 Red Hat, Inc., Johannes Weiner
5 */
6
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>
13#include <linux/fs.h>
14#include <linux/mm.h>
15
16/*
17 * Double CLOCK lists
18 *
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.
26 *
27 * fault ------------------------+
28 * |
29 * +--------------+ | +-------------+
30 * reclaim <- | inactive | <-+-- demotion | active | <--+
31 * +--------------+ +-------------+ |
32 * | |
33 * +-------------- promotion ------------------+
34 *
35 *
36 * Access frequency and refault distance
37 *
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.
41 *
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
45 * circumstance.
46 *
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:
51 *
52 * +-memory available to cache-+
53 * | |
54 * +-inactive------+-active----+
55 * a b | c d e f g h i | J K L M N |
56 * +---------------+-----------+
57 *
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.
62 *
63 * Approximating inactive page access frequency - Observations:
64 *
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
68 * out of memory.
69 *
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
74 * inactive list.
75 *
76 * Thus:
77 *
78 * 1. The sum of evictions and activations between any two points in
79 * time indicate the minimum number of inactive pages accessed in
80 * between.
81 *
82 * 2. Moving one inactive page N page slots towards the tail of the
83 * list requires at least N inactive page accesses.
84 *
85 * Combining these:
86 *
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.
90 *
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.
96 *
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
100 * of this page:
101 *
102 * NR_inactive + (R - E)
103 *
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:
107 *
108 * NR_inactive + (R - E) <= NR_inactive + NR_active
109 *
110 * which can be further simplified to
111 *
112 * (R - E) <= NR_active
113 *
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.
119 *
120 *
121 * Activating refaulting pages
122 *
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.
127 *
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
132 * all anymore.
133 *
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.
137 *
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.
140 *
141 *
142 * Implementation
143 *
144 * For each zone's file LRU lists, a counter for inactive evictions
145 * and activations is maintained (zone->inactive_age).
146 *
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.
150 *
151 * On cache misses for which there are shadow entries, an eligible
152 * refault distance will immediately activate the refaulting page.
153 */
154
Johannes Weiner689c94f2016-03-15 14:57:07 -0700155#define EVICTION_SHIFT (RADIX_TREE_EXCEPTIONAL_ENTRY + \
156 ZONES_SHIFT + NODES_SHIFT)
157#define EVICTION_MASK (~0UL >> EVICTION_SHIFT)
158
Johannes Weinera5289102014-04-03 14:47:51 -0700159static void *pack_shadow(unsigned long eviction, struct zone *zone)
160{
161 eviction = (eviction << NODES_SHIFT) | zone_to_nid(zone);
162 eviction = (eviction << ZONES_SHIFT) | zone_idx(zone);
163 eviction = (eviction << RADIX_TREE_EXCEPTIONAL_SHIFT);
164
165 return (void *)(eviction | RADIX_TREE_EXCEPTIONAL_ENTRY);
166}
167
Johannes Weiner162453b2016-03-15 14:57:10 -0700168static void unpack_shadow(void *shadow, struct zone **zonep,
169 unsigned long *evictionp)
Johannes Weinera5289102014-04-03 14:47:51 -0700170{
171 unsigned long entry = (unsigned long)shadow;
Johannes Weinera5289102014-04-03 14:47:51 -0700172 int zid, nid;
173
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;
Johannes Weinera5289102014-04-03 14:47:51 -0700179
Johannes Weiner162453b2016-03-15 14:57:10 -0700180 *zonep = NODE_DATA(nid)->node_zones + zid;
181 *evictionp = entry;
Johannes Weinera5289102014-04-03 14:47:51 -0700182}
183
184/**
185 * workingset_eviction - note the eviction of a page from memory
186 * @mapping: address space the page was backing
187 * @page: the page being evicted
188 *
189 * Returns a shadow entry to be stored in @mapping->page_tree in place
190 * of the evicted @page so that a later refault can be detected.
191 */
192void *workingset_eviction(struct address_space *mapping, struct page *page)
193{
194 struct zone *zone = page_zone(page);
195 unsigned long eviction;
196
197 eviction = atomic_long_inc_return(&zone->inactive_age);
198 return pack_shadow(eviction, zone);
199}
200
201/**
202 * workingset_refault - evaluate the refault of a previously evicted page
203 * @shadow: shadow entry of the evicted page
204 *
205 * Calculates and evaluates the refault distance of the previously
206 * evicted page in the context of the zone it was allocated in.
207 *
208 * Returns %true if the page should be activated, %false otherwise.
209 */
210bool workingset_refault(void *shadow)
211{
212 unsigned long refault_distance;
Johannes Weiner162453b2016-03-15 14:57:10 -0700213 unsigned long eviction;
214 unsigned long refault;
Johannes Weinera5289102014-04-03 14:47:51 -0700215 struct zone *zone;
216
Johannes Weiner162453b2016-03-15 14:57:10 -0700217 unpack_shadow(shadow, &zone, &eviction);
218
219 refault = atomic_long_read(&zone->inactive_age);
220
221 /*
222 * The unsigned subtraction here gives an accurate distance
223 * across inactive_age overflows in most cases.
224 *
225 * There is a special case: usually, shadow entries have a
226 * short lifetime and are either refaulted or reclaimed along
227 * with the inode before they get too old. But it is not
228 * impossible for the inactive_age to lap a shadow entry in
229 * the field, which can then can result in a false small
230 * refault distance, leading to a false activation should this
231 * old entry actually refault again. However, earlier kernels
232 * used to deactivate unconditionally with *every* reclaim
233 * invocation for the longest time, so the occasional
234 * inappropriate activation leading to pressure on the active
235 * list is not a problem.
236 */
237 refault_distance = (refault - eviction) & EVICTION_MASK;
238
Johannes Weinera5289102014-04-03 14:47:51 -0700239 inc_zone_state(zone, WORKINGSET_REFAULT);
240
241 if (refault_distance <= zone_page_state(zone, NR_ACTIVE_FILE)) {
242 inc_zone_state(zone, WORKINGSET_ACTIVATE);
243 return true;
244 }
245 return false;
246}
247
248/**
249 * workingset_activation - note a page activation
250 * @page: page that is being activated
251 */
252void workingset_activation(struct page *page)
253{
254 atomic_long_inc(&page_zone(page)->inactive_age);
255}
Johannes Weiner449dd692014-04-03 14:47:56 -0700256
257/*
258 * Shadow entries reflect the share of the working set that does not
259 * fit into memory, so their number depends on the access pattern of
260 * the workload. In most cases, they will refault or get reclaimed
261 * along with the inode, but a (malicious) workload that streams
262 * through files with a total size several times that of available
263 * memory, while preventing the inodes from being reclaimed, can
264 * create excessive amounts of shadow nodes. To keep a lid on this,
265 * track shadow nodes and reclaim them when they grow way past the
266 * point where they would still be useful.
267 */
268
269struct list_lru workingset_shadow_nodes;
270
271static unsigned long count_shadow_nodes(struct shrinker *shrinker,
272 struct shrink_control *sc)
273{
274 unsigned long shadow_nodes;
275 unsigned long max_nodes;
276 unsigned long pages;
277
278 /* list_lru lock nests inside IRQ-safe mapping->tree_lock */
279 local_irq_disable();
Vladimir Davydov503c3582015-02-12 14:58:47 -0800280 shadow_nodes = list_lru_shrink_count(&workingset_shadow_nodes, sc);
Johannes Weiner449dd692014-04-03 14:47:56 -0700281 local_irq_enable();
282
283 pages = node_present_pages(sc->nid);
284 /*
285 * Active cache pages are limited to 50% of memory, and shadow
286 * entries that represent a refault distance bigger than that
287 * do not have any effect. Limit the number of shadow nodes
288 * such that shadow entries do not exceed the number of active
289 * cache pages, assuming a worst-case node population density
290 * of 1/8th on average.
291 *
292 * On 64-bit with 7 radix_tree_nodes per page and 64 slots
293 * each, this will reclaim shadow entries when they consume
294 * ~2% of available memory:
295 *
296 * PAGE_SIZE / radix_tree_nodes / node_entries / PAGE_SIZE
297 */
298 max_nodes = pages >> (1 + RADIX_TREE_MAP_SHIFT - 3);
299
300 if (shadow_nodes <= max_nodes)
301 return 0;
302
303 return shadow_nodes - max_nodes;
304}
305
306static enum lru_status shadow_lru_isolate(struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800307 struct list_lru_one *lru,
Johannes Weiner449dd692014-04-03 14:47:56 -0700308 spinlock_t *lru_lock,
309 void *arg)
310{
311 struct address_space *mapping;
312 struct radix_tree_node *node;
313 unsigned int i;
314 int ret;
315
316 /*
317 * Page cache insertions and deletions synchroneously maintain
318 * the shadow node LRU under the mapping->tree_lock and the
319 * lru_lock. Because the page cache tree is emptied before
320 * the inode can be destroyed, holding the lru_lock pins any
321 * address_space that has radix tree nodes on the LRU.
322 *
323 * We can then safely transition to the mapping->tree_lock to
324 * pin only the address_space of the particular node we want
325 * to reclaim, take the node off-LRU, and drop the lru_lock.
326 */
327
328 node = container_of(item, struct radix_tree_node, private_list);
329 mapping = node->private_data;
330
331 /* Coming from the list, invert the lock order */
332 if (!spin_trylock(&mapping->tree_lock)) {
333 spin_unlock(lru_lock);
334 ret = LRU_RETRY;
335 goto out;
336 }
337
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800338 list_lru_isolate(lru, item);
Johannes Weiner449dd692014-04-03 14:47:56 -0700339 spin_unlock(lru_lock);
340
341 /*
342 * The nodes should only contain one or more shadow entries,
343 * no pages, so we expect to be able to remove them all and
344 * delete and free the empty node afterwards.
345 */
346
347 BUG_ON(!node->count);
348 BUG_ON(node->count & RADIX_TREE_COUNT_MASK);
349
350 for (i = 0; i < RADIX_TREE_MAP_SIZE; i++) {
351 if (node->slots[i]) {
352 BUG_ON(!radix_tree_exceptional_entry(node->slots[i]));
353 node->slots[i] = NULL;
354 BUG_ON(node->count < (1U << RADIX_TREE_COUNT_SHIFT));
355 node->count -= 1U << RADIX_TREE_COUNT_SHIFT;
Ross Zwislerf9fe48b2016-01-22 15:10:40 -0800356 BUG_ON(!mapping->nrexceptional);
357 mapping->nrexceptional--;
Johannes Weiner449dd692014-04-03 14:47:56 -0700358 }
359 }
360 BUG_ON(node->count);
361 inc_zone_state(page_zone(virt_to_page(node)), WORKINGSET_NODERECLAIM);
362 if (!__radix_tree_delete_node(&mapping->page_tree, node))
363 BUG();
364
365 spin_unlock(&mapping->tree_lock);
366 ret = LRU_REMOVED_RETRY;
367out:
368 local_irq_enable();
369 cond_resched();
370 local_irq_disable();
371 spin_lock(lru_lock);
372 return ret;
373}
374
375static unsigned long scan_shadow_nodes(struct shrinker *shrinker,
376 struct shrink_control *sc)
377{
378 unsigned long ret;
379
380 /* list_lru lock nests inside IRQ-safe mapping->tree_lock */
381 local_irq_disable();
Vladimir Davydov503c3582015-02-12 14:58:47 -0800382 ret = list_lru_shrink_walk(&workingset_shadow_nodes, sc,
383 shadow_lru_isolate, NULL);
Johannes Weiner449dd692014-04-03 14:47:56 -0700384 local_irq_enable();
385 return ret;
386}
387
388static struct shrinker workingset_shadow_shrinker = {
389 .count_objects = count_shadow_nodes,
390 .scan_objects = scan_shadow_nodes,
391 .seeks = DEFAULT_SEEKS,
392 .flags = SHRINKER_NUMA_AWARE,
393};
394
395/*
396 * Our list_lru->lock is IRQ-safe as it nests inside the IRQ-safe
397 * mapping->tree_lock.
398 */
399static struct lock_class_key shadow_nodes_key;
400
401static int __init workingset_init(void)
402{
403 int ret;
404
405 ret = list_lru_init_key(&workingset_shadow_nodes, &shadow_nodes_key);
406 if (ret)
407 goto err;
408 ret = register_shrinker(&workingset_shadow_shrinker);
409 if (ret)
410 goto err_list_lru;
411 return 0;
412err_list_lru:
413 list_lru_destroy(&workingset_shadow_nodes);
414err:
415 return ret;
416}
417module_init(workingset_init);