blob: fabf2d0f51695eddacdb6b013fac0b9cdee3a655 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * mm/thrash.c
3 *
4 * Copyright (C) 2004, Red Hat, Inc.
5 * Copyright (C) 2004, Rik van Riel <riel@redhat.com>
6 * Released under the GPL, see the file COPYING for details.
7 *
8 * Simple token based thrashing protection, using the algorithm
9 * described in: http://www.cs.wm.edu/~sjiang/token.pdf
Ashwin Chaugule7602bdf2006-12-06 20:31:57 -080010 *
11 * Sep 2006, Ashwin Chaugule <ashwin.chaugule@celunite.com>
12 * Improved algorithm to pass token:
13 * Each task has a priority which is incremented if it contended
14 * for the token in an interval less than its previous attempt.
15 * If the token is acquired, that task's priority is boosted to prevent
16 * the token from bouncing around too often and to let the task make
17 * some progress in its execution.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Ashwin Chaugule7602bdf2006-12-06 20:31:57 -080019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/jiffies.h>
21#include <linux/mm.h>
22#include <linux/sched.h>
23#include <linux/swap.h>
KOSAKI Motohiroa4336582011-06-15 15:08:13 -070024#include <linux/memcontrol.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
KOSAKI Motohiro83cd81a2011-06-15 15:08:14 -070026#include <trace/events/vmscan.h>
27
KOSAKI Motohirod7911ef32011-06-15 15:08:15 -070028#define TOKEN_AGING_INTERVAL (0xFF)
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030static DEFINE_SPINLOCK(swap_token_lock);
Ashwin Chaugule7602bdf2006-12-06 20:31:57 -080031struct mm_struct *swap_token_mm;
KOSAKI Motohiroa4336582011-06-15 15:08:13 -070032struct mem_cgroup *swap_token_memcg;
Adrian Bunke3050052006-12-06 20:32:43 -080033static unsigned int global_faults;
KOSAKI Motohirod7911ef32011-06-15 15:08:15 -070034static unsigned int last_aging;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
KOSAKI Motohiroa4336582011-06-15 15:08:13 -070036#ifdef CONFIG_CGROUP_MEM_RES_CTLR
37static struct mem_cgroup *swap_token_memcg_from_mm(struct mm_struct *mm)
38{
39 struct mem_cgroup *memcg;
40
41 memcg = try_get_mem_cgroup_from_mm(mm);
42 if (memcg)
43 css_put(mem_cgroup_css(memcg));
44
45 return memcg;
46}
47#else
48static struct mem_cgroup *swap_token_memcg_from_mm(struct mm_struct *mm)
49{
50 return NULL;
51}
52#endif
53
Hugh Dickinsa5c9b692009-06-23 12:36:58 -070054void grab_swap_token(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Ashwin Chaugule7602bdf2006-12-06 20:31:57 -080056 int current_interval;
KOSAKI Motohiro83cd81a2011-06-15 15:08:14 -070057 unsigned int old_prio = mm->token_priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Ashwin Chaugule7602bdf2006-12-06 20:31:57 -080059 global_faults++;
60
Hugh Dickinsa5c9b692009-06-23 12:36:58 -070061 current_interval = global_faults - mm->faultstamp;
Ashwin Chaugule7602bdf2006-12-06 20:31:57 -080062
63 if (!spin_trylock(&swap_token_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 return;
Ashwin Chaugule7602bdf2006-12-06 20:31:57 -080065
66 /* First come first served */
KOSAKI Motohiroa4336582011-06-15 15:08:13 -070067 if (!swap_token_mm)
68 goto replace_token;
69
KOSAKI Motohirod7911ef32011-06-15 15:08:15 -070070 if ((global_faults - last_aging) > TOKEN_AGING_INTERVAL) {
71 swap_token_mm->token_priority /= 2;
72 last_aging = global_faults;
73 }
74
KOSAKI Motohiroa4336582011-06-15 15:08:13 -070075 if (mm == swap_token_mm) {
76 mm->token_priority += 2;
KOSAKI Motohiro83cd81a2011-06-15 15:08:14 -070077 goto update_priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
79
KOSAKI Motohiroa4336582011-06-15 15:08:13 -070080 if (current_interval < mm->last_interval)
81 mm->token_priority++;
82 else {
83 if (likely(mm->token_priority > 0))
84 mm->token_priority--;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
Ashwin Chaugule7602bdf2006-12-06 20:31:57 -080086
KOSAKI Motohiroa4336582011-06-15 15:08:13 -070087 /* Check if we deserve the token */
88 if (mm->token_priority > swap_token_mm->token_priority)
89 goto replace_token;
90
KOSAKI Motohiro83cd81a2011-06-15 15:08:14 -070091update_priority:
KOSAKI Motohirod7911ef32011-06-15 15:08:15 -070092 trace_update_swap_token_priority(mm, old_prio, swap_token_mm);
KOSAKI Motohiro83cd81a2011-06-15 15:08:14 -070093
Ashwin Chaugule7602bdf2006-12-06 20:31:57 -080094out:
Hugh Dickinsa5c9b692009-06-23 12:36:58 -070095 mm->faultstamp = global_faults;
96 mm->last_interval = current_interval;
Ashwin Chaugule7602bdf2006-12-06 20:31:57 -080097 spin_unlock(&swap_token_lock);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -070098 return;
99
100replace_token:
101 mm->token_priority += 2;
KOSAKI Motohiro83cd81a2011-06-15 15:08:14 -0700102 trace_replace_swap_token(swap_token_mm, mm);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700103 swap_token_mm = mm;
104 swap_token_memcg = swap_token_memcg_from_mm(mm);
KOSAKI Motohirod7911ef32011-06-15 15:08:15 -0700105 last_aging = global_faults;
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700106 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
109/* Called on process exit. */
110void __put_swap_token(struct mm_struct *mm)
111{
112 spin_lock(&swap_token_lock);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700113 if (likely(mm == swap_token_mm)) {
KOSAKI Motohiro83cd81a2011-06-15 15:08:14 -0700114 trace_put_swap_token(swap_token_mm);
Ashwin Chaugule7602bdf2006-12-06 20:31:57 -0800115 swap_token_mm = NULL;
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700116 swap_token_memcg = NULL;
117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 spin_unlock(&swap_token_lock);
119}
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700120
121static bool match_memcg(struct mem_cgroup *a, struct mem_cgroup *b)
122{
123 if (!a)
124 return true;
125 if (!b)
126 return true;
127 if (a == b)
128 return true;
129 return false;
130}
131
132void disable_swap_token(struct mem_cgroup *memcg)
133{
134 /* memcg reclaim don't disable unrelated mm token. */
135 if (match_memcg(memcg, swap_token_memcg)) {
136 spin_lock(&swap_token_lock);
137 if (match_memcg(memcg, swap_token_memcg)) {
KOSAKI Motohiro83cd81a2011-06-15 15:08:14 -0700138 trace_disable_swap_token(swap_token_mm);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700139 swap_token_mm = NULL;
140 swap_token_memcg = NULL;
141 }
142 spin_unlock(&swap_token_lock);
143 }
144}