blob: 2372d4ed5dd8ff763d3aaed5cb546c8033eca1ff [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Chaugule7602bdf2006-12-06 20:31:57 -080010 *
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 Torvalds1da177e2005-04-16 15:20:36 -070018 */
Ashwin Chaugule7602bdf2006-12-06 20:31:57 -080019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/jiffies.h>
21#include <linux/mm.h>
22#include <linux/sched.h>
23#include <linux/swap.h>
24
25static DEFINE_SPINLOCK(swap_token_lock);
Ashwin Chaugule7602bdf2006-12-06 20:31:57 -080026struct mm_struct *swap_token_mm;
Adrian Bunke3050052006-12-06 20:32:43 -080027static unsigned int global_faults;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Hugh Dickinsa5c9b692009-06-23 12:36:58 -070029void grab_swap_token(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Ashwin Chaugule7602bdf2006-12-06 20:31:57 -080031 int current_interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Ashwin Chaugule7602bdf2006-12-06 20:31:57 -080033 global_faults++;
34
Hugh Dickinsa5c9b692009-06-23 12:36:58 -070035 current_interval = global_faults - mm->faultstamp;
Ashwin Chaugule7602bdf2006-12-06 20:31:57 -080036
37 if (!spin_trylock(&swap_token_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 return;
Ashwin Chaugule7602bdf2006-12-06 20:31:57 -080039
40 /* First come first served */
41 if (swap_token_mm == NULL) {
Hugh Dickinsa5c9b692009-06-23 12:36:58 -070042 mm->token_priority = mm->token_priority + 2;
43 swap_token_mm = mm;
Ashwin Chaugule7602bdf2006-12-06 20:31:57 -080044 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 }
46
Hugh Dickinsa5c9b692009-06-23 12:36:58 -070047 if (mm != swap_token_mm) {
48 if (current_interval < mm->last_interval)
49 mm->token_priority++;
Ashwin Chaugule7602bdf2006-12-06 20:31:57 -080050 else {
Hugh Dickinsa5c9b692009-06-23 12:36:58 -070051 if (likely(mm->token_priority > 0))
52 mm->token_priority--;
Rik van Rielf7b7fd82005-11-28 13:44:07 -080053 }
Ashwin Chaugule7602bdf2006-12-06 20:31:57 -080054 /* Check if we deserve the token */
Hugh Dickinsa5c9b692009-06-23 12:36:58 -070055 if (mm->token_priority > swap_token_mm->token_priority) {
56 mm->token_priority += 2;
57 swap_token_mm = mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 }
Ashwin Chaugule7602bdf2006-12-06 20:31:57 -080059 } else {
60 /* Token holder came in again! */
Hugh Dickinsa5c9b692009-06-23 12:36:58 -070061 mm->token_priority += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 }
Ashwin Chaugule7602bdf2006-12-06 20:31:57 -080063
64out:
Hugh Dickinsa5c9b692009-06-23 12:36:58 -070065 mm->faultstamp = global_faults;
66 mm->last_interval = current_interval;
Ashwin Chaugule7602bdf2006-12-06 20:31:57 -080067 spin_unlock(&swap_token_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
70/* Called on process exit. */
71void __put_swap_token(struct mm_struct *mm)
72{
73 spin_lock(&swap_token_lock);
Ashwin Chaugule7602bdf2006-12-06 20:31:57 -080074 if (likely(mm == swap_token_mm))
75 swap_token_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 spin_unlock(&swap_token_lock);
77}