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> |
| 24 | |
| 25 | static DEFINE_SPINLOCK(swap_token_lock); |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 26 | struct mm_struct *swap_token_mm; |
Adrian Bunk | e305005 | 2006-12-06 20:32:43 -0800 | [diff] [blame] | 27 | static unsigned int global_faults; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Hugh Dickins | a5c9b69 | 2009-06-23 12:36:58 -0700 | [diff] [blame] | 29 | void grab_swap_token(struct mm_struct *mm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | { |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 31 | int current_interval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 33 | global_faults++; |
| 34 | |
Hugh Dickins | a5c9b69 | 2009-06-23 12:36:58 -0700 | [diff] [blame] | 35 | current_interval = global_faults - mm->faultstamp; |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 36 | |
| 37 | if (!spin_trylock(&swap_token_lock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | return; |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 39 | |
| 40 | /* First come first served */ |
| 41 | if (swap_token_mm == NULL) { |
Hugh Dickins | a5c9b69 | 2009-06-23 12:36:58 -0700 | [diff] [blame] | 42 | mm->token_priority = mm->token_priority + 2; |
| 43 | swap_token_mm = mm; |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 44 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Hugh Dickins | a5c9b69 | 2009-06-23 12:36:58 -0700 | [diff] [blame] | 47 | if (mm != swap_token_mm) { |
| 48 | if (current_interval < mm->last_interval) |
| 49 | mm->token_priority++; |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 50 | else { |
Hugh Dickins | a5c9b69 | 2009-06-23 12:36:58 -0700 | [diff] [blame] | 51 | if (likely(mm->token_priority > 0)) |
| 52 | mm->token_priority--; |
Rik van Riel | f7b7fd8 | 2005-11-28 13:44:07 -0800 | [diff] [blame] | 53 | } |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 54 | /* Check if we deserve the token */ |
Hugh Dickins | a5c9b69 | 2009-06-23 12:36:58 -0700 | [diff] [blame] | 55 | if (mm->token_priority > swap_token_mm->token_priority) { |
| 56 | mm->token_priority += 2; |
| 57 | swap_token_mm = mm; |
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 | } else { |
| 60 | /* Token holder came in again! */ |
Hugh Dickins | a5c9b69 | 2009-06-23 12:36:58 -0700 | [diff] [blame] | 61 | mm->token_priority += 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | } |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 63 | |
| 64 | out: |
Hugh Dickins | a5c9b69 | 2009-06-23 12:36:58 -0700 | [diff] [blame] | 65 | mm->faultstamp = global_faults; |
| 66 | mm->last_interval = current_interval; |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 67 | spin_unlock(&swap_token_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /* Called on process exit. */ |
| 71 | void __put_swap_token(struct mm_struct *mm) |
| 72 | { |
| 73 | spin_lock(&swap_token_lock); |
Ashwin Chaugule | 7602bdf | 2006-12-06 20:31:57 -0800 | [diff] [blame] | 74 | if (likely(mm == swap_token_mm)) |
| 75 | swap_token_mm = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | spin_unlock(&swap_token_lock); |
| 77 | } |