2 * Copyright (c) 2016, The Linux Foundation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/mm_types.h>
15 #include <linux/swapfile.h>
16 #include <linux/swap.h>
18 #define SWAP_RATIO_GROUP_START (SWAP_FLAG_PRIO_MASK - 9) /* 32758 */
19 #define SWAP_RATIO_GROUP_END (SWAP_FLAG_PRIO_MASK) /* 32767 */
20 #define SWAP_FAST_WRITES (SWAPFILE_CLUSTER * (SWAP_CLUSTER_MAX / 8))
21 #define SWAP_SLOW_WRITES SWAPFILE_CLUSTER
24 * The fast/slow swap write ratio.
25 * 100 indicates that all writes should
26 * go to fast swap device.
28 int sysctl_swap_ratio = 100;
30 /* Enable the swap ratio feature */
31 int sysctl_swap_ratio_enable;
33 static bool is_same_group(struct swap_info_struct *a,
34 struct swap_info_struct *b)
36 if (!sysctl_swap_ratio_enable)
39 if (!is_swap_ratio_group(a->prio))
42 if (a->prio == b->prio)
48 /* Caller must hold swap_avail_lock */
49 static int calculate_write_pending(struct swap_info_struct *si,
50 struct swap_info_struct *n)
52 int ratio = sysctl_swap_ratio;
54 if ((ratio < 0) || (ratio > 100))
57 if (WARN_ON(!(si->flags & SWP_FAST)))
60 if ((n->flags & SWP_FAST) || !is_same_group(si, n))
63 si->max_writes = ratio ? SWAP_FAST_WRITES : 0;
64 n->max_writes = ratio ? (SWAP_FAST_WRITES * 100) /
65 ratio - SWAP_FAST_WRITES : SWAP_SLOW_WRITES;
67 si->write_pending = si->max_writes;
68 n->write_pending = n->max_writes;
70 trace_printk("%u, %u\n", si->max_writes, n->max_writes);
75 static int swap_ratio_slow(struct swap_info_struct **si)
77 struct swap_info_struct *n = NULL;
80 spin_lock(&(*si)->lock);
81 spin_lock(&swap_avail_lock);
82 if (&(*si)->avail_list == plist_last(&swap_avail_head)) {
83 /* just to make skip work */
88 n = plist_next_entry(&(*si)->avail_list,
89 struct swap_info_struct,
92 /* No other swap device */
97 spin_unlock(&swap_avail_lock);
99 spin_lock(&swap_avail_lock);
101 if ((*si)->flags & SWP_FAST) {
102 if ((*si)->write_pending) {
103 (*si)->write_pending--;
106 if ((n->flags & SWP_FAST) || !is_same_group(*si, n)) {
107 /* Should never happen */
109 } else if (n->write_pending) {
111 * Requeue fast device, since there are pending
112 * writes for slow device.
114 plist_requeue(&(*si)->avail_list,
117 spin_unlock(&(*si)->lock);
121 if (0 > calculate_write_pending(*si, n)) {
125 /* Restart from fast device */
126 (*si)->write_pending--;
130 if (!(n->flags & SWP_FAST) || !is_same_group(*si, n)) {
131 /* Should never happen */
133 } else if (n->write_pending) {
135 * Pending writes for fast device.
136 * We reach here when slow device is swapped on first,
137 * before fast device.
139 /* requeue slow device to the end */
140 plist_requeue(&(*si)->avail_list, &swap_avail_head);
142 spin_unlock(&(*si)->lock);
146 if ((*si)->write_pending) {
147 (*si)->write_pending--;
149 if (0 > calculate_write_pending(n, *si)) {
154 plist_requeue(&(*si)->avail_list,
156 spin_unlock(&(*si)->lock);
163 spin_unlock(&(*si)->lock);
165 spin_unlock(&swap_avail_lock);
166 /* n and si would have got interchanged */
167 spin_unlock(&n->lock);
171 bool is_swap_ratio_group(int prio)
173 return ((prio >= SWAP_RATIO_GROUP_START) &&
174 (prio <= SWAP_RATIO_GROUP_END)) ? true : false;
177 void setup_swap_ratio(struct swap_info_struct *p, int prio)
179 /* Used only if sysctl_swap_ratio_enable is set */
180 if (is_swap_ratio_group(prio)) {
181 if (p->flags & SWP_FAST)
182 p->write_pending = SWAP_FAST_WRITES;
184 p->write_pending = SWAP_SLOW_WRITES;
185 p->max_writes = p->write_pending;
189 int swap_ratio(struct swap_info_struct **si)
191 if (!sysctl_swap_ratio_enable)
194 if (is_swap_ratio_group((*si)->prio))
195 return swap_ratio_slow(si);