Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
KOSAKI Motohiro | e21c7ff | 2011-07-25 17:12:06 -0700 | [diff] [blame] | 9 | * described in: http://www.cse.ohio-state.edu/hpcs/WWW/HTML/publications/abs05-1.html |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/jiffies.h> |
| 21 | #include <linux/mm.h> |
| 22 | #include <linux/sched.h> |
| 23 | #include <linux/swap.h> |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 24 | #include <linux/memcontrol.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
KOSAKI Motohiro | 83cd81a | 2011-06-15 15:08:14 -0700 | [diff] [blame] | 26 | #include <trace/events/vmscan.h> |
| 27 | |
KOSAKI Motohiro | d7911ef3 | 2011-06-15 15:08:15 -0700 | [diff] [blame] | 28 | #define TOKEN_AGING_INTERVAL (0xFF) |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | static DEFINE_SPINLOCK(swap_token_lock); |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 31 | struct mm_struct *swap_token_mm; |
H Hartley Sweeten | 22d5368 | 2011-10-31 17:09:19 -0700 | [diff] [blame] | 32 | static struct mem_cgroup *swap_token_memcg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 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 | |
Hugh Dickins | a5c9b69 | 2009-06-23 12:36:58 -0700 | [diff] [blame] | 52 | void grab_swap_token(struct mm_struct *mm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | { |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 54 | int current_interval; |
KOSAKI Motohiro | 83cd81a | 2011-06-15 15:08:14 -0700 | [diff] [blame] | 55 | unsigned int old_prio = mm->token_priority; |
KOSAKI Motohiro | 53bb01f | 2011-07-25 17:12:07 -0700 | [diff] [blame] | 56 | static unsigned int global_faults; |
| 57 | static unsigned int last_aging; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 59 | global_faults++; |
| 60 | |
Hugh Dickins | a5c9b69 | 2009-06-23 12:36:58 -0700 | [diff] [blame] | 61 | current_interval = global_faults - mm->faultstamp; |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 62 | |
| 63 | if (!spin_trylock(&swap_token_lock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | return; |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 65 | |
| 66 | /* First come first served */ |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 67 | if (!swap_token_mm) |
| 68 | goto replace_token; |
| 69 | |
KOSAKI Motohiro | 45ebb84 | 2011-07-25 17:12:08 -0700 | [diff] [blame] | 70 | /* |
| 71 | * Usually, we don't need priority aging because long interval faults |
| 72 | * makes priority decrease quickly. But there is one exception. If the |
| 73 | * token owner task is sleeping, it never make long interval faults. |
| 74 | * Thus, we need a priority aging mechanism instead. The requirements |
| 75 | * of priority aging are |
| 76 | * 1) An aging interval is reasonable enough long. Too short aging |
| 77 | * interval makes quick swap token lost and decrease performance. |
| 78 | * 2) The swap token owner task have to get priority aging even if |
| 79 | * it's under sleep. |
| 80 | */ |
KOSAKI Motohiro | d7911ef3 | 2011-06-15 15:08:15 -0700 | [diff] [blame] | 81 | if ((global_faults - last_aging) > TOKEN_AGING_INTERVAL) { |
| 82 | swap_token_mm->token_priority /= 2; |
| 83 | last_aging = global_faults; |
| 84 | } |
| 85 | |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 86 | if (mm == swap_token_mm) { |
| 87 | mm->token_priority += 2; |
KOSAKI Motohiro | 83cd81a | 2011-06-15 15:08:14 -0700 | [diff] [blame] | 88 | goto update_priority; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
| 90 | |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 91 | if (current_interval < mm->last_interval) |
| 92 | mm->token_priority++; |
| 93 | else { |
| 94 | if (likely(mm->token_priority > 0)) |
| 95 | mm->token_priority--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | } |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 97 | |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 98 | /* Check if we deserve the token */ |
| 99 | if (mm->token_priority > swap_token_mm->token_priority) |
| 100 | goto replace_token; |
| 101 | |
KOSAKI Motohiro | 83cd81a | 2011-06-15 15:08:14 -0700 | [diff] [blame] | 102 | update_priority: |
KOSAKI Motohiro | d7911ef3 | 2011-06-15 15:08:15 -0700 | [diff] [blame] | 103 | trace_update_swap_token_priority(mm, old_prio, swap_token_mm); |
KOSAKI Motohiro | 83cd81a | 2011-06-15 15:08:14 -0700 | [diff] [blame] | 104 | |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 105 | out: |
Hugh Dickins | a5c9b69 | 2009-06-23 12:36:58 -0700 | [diff] [blame] | 106 | mm->faultstamp = global_faults; |
| 107 | mm->last_interval = current_interval; |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 108 | spin_unlock(&swap_token_lock); |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 109 | return; |
| 110 | |
| 111 | replace_token: |
| 112 | mm->token_priority += 2; |
KOSAKI Motohiro | 83cd81a | 2011-06-15 15:08:14 -0700 | [diff] [blame] | 113 | trace_replace_swap_token(swap_token_mm, mm); |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 114 | swap_token_mm = mm; |
| 115 | swap_token_memcg = swap_token_memcg_from_mm(mm); |
KOSAKI Motohiro | d7911ef3 | 2011-06-15 15:08:15 -0700 | [diff] [blame] | 116 | last_aging = global_faults; |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 117 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* Called on process exit. */ |
| 121 | void __put_swap_token(struct mm_struct *mm) |
| 122 | { |
| 123 | spin_lock(&swap_token_lock); |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 124 | if (likely(mm == swap_token_mm)) { |
KOSAKI Motohiro | 83cd81a | 2011-06-15 15:08:14 -0700 | [diff] [blame] | 125 | trace_put_swap_token(swap_token_mm); |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 126 | swap_token_mm = NULL; |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 127 | swap_token_memcg = NULL; |
| 128 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | spin_unlock(&swap_token_lock); |
| 130 | } |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 131 | |
| 132 | static bool match_memcg(struct mem_cgroup *a, struct mem_cgroup *b) |
| 133 | { |
| 134 | if (!a) |
| 135 | return true; |
| 136 | if (!b) |
| 137 | return true; |
| 138 | if (a == b) |
| 139 | return true; |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | void disable_swap_token(struct mem_cgroup *memcg) |
| 144 | { |
| 145 | /* memcg reclaim don't disable unrelated mm token. */ |
| 146 | if (match_memcg(memcg, swap_token_memcg)) { |
| 147 | spin_lock(&swap_token_lock); |
| 148 | if (match_memcg(memcg, swap_token_memcg)) { |
KOSAKI Motohiro | 83cd81a | 2011-06-15 15:08:14 -0700 | [diff] [blame] | 149 | trace_disable_swap_token(swap_token_mm); |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 150 | swap_token_mm = NULL; |
| 151 | swap_token_memcg = NULL; |
| 152 | } |
| 153 | spin_unlock(&swap_token_lock); |
| 154 | } |
| 155 | } |