diff options
Diffstat (limited to 'mm/thrash.c')
-rw-r--r-- | mm/thrash.c | 105 |
1 files changed, 86 insertions, 19 deletions
diff --git a/mm/thrash.c b/mm/thrash.c index 2372d4ed5dd8..fabf2d0f5169 100644 --- a/mm/thrash.c +++ b/mm/thrash.c | |||
@@ -21,14 +21,40 @@ | |||
21 | #include <linux/mm.h> | 21 | #include <linux/mm.h> |
22 | #include <linux/sched.h> | 22 | #include <linux/sched.h> |
23 | #include <linux/swap.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) | ||
24 | 29 | ||
25 | static DEFINE_SPINLOCK(swap_token_lock); | 30 | static DEFINE_SPINLOCK(swap_token_lock); |
26 | struct mm_struct *swap_token_mm; | 31 | struct mm_struct *swap_token_mm; |
32 | struct mem_cgroup *swap_token_memcg; | ||
27 | static unsigned int global_faults; | 33 | static unsigned int global_faults; |
34 | static unsigned int last_aging; | ||
35 | |||
36 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR | ||
37 | static 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 | ||
48 | static struct mem_cgroup *swap_token_memcg_from_mm(struct mm_struct *mm) | ||
49 | { | ||
50 | return NULL; | ||
51 | } | ||
52 | #endif | ||
28 | 53 | ||
29 | void grab_swap_token(struct mm_struct *mm) | 54 | void grab_swap_token(struct mm_struct *mm) |
30 | { | 55 | { |
31 | int current_interval; | 56 | int current_interval; |
57 | unsigned int old_prio = mm->token_priority; | ||
32 | 58 | ||
33 | global_faults++; | 59 | global_faults++; |
34 | 60 | ||
@@ -38,40 +64,81 @@ void grab_swap_token(struct mm_struct *mm) | |||
38 | return; | 64 | return; |
39 | 65 | ||
40 | /* First come first served */ | 66 | /* First come first served */ |
41 | if (swap_token_mm == NULL) { | 67 | if (!swap_token_mm) |
42 | mm->token_priority = mm->token_priority + 2; | 68 | goto replace_token; |
43 | swap_token_mm = mm; | 69 | |
44 | goto out; | 70 | if ((global_faults - last_aging) > TOKEN_AGING_INTERVAL) { |
71 | swap_token_mm->token_priority /= 2; | ||
72 | last_aging = global_faults; | ||
45 | } | 73 | } |
46 | 74 | ||
47 | if (mm != swap_token_mm) { | 75 | if (mm == swap_token_mm) { |
48 | if (current_interval < mm->last_interval) | ||
49 | mm->token_priority++; | ||
50 | else { | ||
51 | if (likely(mm->token_priority > 0)) | ||
52 | mm->token_priority--; | ||
53 | } | ||
54 | /* Check if we deserve the token */ | ||
55 | if (mm->token_priority > swap_token_mm->token_priority) { | ||
56 | mm->token_priority += 2; | ||
57 | swap_token_mm = mm; | ||
58 | } | ||
59 | } else { | ||
60 | /* Token holder came in again! */ | ||
61 | mm->token_priority += 2; | 76 | mm->token_priority += 2; |
77 | goto update_priority; | ||
78 | } | ||
79 | |||
80 | if (current_interval < mm->last_interval) | ||
81 | mm->token_priority++; | ||
82 | else { | ||
83 | if (likely(mm->token_priority > 0)) | ||
84 | mm->token_priority--; | ||
62 | } | 85 | } |
63 | 86 | ||
87 | /* Check if we deserve the token */ | ||
88 | if (mm->token_priority > swap_token_mm->token_priority) | ||
89 | goto replace_token; | ||
90 | |||
91 | update_priority: | ||
92 | trace_update_swap_token_priority(mm, old_prio, swap_token_mm); | ||
93 | |||
64 | out: | 94 | out: |
65 | mm->faultstamp = global_faults; | 95 | mm->faultstamp = global_faults; |
66 | mm->last_interval = current_interval; | 96 | mm->last_interval = current_interval; |
67 | spin_unlock(&swap_token_lock); | 97 | spin_unlock(&swap_token_lock); |
98 | return; | ||
99 | |||
100 | replace_token: | ||
101 | mm->token_priority += 2; | ||
102 | trace_replace_swap_token(swap_token_mm, mm); | ||
103 | swap_token_mm = mm; | ||
104 | swap_token_memcg = swap_token_memcg_from_mm(mm); | ||
105 | last_aging = global_faults; | ||
106 | goto out; | ||
68 | } | 107 | } |
69 | 108 | ||
70 | /* Called on process exit. */ | 109 | /* Called on process exit. */ |
71 | void __put_swap_token(struct mm_struct *mm) | 110 | void __put_swap_token(struct mm_struct *mm) |
72 | { | 111 | { |
73 | spin_lock(&swap_token_lock); | 112 | spin_lock(&swap_token_lock); |
74 | if (likely(mm == swap_token_mm)) | 113 | if (likely(mm == swap_token_mm)) { |
114 | trace_put_swap_token(swap_token_mm); | ||
75 | swap_token_mm = NULL; | 115 | swap_token_mm = NULL; |
116 | swap_token_memcg = NULL; | ||
117 | } | ||
76 | spin_unlock(&swap_token_lock); | 118 | spin_unlock(&swap_token_lock); |
77 | } | 119 | } |
120 | |||
121 | static 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 | |||
132 | void 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)) { | ||
138 | trace_disable_swap_token(swap_token_mm); | ||
139 | swap_token_mm = NULL; | ||
140 | swap_token_memcg = NULL; | ||
141 | } | ||
142 | spin_unlock(&swap_token_lock); | ||
143 | } | ||
144 | } | ||