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>
22 #include <linux/sched.h>
23 #include <linux/swap.h>
24 #include <linux/memcontrol.h>
26 #include <trace/events/vmscan.h>
28 static DEFINE_SPINLOCK(swap_token_lock);
29 struct mm_struct *swap_token_mm;
30 struct mem_cgroup *swap_token_memcg;
31 static unsigned int global_faults;
33 #ifdef CONFIG_CGROUP_MEM_RES_CTLR
34 static struct mem_cgroup *swap_token_memcg_from_mm(struct mm_struct *mm)
36 struct mem_cgroup *memcg;
38 memcg = try_get_mem_cgroup_from_mm(mm);
40 css_put(mem_cgroup_css(memcg));
45 static struct mem_cgroup *swap_token_memcg_from_mm(struct mm_struct *mm)
51 void grab_swap_token(struct mm_struct *mm)
54 unsigned int old_prio = mm->token_priority;
58 current_interval = global_faults - mm->faultstamp;
60 if (!spin_trylock(&swap_token_lock))
63 /* First come first served */
67 if (mm == swap_token_mm) {
68 mm->token_priority += 2;
72 if (current_interval < mm->last_interval)
75 if (likely(mm->token_priority > 0))
79 /* Check if we deserve the token */
80 if (mm->token_priority > swap_token_mm->token_priority)
84 trace_update_swap_token_priority(mm, old_prio);
87 mm->faultstamp = global_faults;
88 mm->last_interval = current_interval;
89 spin_unlock(&swap_token_lock);
93 mm->token_priority += 2;
94 trace_replace_swap_token(swap_token_mm, mm);
96 swap_token_memcg = swap_token_memcg_from_mm(mm);
100 /* Called on process exit. */
101 void __put_swap_token(struct mm_struct *mm)
103 spin_lock(&swap_token_lock);
104 if (likely(mm == swap_token_mm)) {
105 trace_put_swap_token(swap_token_mm);
106 swap_token_mm = NULL;
107 swap_token_memcg = NULL;
109 spin_unlock(&swap_token_lock);
112 static bool match_memcg(struct mem_cgroup *a, struct mem_cgroup *b)
123 void disable_swap_token(struct mem_cgroup *memcg)
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)) {
129 trace_disable_swap_token(swap_token_mm);
130 swap_token_mm = NULL;
131 swap_token_memcg = NULL;
133 spin_unlock(&swap_token_lock);