1 From: Kiyoshi Ueda <k-ueda@ct.jp.nec.com>
2 Subject: dm-mpath: add queue-length dynamic load balancer
3 References: FATE#303862,FATE#302108
5 This patch adds a dynamic load balancer, dm-queue-length, which
6 balances the number of in-flight I/Os.
8 The code is based on the patch posted by Stefan Bader:
9 https://www.redhat.com/archives/dm-devel/2005-October/msg00050.html
12 Signed-off-by: Stefan Bader <stefan.bader@canonical.com>
13 Signed-off-by: Kiyoshi Ueda <k-ueda@ct.jp.nec.com>
14 Signed-off-by: Jun'ichi Nomura <j-nomura@ce.jp.nec.com>
15 Signed-off-by: Hannes Reinecke <hare@suse.de>
17 Index: linux-2.6.27/drivers/md/Makefile
18 ===================================================================
19 --- linux-2.6.27.orig/drivers/md/Makefile
20 +++ linux-2.6.27/drivers/md/Makefile
21 @@ -32,7 +32,8 @@ obj-$(CONFIG_BLK_DEV_MD) += md-mod.o
22 obj-$(CONFIG_BLK_DEV_DM) += dm-mod.o
23 obj-$(CONFIG_DM_CRYPT) += dm-crypt.o
24 obj-$(CONFIG_DM_DELAY) += dm-delay.o
25 -obj-$(CONFIG_DM_MULTIPATH) += dm-multipath.o dm-round-robin.o dm-least-pending.o
26 +obj-$(CONFIG_DM_MULTIPATH) += dm-multipath.o dm-round-robin.o \
27 + dm-least-pending.o dm-queue-length.o
28 obj-$(CONFIG_DM_SNAPSHOT) += dm-snapshot.o
29 obj-$(CONFIG_DM_MIRROR) += dm-mirror.o dm-regions.o dm-log.o
30 obj-$(CONFIG_DM_RAID45) += dm-raid45.o dm-log.o dm-memcache.o \
31 Index: linux-2.6.27/drivers/md/dm-queue-length.c
32 ===================================================================
34 +++ linux-2.6.27/drivers/md/dm-queue-length.c
37 + * Copyright (C) 2004-2005 IBM Corp. All Rights Reserved.
38 + * Copyright (C) 2006-2008 NEC Corporation.
42 + * Module Author: Stefan Bader, IBM
43 + * Modified by: Kiyoshi Ueda, NEC
45 + * This file is released under the GPL.
47 + * Load balancing path selector.
51 +#include "dm-path-selector.h"
53 +#include <linux/slab.h>
54 +#include <linux/ctype.h>
55 +#include <linux/errno.h>
56 +#include <linux/module.h>
57 +#include <asm/atomic.h>
59 +#define DM_MSG_PREFIX "multipath queue-length"
60 +#define QL_MIN_IO 128
61 +#define QL_VERSION "0.1.0"
64 + struct list_head valid_paths;
65 + struct list_head failed_paths;
69 + struct list_head list;
70 + struct dm_path *path;
71 + unsigned int repeat_count;
75 +static struct selector *alloc_selector(void)
77 + struct selector *s = kzalloc(sizeof(*s), GFP_KERNEL);
80 + INIT_LIST_HEAD(&s->valid_paths);
81 + INIT_LIST_HEAD(&s->failed_paths);
87 +static int ql_create(struct path_selector *ps, unsigned argc, char **argv)
89 + struct selector *s = alloc_selector();
99 +static void ql_free_paths(struct list_head *paths)
101 + struct path_info *cpi, *npi;
103 + list_for_each_entry_safe(cpi, npi, paths, list) {
104 + list_del(&cpi->list);
105 + cpi->path->pscontext = NULL;
110 +static void ql_destroy(struct path_selector *ps)
112 + struct selector *s = (struct selector *) ps->context;
114 + ql_free_paths(&s->valid_paths);
115 + ql_free_paths(&s->failed_paths);
117 + ps->context = NULL;
120 +static int ql_add_path(struct path_selector *ps, struct dm_path *path,
121 + int argc, char **argv, char **error)
123 + struct selector *s = (struct selector *) ps->context;
124 + struct path_info *pi;
125 + unsigned int repeat_count = QL_MIN_IO;
127 + /* Parse the arguments */
129 + *error = "queue-length ps: incorrect number of arguments";
133 + /* First path argument is number of I/Os before switching path. */
134 + if ((argc == 1) && (sscanf(argv[0], "%u", &repeat_count) != 1)) {
135 + *error = "queue-length ps: invalid repeat count";
139 + /* Allocate the path information structure */
140 + pi = kmalloc(sizeof(*pi), GFP_KERNEL);
142 + *error = "queue-length ps: Error allocating path information";
147 + pi->repeat_count = repeat_count;
148 + atomic_set(&pi->qlen, 0);
149 + path->pscontext = pi;
151 + list_add_tail(&pi->list, &s->valid_paths);
156 +static void ql_fail_path(struct path_selector *ps, struct dm_path *path)
158 + struct selector *s = (struct selector *) ps->context;
159 + struct path_info *pi = path->pscontext;
161 + list_move(&pi->list, &s->failed_paths);
164 +static int ql_reinstate_path(struct path_selector *ps, struct dm_path *path)
166 + struct selector *s = (struct selector *) ps->context;
167 + struct path_info *pi = path->pscontext;
169 + list_move_tail(&pi->list, &s->valid_paths);
174 +static inline int ql_compare_qlen(struct path_info *pi1, struct path_info *pi2)
176 + return atomic_read(&pi1->qlen) - atomic_read(&pi2->qlen);
179 +static struct dm_path *ql_select_path(struct path_selector *ps,
180 + unsigned *repeat_count)
182 + struct selector *s = (struct selector *) ps->context;
183 + struct path_info *cpi = NULL, *spi = NULL;
185 + if (list_empty(&s->valid_paths))
188 + /* Change preferred (first in list) path to evenly balance. */
189 + list_move_tail(s->valid_paths.next, &s->valid_paths);
191 + list_for_each_entry(cpi, &s->valid_paths, list) {
194 + else if (ql_compare_qlen(cpi, spi) < 0)
199 + *repeat_count = spi->repeat_count;
201 + return spi ? spi->path : NULL;
204 +static int ql_start_io(struct path_selector *ps, struct dm_path *path)
206 + struct path_info *pi = path->pscontext;
208 + atomic_inc(&pi->qlen);
213 +static int ql_end_io(struct path_selector *ps, struct dm_path *path)
215 + struct path_info *pi = path->pscontext;
217 + atomic_dec(&pi->qlen);
222 +static int ql_status(struct path_selector *ps, struct dm_path *path,
223 + status_type_t type, char *result, unsigned int maxlen)
226 + struct path_info *pi;
228 + /* When called with (path == NULL), return selector status/args. */
232 + pi = path->pscontext;
235 + case STATUSTYPE_INFO:
236 + DMEMIT("%u ", atomic_read(&pi->qlen));
238 + case STATUSTYPE_TABLE:
239 + DMEMIT("%u ", pi->repeat_count);
247 +static struct path_selector_type ql_ps = {
248 + .name = "queue-length",
249 + .module = THIS_MODULE,
252 + .create = ql_create,
253 + .destroy = ql_destroy,
254 + .status = ql_status,
255 + .add_path = ql_add_path,
256 + .fail_path = ql_fail_path,
257 + .reinstate_path = ql_reinstate_path,
258 + .select_path = ql_select_path,
259 + .start_io = ql_start_io,
260 + .end_io = ql_end_io,
263 +static int __init dm_ql_init(void)
265 + int r = dm_register_path_selector(&ql_ps);
268 + DMERR("register failed %d", r);
270 + DMINFO("version " QL_VERSION " loaded");
275 +static void __exit dm_ql_exit(void)
277 + int r = dm_unregister_path_selector(&ql_ps);
280 + DMERR("unregister failed %d", r);
283 +module_init(dm_ql_init);
284 +module_exit(dm_ql_exit);
286 +MODULE_AUTHOR("Stefan Bader <Stefan.Bader at de.ibm.com>");
288 + "(C) Copyright IBM Corp. 2004,2005 All Rights Reserved.\n"
289 + DM_NAME " load balancing path selector (dm-queue-length.c version "
292 +MODULE_LICENSE("GPL");