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 |
| 9 | * described in: http://www.cs.wm.edu/~sjiang/token.pdf |
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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | static DEFINE_SPINLOCK(swap_token_lock); |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 29 | struct mm_struct *swap_token_mm; |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 30 | struct mem_cgroup *swap_token_memcg; |
Adrian Bunk | e305005 | 2006-12-06 20:32:43 -0800 | [diff] [blame] | 31 | static unsigned int global_faults; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 33 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR |
| 34 | static struct mem_cgroup *swap_token_memcg_from_mm(struct mm_struct *mm) |
| 35 | { |
| 36 | struct mem_cgroup *memcg; |
| 37 | |
| 38 | memcg = try_get_mem_cgroup_from_mm(mm); |
| 39 | if (memcg) |
| 40 | css_put(mem_cgroup_css(memcg)); |
| 41 | |
| 42 | return memcg; |
| 43 | } |
| 44 | #else |
| 45 | static struct mem_cgroup *swap_token_memcg_from_mm(struct mm_struct *mm) |
| 46 | { |
| 47 | return NULL; |
| 48 | } |
| 49 | #endif |
| 50 | |
Hugh Dickins | a5c9b69 | 2009-06-23 12:36:58 -0700 | [diff] [blame] | 51 | void grab_swap_token(struct mm_struct *mm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 53 | int current_interval; |
KOSAKI Motohiro | 83cd81a | 2011-06-15 15:08:14 -0700 | [diff] [blame^] | 54 | unsigned int old_prio = mm->token_priority; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 56 | global_faults++; |
| 57 | |
Hugh Dickins | a5c9b69 | 2009-06-23 12:36:58 -0700 | [diff] [blame] | 58 | current_interval = global_faults - mm->faultstamp; |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 59 | |
| 60 | if (!spin_trylock(&swap_token_lock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | return; |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 62 | |
| 63 | /* First come first served */ |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 64 | if (!swap_token_mm) |
| 65 | goto replace_token; |
| 66 | |
| 67 | if (mm == swap_token_mm) { |
| 68 | mm->token_priority += 2; |
KOSAKI Motohiro | 83cd81a | 2011-06-15 15:08:14 -0700 | [diff] [blame^] | 69 | goto update_priority; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | } |
| 71 | |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 72 | if (current_interval < mm->last_interval) |
| 73 | mm->token_priority++; |
| 74 | else { |
| 75 | if (likely(mm->token_priority > 0)) |
| 76 | mm->token_priority--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 78 | |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 79 | /* Check if we deserve the token */ |
| 80 | if (mm->token_priority > swap_token_mm->token_priority) |
| 81 | goto replace_token; |
| 82 | |
KOSAKI Motohiro | 83cd81a | 2011-06-15 15:08:14 -0700 | [diff] [blame^] | 83 | update_priority: |
| 84 | trace_update_swap_token_priority(mm, old_prio); |
| 85 | |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 86 | out: |
Hugh Dickins | a5c9b69 | 2009-06-23 12:36:58 -0700 | [diff] [blame] | 87 | mm->faultstamp = global_faults; |
| 88 | mm->last_interval = current_interval; |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 89 | spin_unlock(&swap_token_lock); |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 90 | return; |
| 91 | |
| 92 | replace_token: |
| 93 | mm->token_priority += 2; |
KOSAKI Motohiro | 83cd81a | 2011-06-15 15:08:14 -0700 | [diff] [blame^] | 94 | trace_replace_swap_token(swap_token_mm, mm); |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 95 | swap_token_mm = mm; |
| 96 | swap_token_memcg = swap_token_memcg_from_mm(mm); |
| 97 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /* Called on process exit. */ |
| 101 | void __put_swap_token(struct mm_struct *mm) |
| 102 | { |
| 103 | spin_lock(&swap_token_lock); |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 104 | if (likely(mm == swap_token_mm)) { |
KOSAKI Motohiro | 83cd81a | 2011-06-15 15:08:14 -0700 | [diff] [blame^] | 105 | trace_put_swap_token(swap_token_mm); |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 106 | swap_token_mm = NULL; |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 107 | swap_token_memcg = NULL; |
| 108 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | spin_unlock(&swap_token_lock); |
| 110 | } |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 111 | |
| 112 | static bool match_memcg(struct mem_cgroup *a, struct mem_cgroup *b) |
| 113 | { |
| 114 | if (!a) |
| 115 | return true; |
| 116 | if (!b) |
| 117 | return true; |
| 118 | if (a == b) |
| 119 | return true; |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | void disable_swap_token(struct mem_cgroup *memcg) |
| 124 | { |
| 125 | /* memcg reclaim don't disable unrelated mm token. */ |
| 126 | if (match_memcg(memcg, swap_token_memcg)) { |
| 127 | spin_lock(&swap_token_lock); |
| 128 | if (match_memcg(memcg, swap_token_memcg)) { |
KOSAKI Motohiro | 83cd81a | 2011-06-15 15:08:14 -0700 | [diff] [blame^] | 129 | trace_disable_swap_token(swap_token_mm); |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 130 | swap_token_mm = NULL; |
| 131 | swap_token_memcg = NULL; |
| 132 | } |
| 133 | spin_unlock(&swap_token_lock); |
| 134 | } |
| 135 | } |