1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2007-2009 NEC Corporation. All Rights Reserved.
5 * Module Author: Kiyoshi Ueda
7 * This file is released under the GPL.
9 * Throughput oriented path selector.
13 #include "dm-path-selector.h"
15 #include <linux/slab.h>
16 #include <linux/module.h>
18 #define DM_MSG_PREFIX "multipath service-time"
20 #define ST_MAX_RELATIVE_THROUGHPUT 100
21 #define ST_MAX_RELATIVE_THROUGHPUT_SHIFT 7
22 #define ST_MAX_INFLIGHT_SIZE ((size_t)-1 >> ST_MAX_RELATIVE_THROUGHPUT_SHIFT)
23 #define ST_VERSION "0.3.0"
26 struct list_head valid_paths;
27 struct list_head failed_paths;
32 struct list_head list;
34 unsigned int repeat_count;
35 unsigned int relative_throughput;
36 atomic_t in_flight_size; /* Total size of in-flight I/Os */
39 static struct selector *alloc_selector(void)
41 struct selector *s = kmalloc(sizeof(*s), GFP_KERNEL);
44 INIT_LIST_HEAD(&s->valid_paths);
45 INIT_LIST_HEAD(&s->failed_paths);
46 spin_lock_init(&s->lock);
52 static int st_create(struct path_selector *ps, unsigned int argc, char **argv)
54 struct selector *s = alloc_selector();
63 static void free_paths(struct list_head *paths)
65 struct path_info *pi, *next;
67 list_for_each_entry_safe(pi, next, paths, list) {
73 static void st_destroy(struct path_selector *ps)
75 struct selector *s = ps->context;
77 free_paths(&s->valid_paths);
78 free_paths(&s->failed_paths);
83 static int st_status(struct path_selector *ps, struct dm_path *path,
84 status_type_t type, char *result, unsigned int maxlen)
96 DMEMIT("%d %u ", atomic_read(&pi->in_flight_size),
97 pi->relative_throughput);
99 case STATUSTYPE_TABLE:
100 DMEMIT("%u %u ", pi->repeat_count,
101 pi->relative_throughput);
112 static int st_add_path(struct path_selector *ps, struct dm_path *path,
113 int argc, char **argv, char **error)
115 struct selector *s = ps->context;
116 struct path_info *pi;
117 unsigned int repeat_count = ST_MIN_IO;
118 unsigned int relative_throughput = 1;
123 * Arguments: [<repeat_count> [<relative_throughput>]]
124 * <repeat_count>: The number of I/Os before switching path.
125 * If not given, default (ST_MIN_IO) is used.
126 * <relative_throughput>: The relative throughput value of
127 * the path among all paths in the path-group.
128 * The valid range: 0-<ST_MAX_RELATIVE_THROUGHPUT>
129 * If not given, minimum value '1' is used.
130 * If '0' is given, the path isn't selected while
131 * other paths having a positive value are available.
134 *error = "service-time ps: incorrect number of arguments";
138 if (argc && (sscanf(argv[0], "%u%c", &repeat_count, &dummy) != 1)) {
139 *error = "service-time ps: invalid repeat count";
143 if (repeat_count > 1) {
144 DMWARN_LIMIT("repeat_count > 1 is deprecated, using 1 instead");
149 (sscanf(argv[1], "%u%c", &relative_throughput, &dummy) != 1 ||
150 relative_throughput > ST_MAX_RELATIVE_THROUGHPUT)) {
151 *error = "service-time ps: invalid relative_throughput value";
155 /* allocate the path */
156 pi = kmalloc(sizeof(*pi), GFP_KERNEL);
158 *error = "service-time ps: Error allocating path context";
163 pi->repeat_count = repeat_count;
164 pi->relative_throughput = relative_throughput;
165 atomic_set(&pi->in_flight_size, 0);
167 path->pscontext = pi;
169 spin_lock_irqsave(&s->lock, flags);
170 list_add_tail(&pi->list, &s->valid_paths);
171 spin_unlock_irqrestore(&s->lock, flags);
176 static void st_fail_path(struct path_selector *ps, struct dm_path *path)
178 struct selector *s = ps->context;
179 struct path_info *pi = path->pscontext;
182 spin_lock_irqsave(&s->lock, flags);
183 list_move(&pi->list, &s->failed_paths);
184 spin_unlock_irqrestore(&s->lock, flags);
187 static int st_reinstate_path(struct path_selector *ps, struct dm_path *path)
189 struct selector *s = ps->context;
190 struct path_info *pi = path->pscontext;
193 spin_lock_irqsave(&s->lock, flags);
194 list_move_tail(&pi->list, &s->valid_paths);
195 spin_unlock_irqrestore(&s->lock, flags);
201 * Compare the estimated service time of 2 paths, pi1 and pi2,
202 * for the incoming I/O.
205 * < 0 : pi1 is better
206 * 0 : no difference between pi1 and pi2
207 * > 0 : pi2 is better
210 * Basically, the service time is estimated by:
211 * ('pi->in-flight-size' + 'incoming') / 'pi->relative_throughput'
212 * To reduce the calculation, some optimizations are made.
213 * (See comments inline)
215 static int st_compare_load(struct path_info *pi1, struct path_info *pi2,
218 size_t sz1, sz2, st1, st2;
220 sz1 = atomic_read(&pi1->in_flight_size);
221 sz2 = atomic_read(&pi2->in_flight_size);
224 * Case 1: Both have same throughput value. Choose less loaded path.
226 if (pi1->relative_throughput == pi2->relative_throughput)
230 * Case 2a: Both have same load. Choose higher throughput path.
231 * Case 2b: One path has no throughput value. Choose the other one.
234 !pi1->relative_throughput || !pi2->relative_throughput)
235 return pi2->relative_throughput - pi1->relative_throughput;
238 * Case 3: Calculate service time. Choose faster path.
239 * Service time using pi1:
240 * st1 = (sz1 + incoming) / pi1->relative_throughput
241 * Service time using pi2:
242 * st2 = (sz2 + incoming) / pi2->relative_throughput
244 * To avoid the division, transform the expression to use
246 * Because ->relative_throughput > 0 here, if st1 < st2,
247 * the expressions below are the same meaning:
248 * (sz1 + incoming) / pi1->relative_throughput <
249 * (sz2 + incoming) / pi2->relative_throughput
250 * (sz1 + incoming) * pi2->relative_throughput <
251 * (sz2 + incoming) * pi1->relative_throughput
252 * So use the later one.
256 if (unlikely(sz1 >= ST_MAX_INFLIGHT_SIZE ||
257 sz2 >= ST_MAX_INFLIGHT_SIZE)) {
259 * Size may be too big for multiplying pi->relative_throughput
261 * To avoid the overflow and mis-selection, shift down both.
263 sz1 >>= ST_MAX_RELATIVE_THROUGHPUT_SHIFT;
264 sz2 >>= ST_MAX_RELATIVE_THROUGHPUT_SHIFT;
266 st1 = sz1 * pi2->relative_throughput;
267 st2 = sz2 * pi1->relative_throughput;
272 * Case 4: Service time is equal. Choose higher throughput path.
274 return pi2->relative_throughput - pi1->relative_throughput;
277 static struct dm_path *st_select_path(struct path_selector *ps, size_t nr_bytes)
279 struct selector *s = ps->context;
280 struct path_info *pi = NULL, *best = NULL;
281 struct dm_path *ret = NULL;
284 spin_lock_irqsave(&s->lock, flags);
285 if (list_empty(&s->valid_paths))
288 list_for_each_entry(pi, &s->valid_paths, list)
289 if (!best || (st_compare_load(pi, best, nr_bytes) < 0))
295 /* Move most recently used to least preferred to evenly balance. */
296 list_move_tail(&best->list, &s->valid_paths);
300 spin_unlock_irqrestore(&s->lock, flags);
304 static int st_start_io(struct path_selector *ps, struct dm_path *path,
307 struct path_info *pi = path->pscontext;
309 atomic_add(nr_bytes, &pi->in_flight_size);
314 static int st_end_io(struct path_selector *ps, struct dm_path *path,
315 size_t nr_bytes, u64 start_time)
317 struct path_info *pi = path->pscontext;
319 atomic_sub(nr_bytes, &pi->in_flight_size);
324 static struct path_selector_type st_ps = {
325 .name = "service-time",
326 .module = THIS_MODULE,
330 .destroy = st_destroy,
332 .add_path = st_add_path,
333 .fail_path = st_fail_path,
334 .reinstate_path = st_reinstate_path,
335 .select_path = st_select_path,
336 .start_io = st_start_io,
340 static int __init dm_st_init(void)
342 int r = dm_register_path_selector(&st_ps);
345 DMERR("register failed %d", r);
347 DMINFO("version " ST_VERSION " loaded");
352 static void __exit dm_st_exit(void)
354 int r = dm_unregister_path_selector(&st_ps);
357 DMERR("unregister failed %d", r);
360 module_init(dm_st_init);
361 module_exit(dm_st_exit);
363 MODULE_DESCRIPTION(DM_NAME " throughput oriented path selector");
364 MODULE_AUTHOR("Kiyoshi Ueda <k-ueda@ct.jp.nec.com>");
365 MODULE_LICENSE("GPL");