Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/roland...
[linux-2.6/mini2440.git] / mm / thrash.c
blobc4c5205a9c35f82c2dc34b52b4b37a8b0a0ecc44
1 /*
2 * mm/thrash.c
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.
8 * Simple token based thrashing protection, using the algorithm
9 * described in: http://www.cs.wm.edu/~sjiang/token.pdf
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.
20 #include <linux/jiffies.h>
21 #include <linux/mm.h>
22 #include <linux/sched.h>
23 #include <linux/swap.h>
25 static DEFINE_SPINLOCK(swap_token_lock);
26 struct mm_struct *swap_token_mm;
27 static unsigned int global_faults;
29 void grab_swap_token(void)
31 int current_interval;
33 global_faults++;
35 current_interval = global_faults - current->mm->faultstamp;
37 if (!spin_trylock(&swap_token_lock))
38 return;
40 /* First come first served */
41 if (swap_token_mm == NULL) {
42 current->mm->token_priority = current->mm->token_priority + 2;
43 swap_token_mm = current->mm;
44 goto out;
47 if (current->mm != swap_token_mm) {
48 if (current_interval < current->mm->last_interval)
49 current->mm->token_priority++;
50 else {
51 if (likely(current->mm->token_priority > 0))
52 current->mm->token_priority--;
54 /* Check if we deserve the token */
55 if (current->mm->token_priority >
56 swap_token_mm->token_priority) {
57 current->mm->token_priority += 2;
58 swap_token_mm = current->mm;
60 } else {
61 /* Token holder came in again! */
62 current->mm->token_priority += 2;
65 out:
66 current->mm->faultstamp = global_faults;
67 current->mm->last_interval = current_interval;
68 spin_unlock(&swap_token_lock);
69 return;
72 /* Called on process exit. */
73 void __put_swap_token(struct mm_struct *mm)
75 spin_lock(&swap_token_lock);
76 if (likely(mm == swap_token_mm))
77 swap_token_mm = NULL;
78 spin_unlock(&swap_token_lock);