Skip to content

Commit 7602bdf

Browse files
Ashwin ChauguleLinus Torvalds
authored andcommitted
[PATCH] new scheme to preempt swap token
The new swap token patches replace the current token traversal algo. The old algo had a crude timeout parameter that was used to handover the token from one task to another. This algo, transfers the token to the tasks that are in need of the token. The urgency for the token is based on the number of times a task is required to swap-in pages. Accordingly, the priority of a task is incremented if it has been badly affected due to swap-outs. To ensure that the token doesnt bounce around rapidly, the token holders are given a priority boost. The priority of tasks is also decremented, if their rate of swap-in's keeps reducing. This way, the condition to check whether to pre-empt the swap token, is a matter of comparing two task's priority fields. [[email protected]: cleanups] Signed-off-by: Ashwin Chaugule <[email protected]> Cc: Rik van Riel <[email protected]> Cc: Peter Zijlstra <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 098fe65 commit 7602bdf

File tree

5 files changed

+63
-86
lines changed

5 files changed

+63
-86
lines changed

include/linux/sched.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,16 @@ struct mm_struct {
344344
/* Architecture-specific MM context */
345345
mm_context_t context;
346346

347-
/* Token based thrashing protection. */
348-
unsigned long swap_token_time;
349-
char recent_pagein;
347+
/* Swap token stuff */
348+
/*
349+
* Last value of global fault stamp as seen by this process.
350+
* In other words, this value gives an indication of how long
351+
* it has been since this task got the token.
352+
* Look at mm/thrash.c
353+
*/
354+
unsigned int faultstamp;
355+
unsigned int token_priority;
356+
unsigned int last_interval;
350357

351358
/* coredumping support */
352359
int core_waiters;

include/linux/swap.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ extern spinlock_t swap_lock;
259259

260260
/* linux/mm/thrash.c */
261261
extern struct mm_struct * swap_token_mm;
262-
extern unsigned long swap_token_default_timeout;
263262
extern void grab_swap_token(void);
264263
extern void __put_swap_token(struct mm_struct *);
265264

kernel/fork.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,10 @@ static struct mm_struct *dup_mm(struct task_struct *tsk)
479479

480480
memcpy(mm, oldmm, sizeof(*mm));
481481

482+
/* Initializing for Swap token stuff */
483+
mm->token_priority = 0;
484+
mm->last_interval = 0;
485+
482486
if (!mm_init(mm))
483487
goto fail_nomem;
484488

@@ -542,6 +546,10 @@ static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
542546
goto fail_nomem;
543547

544548
good_mm:
549+
/* Initializing for Swap token stuff */
550+
mm->token_priority = 0;
551+
mm->last_interval = 0;
552+
545553
tsk->mm = mm;
546554
tsk->active_mm = mm;
547555
return 0;

kernel/sysctl.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -977,17 +977,6 @@ static ctl_table vm_table[] = {
977977
.extra1 = &zero,
978978
},
979979
#endif
980-
#ifdef CONFIG_SWAP
981-
{
982-
.ctl_name = VM_SWAP_TOKEN_TIMEOUT,
983-
.procname = "swap_token_timeout",
984-
.data = &swap_token_default_timeout,
985-
.maxlen = sizeof(swap_token_default_timeout),
986-
.mode = 0644,
987-
.proc_handler = &proc_dointvec_jiffies,
988-
.strategy = &sysctl_jiffies,
989-
},
990-
#endif
991980
#ifdef CONFIG_NUMA
992981
{
993982
.ctl_name = VM_ZONE_RECLAIM_MODE,

mm/thrash.c

Lines changed: 45 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -7,100 +7,74 @@
77
*
88
* Simple token based thrashing protection, using the algorithm
99
* described in: http://www.cs.wm.edu/~sjiang/token.pdf
10+
*
11+
* Sep 2006, Ashwin Chaugule <[email protected]>
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.
1018
*/
19+
1120
#include <linux/jiffies.h>
1221
#include <linux/mm.h>
1322
#include <linux/sched.h>
1423
#include <linux/swap.h>
1524

1625
static DEFINE_SPINLOCK(swap_token_lock);
17-
static unsigned long swap_token_timeout;
18-
static unsigned long swap_token_check;
19-
struct mm_struct * swap_token_mm = &init_mm;
20-
21-
#define SWAP_TOKEN_CHECK_INTERVAL (HZ * 2)
22-
#define SWAP_TOKEN_TIMEOUT (300 * HZ)
23-
/*
24-
* Currently disabled; Needs further code to work at HZ * 300.
25-
*/
26-
unsigned long swap_token_default_timeout = SWAP_TOKEN_TIMEOUT;
27-
28-
/*
29-
* Take the token away if the process had no page faults
30-
* in the last interval, or if it has held the token for
31-
* too long.
32-
*/
33-
#define SWAP_TOKEN_ENOUGH_RSS 1
34-
#define SWAP_TOKEN_TIMED_OUT 2
35-
static int should_release_swap_token(struct mm_struct *mm)
36-
{
37-
int ret = 0;
38-
if (!mm->recent_pagein)
39-
ret = SWAP_TOKEN_ENOUGH_RSS;
40-
else if (time_after(jiffies, swap_token_timeout))
41-
ret = SWAP_TOKEN_TIMED_OUT;
42-
mm->recent_pagein = 0;
43-
return ret;
44-
}
26+
struct mm_struct *swap_token_mm;
27+
unsigned int global_faults;
4528

46-
/*
47-
* Try to grab the swapout protection token. We only try to
48-
* grab it once every TOKEN_CHECK_INTERVAL, both to prevent
49-
* SMP lock contention and to check that the process that held
50-
* the token before is no longer thrashing.
51-
*/
5229
void grab_swap_token(void)
5330
{
54-
struct mm_struct *mm;
55-
int reason;
31+
int current_interval;
5632

57-
/* We have the token. Let others know we still need it. */
58-
if (has_swap_token(current->mm)) {
59-
current->mm->recent_pagein = 1;
60-
if (unlikely(!swap_token_default_timeout))
61-
disable_swap_token();
62-
return;
63-
}
64-
65-
if (time_after(jiffies, swap_token_check)) {
33+
global_faults++;
6634

67-
if (!swap_token_default_timeout) {
68-
swap_token_check = jiffies + SWAP_TOKEN_CHECK_INTERVAL;
69-
return;
70-
}
71-
72-
/* ... or if we recently held the token. */
73-
if (time_before(jiffies, current->mm->swap_token_time))
74-
return;
35+
current_interval = global_faults - current->mm->faultstamp;
7536

76-
if (!spin_trylock(&swap_token_lock))
77-
return;
37+
if (!spin_trylock(&swap_token_lock))
38+
return;
7839

79-
swap_token_check = jiffies + SWAP_TOKEN_CHECK_INTERVAL;
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;
45+
}
8046

81-
mm = swap_token_mm;
82-
if ((reason = should_release_swap_token(mm))) {
83-
unsigned long eligible = jiffies;
84-
if (reason == SWAP_TOKEN_TIMED_OUT) {
85-
eligible += swap_token_default_timeout;
86-
}
87-
mm->swap_token_time = eligible;
88-
swap_token_timeout = jiffies + swap_token_default_timeout;
47+
if (current->mm != swap_token_mm) {
48+
if (current_interval < current->mm->last_interval)
49+
current->mm->token_priority++;
50+
else {
51+
current->mm->token_priority--;
52+
if (unlikely(current->mm->token_priority < 0))
53+
current->mm->token_priority = 0;
54+
}
55+
/* Check if we deserve the token */
56+
if (current->mm->token_priority >
57+
swap_token_mm->token_priority) {
58+
current->mm->token_priority += 2;
8959
swap_token_mm = current->mm;
9060
}
91-
spin_unlock(&swap_token_lock);
61+
} else {
62+
/* Token holder came in again! */
63+
current->mm->token_priority += 2;
9264
}
93-
return;
65+
66+
out:
67+
current->mm->faultstamp = global_faults;
68+
current->mm->last_interval = current_interval;
69+
spin_unlock(&swap_token_lock);
70+
return;
9471
}
9572

9673
/* Called on process exit. */
9774
void __put_swap_token(struct mm_struct *mm)
9875
{
9976
spin_lock(&swap_token_lock);
100-
if (likely(mm == swap_token_mm)) {
101-
mm->swap_token_time = jiffies + SWAP_TOKEN_CHECK_INTERVAL;
102-
swap_token_mm = &init_mm;
103-
swap_token_check = jiffies;
104-
}
77+
if (likely(mm == swap_token_mm))
78+
swap_token_mm = NULL;
10579
spin_unlock(&swap_token_lock);
10680
}

0 commit comments

Comments
 (0)