Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / mm / thrash.c
1 /*
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.cse.ohio-state.edu/hpcs/WWW/HTML/publications/abs05-1.html
10  *
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.
18  */
19
20 #include <linux/jiffies.h>
21 #include <linux/mm.h>
22 #include <linux/sched.h>
23 #include <linux/swap.h>
24 #include <linux/memcontrol.h>
25
26 #include <trace/events/vmscan.h>
27
28 #define TOKEN_AGING_INTERVAL    (0xFF)
29
30 static DEFINE_SPINLOCK(swap_token_lock);
31 struct mm_struct *swap_token_mm;
32 static struct mem_cgroup *swap_token_memcg;
33
34 #ifdef CONFIG_CGROUP_MEM_RES_CTLR
35 static struct mem_cgroup *swap_token_memcg_from_mm(struct mm_struct *mm)
36 {
37         struct mem_cgroup *memcg;
38
39         memcg = try_get_mem_cgroup_from_mm(mm);
40         if (memcg)
41                 css_put(mem_cgroup_css(memcg));
42
43         return memcg;
44 }
45 #else
46 static struct mem_cgroup *swap_token_memcg_from_mm(struct mm_struct *mm)
47 {
48         return NULL;
49 }
50 #endif
51
52 void grab_swap_token(struct mm_struct *mm)
53 {
54         int current_interval;
55         unsigned int old_prio;
56         static unsigned int global_faults;
57         static unsigned int last_aging;
58
59         global_faults++;
60         if (mm == NULL)
61                 return;
62
63         old_prio = mm->token_priority;
64         current_interval = global_faults - mm->faultstamp;
65
66         if (!spin_trylock(&swap_token_lock))
67                 return;
68
69         /* First come first served */
70         if (!swap_token_mm)
71                 goto replace_token;
72
73         /*
74          * Usually, we don't need priority aging because long interval faults
75          * makes priority decrease quickly. But there is one exception. If the
76          * token owner task is sleeping, it never make long interval faults.
77          * Thus, we need a priority aging mechanism instead. The requirements
78          * of priority aging are
79          *  1) An aging interval is reasonable enough long. Too short aging
80          *     interval makes quick swap token lost and decrease performance.
81          *  2) The swap token owner task have to get priority aging even if
82          *     it's under sleep.
83          */
84         if ((global_faults - last_aging) > TOKEN_AGING_INTERVAL) {
85                 swap_token_mm->token_priority /= 2;
86                 last_aging = global_faults;
87         }
88
89         if (mm == swap_token_mm) {
90                 mm->token_priority += 2;
91                 goto update_priority;
92         }
93
94         if (current_interval < mm->last_interval)
95                 mm->token_priority++;
96         else {
97                 if (likely(mm->token_priority > 0))
98                         mm->token_priority--;
99         }
100
101         /* Check if we deserve the token */
102         if (mm->token_priority > swap_token_mm->token_priority)
103                 goto replace_token;
104
105 update_priority:
106         trace_update_swap_token_priority(mm, old_prio, swap_token_mm);
107
108 out:
109         mm->faultstamp = global_faults;
110         mm->last_interval = current_interval;
111         spin_unlock(&swap_token_lock);
112         return;
113
114 replace_token:
115         mm->token_priority += 2;
116         trace_replace_swap_token(swap_token_mm, mm);
117         swap_token_mm = mm;
118         swap_token_memcg = swap_token_memcg_from_mm(mm);
119         last_aging = global_faults;
120         goto out;
121 }
122
123 /* Called on process exit. */
124 void __put_swap_token(struct mm_struct *mm)
125 {
126         spin_lock(&swap_token_lock);
127         if (likely(mm == swap_token_mm)) {
128                 trace_put_swap_token(swap_token_mm);
129                 swap_token_mm = NULL;
130                 swap_token_memcg = NULL;
131         }
132         spin_unlock(&swap_token_lock);
133 }
134
135 static bool match_memcg(struct mem_cgroup *a, struct mem_cgroup *b)
136 {
137         if (!a)
138                 return true;
139         if (!b)
140                 return true;
141         if (a == b)
142                 return true;
143         return false;
144 }
145
146 void disable_swap_token(struct mem_cgroup *memcg)
147 {
148         /* memcg reclaim don't disable unrelated mm token. */
149         if (match_memcg(memcg, swap_token_memcg)) {
150                 spin_lock(&swap_token_lock);
151                 if (match_memcg(memcg, swap_token_memcg)) {
152                         trace_disable_swap_token(swap_token_mm);
153                         swap_token_mm = NULL;
154                         swap_token_memcg = NULL;
155                 }
156                 spin_unlock(&swap_token_lock);
157         }
158 }