13bd94003SHeinz Mauelshagen // SPDX-License-Identifier: GPL-2.0-only
2298fb372SMike Snitzer /*
3298fb372SMike Snitzer * Copyright (C) 2004-2005 IBM Corp. All Rights Reserved.
4298fb372SMike Snitzer * Copyright (C) 2006-2009 NEC Corporation.
5298fb372SMike Snitzer *
6298fb372SMike Snitzer * dm-queue-length.c
7298fb372SMike Snitzer *
8298fb372SMike Snitzer * Module Author: Stefan Bader, IBM
9298fb372SMike Snitzer * Modified by: Kiyoshi Ueda, NEC
10298fb372SMike Snitzer *
11298fb372SMike Snitzer * This file is released under the GPL.
12298fb372SMike Snitzer *
13298fb372SMike Snitzer * queue-length path selector - choose a path with the least number of
14298fb372SMike Snitzer * in-flight I/Os.
15298fb372SMike Snitzer */
16298fb372SMike Snitzer
17298fb372SMike Snitzer #include "dm.h"
18298fb372SMike Snitzer #include "dm-path-selector.h"
19298fb372SMike Snitzer
20298fb372SMike Snitzer #include <linux/slab.h>
21298fb372SMike Snitzer #include <linux/ctype.h>
22298fb372SMike Snitzer #include <linux/errno.h>
23298fb372SMike Snitzer #include <linux/module.h>
24298fb372SMike Snitzer #include <linux/atomic.h>
25298fb372SMike Snitzer
26298fb372SMike Snitzer #define DM_MSG_PREFIX "multipath queue-length"
27298fb372SMike Snitzer #define QL_MIN_IO 1
28298fb372SMike Snitzer #define QL_VERSION "0.2.0"
29298fb372SMike Snitzer
30298fb372SMike Snitzer struct selector {
31298fb372SMike Snitzer struct list_head valid_paths;
32298fb372SMike Snitzer struct list_head failed_paths;
33298fb372SMike Snitzer spinlock_t lock;
34298fb372SMike Snitzer };
35298fb372SMike Snitzer
36298fb372SMike Snitzer struct path_info {
37298fb372SMike Snitzer struct list_head list;
38298fb372SMike Snitzer struct dm_path *path;
39*86a3238cSHeinz Mauelshagen unsigned int repeat_count;
40298fb372SMike Snitzer atomic_t qlen; /* the number of in-flight I/Os */
41298fb372SMike Snitzer };
42298fb372SMike Snitzer
alloc_selector(void)43298fb372SMike Snitzer static struct selector *alloc_selector(void)
44298fb372SMike Snitzer {
45298fb372SMike Snitzer struct selector *s = kmalloc(sizeof(*s), GFP_KERNEL);
46298fb372SMike Snitzer
47298fb372SMike Snitzer if (s) {
48298fb372SMike Snitzer INIT_LIST_HEAD(&s->valid_paths);
49298fb372SMike Snitzer INIT_LIST_HEAD(&s->failed_paths);
50298fb372SMike Snitzer spin_lock_init(&s->lock);
51298fb372SMike Snitzer }
52298fb372SMike Snitzer
53298fb372SMike Snitzer return s;
54298fb372SMike Snitzer }
55298fb372SMike Snitzer
ql_create(struct path_selector * ps,unsigned int argc,char ** argv)56*86a3238cSHeinz Mauelshagen static int ql_create(struct path_selector *ps, unsigned int argc, char **argv)
57298fb372SMike Snitzer {
58298fb372SMike Snitzer struct selector *s = alloc_selector();
59298fb372SMike Snitzer
60298fb372SMike Snitzer if (!s)
61298fb372SMike Snitzer return -ENOMEM;
62298fb372SMike Snitzer
63298fb372SMike Snitzer ps->context = s;
64298fb372SMike Snitzer return 0;
65298fb372SMike Snitzer }
66298fb372SMike Snitzer
ql_free_paths(struct list_head * paths)67298fb372SMike Snitzer static void ql_free_paths(struct list_head *paths)
68298fb372SMike Snitzer {
69298fb372SMike Snitzer struct path_info *pi, *next;
70298fb372SMike Snitzer
71298fb372SMike Snitzer list_for_each_entry_safe(pi, next, paths, list) {
72298fb372SMike Snitzer list_del(&pi->list);
73298fb372SMike Snitzer kfree(pi);
74298fb372SMike Snitzer }
75298fb372SMike Snitzer }
76298fb372SMike Snitzer
ql_destroy(struct path_selector * ps)77298fb372SMike Snitzer static void ql_destroy(struct path_selector *ps)
78298fb372SMike Snitzer {
79298fb372SMike Snitzer struct selector *s = ps->context;
80298fb372SMike Snitzer
81298fb372SMike Snitzer ql_free_paths(&s->valid_paths);
82298fb372SMike Snitzer ql_free_paths(&s->failed_paths);
83298fb372SMike Snitzer kfree(s);
84298fb372SMike Snitzer ps->context = NULL;
85298fb372SMike Snitzer }
86298fb372SMike Snitzer
ql_status(struct path_selector * ps,struct dm_path * path,status_type_t type,char * result,unsigned int maxlen)87298fb372SMike Snitzer static int ql_status(struct path_selector *ps, struct dm_path *path,
88*86a3238cSHeinz Mauelshagen status_type_t type, char *result, unsigned int maxlen)
89298fb372SMike Snitzer {
90*86a3238cSHeinz Mauelshagen unsigned int sz = 0;
91298fb372SMike Snitzer struct path_info *pi;
92298fb372SMike Snitzer
93298fb372SMike Snitzer /* When called with NULL path, return selector status/args. */
94298fb372SMike Snitzer if (!path)
95298fb372SMike Snitzer DMEMIT("0 ");
96298fb372SMike Snitzer else {
97298fb372SMike Snitzer pi = path->pscontext;
98298fb372SMike Snitzer
99298fb372SMike Snitzer switch (type) {
100298fb372SMike Snitzer case STATUSTYPE_INFO:
101298fb372SMike Snitzer DMEMIT("%d ", atomic_read(&pi->qlen));
102298fb372SMike Snitzer break;
103298fb372SMike Snitzer case STATUSTYPE_TABLE:
104298fb372SMike Snitzer DMEMIT("%u ", pi->repeat_count);
105298fb372SMike Snitzer break;
1068ec45662STushar Sugandhi case STATUSTYPE_IMA:
1078ec45662STushar Sugandhi *result = '\0';
1088ec45662STushar Sugandhi break;
109298fb372SMike Snitzer }
110298fb372SMike Snitzer }
111298fb372SMike Snitzer
112298fb372SMike Snitzer return sz;
113298fb372SMike Snitzer }
114298fb372SMike Snitzer
ql_add_path(struct path_selector * ps,struct dm_path * path,int argc,char ** argv,char ** error)115298fb372SMike Snitzer static int ql_add_path(struct path_selector *ps, struct dm_path *path,
116298fb372SMike Snitzer int argc, char **argv, char **error)
117298fb372SMike Snitzer {
118298fb372SMike Snitzer struct selector *s = ps->context;
119298fb372SMike Snitzer struct path_info *pi;
120*86a3238cSHeinz Mauelshagen unsigned int repeat_count = QL_MIN_IO;
121298fb372SMike Snitzer char dummy;
122298fb372SMike Snitzer unsigned long flags;
123298fb372SMike Snitzer
124298fb372SMike Snitzer /*
125298fb372SMike Snitzer * Arguments: [<repeat_count>]
126298fb372SMike Snitzer * <repeat_count>: The number of I/Os before switching path.
127298fb372SMike Snitzer * If not given, default (QL_MIN_IO) is used.
128298fb372SMike Snitzer */
129298fb372SMike Snitzer if (argc > 1) {
130298fb372SMike Snitzer *error = "queue-length ps: incorrect number of arguments";
131298fb372SMike Snitzer return -EINVAL;
132298fb372SMike Snitzer }
133298fb372SMike Snitzer
134298fb372SMike Snitzer if ((argc == 1) && (sscanf(argv[0], "%u%c", &repeat_count, &dummy) != 1)) {
135298fb372SMike Snitzer *error = "queue-length ps: invalid repeat count";
136298fb372SMike Snitzer return -EINVAL;
137298fb372SMike Snitzer }
138298fb372SMike Snitzer
139298fb372SMike Snitzer if (repeat_count > 1) {
140298fb372SMike Snitzer DMWARN_LIMIT("repeat_count > 1 is deprecated, using 1 instead");
141298fb372SMike Snitzer repeat_count = 1;
142298fb372SMike Snitzer }
143298fb372SMike Snitzer
144298fb372SMike Snitzer /* Allocate the path information structure */
145298fb372SMike Snitzer pi = kmalloc(sizeof(*pi), GFP_KERNEL);
146298fb372SMike Snitzer if (!pi) {
147298fb372SMike Snitzer *error = "queue-length ps: Error allocating path information";
148298fb372SMike Snitzer return -ENOMEM;
149298fb372SMike Snitzer }
150298fb372SMike Snitzer
151298fb372SMike Snitzer pi->path = path;
152298fb372SMike Snitzer pi->repeat_count = repeat_count;
153298fb372SMike Snitzer atomic_set(&pi->qlen, 0);
154298fb372SMike Snitzer
155298fb372SMike Snitzer path->pscontext = pi;
156298fb372SMike Snitzer
157298fb372SMike Snitzer spin_lock_irqsave(&s->lock, flags);
158298fb372SMike Snitzer list_add_tail(&pi->list, &s->valid_paths);
159298fb372SMike Snitzer spin_unlock_irqrestore(&s->lock, flags);
160298fb372SMike Snitzer
161298fb372SMike Snitzer return 0;
162298fb372SMike Snitzer }
163298fb372SMike Snitzer
ql_fail_path(struct path_selector * ps,struct dm_path * path)164298fb372SMike Snitzer static void ql_fail_path(struct path_selector *ps, struct dm_path *path)
165298fb372SMike Snitzer {
166298fb372SMike Snitzer struct selector *s = ps->context;
167298fb372SMike Snitzer struct path_info *pi = path->pscontext;
168298fb372SMike Snitzer unsigned long flags;
169298fb372SMike Snitzer
170298fb372SMike Snitzer spin_lock_irqsave(&s->lock, flags);
171298fb372SMike Snitzer list_move(&pi->list, &s->failed_paths);
172298fb372SMike Snitzer spin_unlock_irqrestore(&s->lock, flags);
173298fb372SMike Snitzer }
174298fb372SMike Snitzer
ql_reinstate_path(struct path_selector * ps,struct dm_path * path)175298fb372SMike Snitzer static int ql_reinstate_path(struct path_selector *ps, struct dm_path *path)
176298fb372SMike Snitzer {
177298fb372SMike Snitzer struct selector *s = ps->context;
178298fb372SMike Snitzer struct path_info *pi = path->pscontext;
179298fb372SMike Snitzer unsigned long flags;
180298fb372SMike Snitzer
181298fb372SMike Snitzer spin_lock_irqsave(&s->lock, flags);
182298fb372SMike Snitzer list_move_tail(&pi->list, &s->valid_paths);
183298fb372SMike Snitzer spin_unlock_irqrestore(&s->lock, flags);
184298fb372SMike Snitzer
185298fb372SMike Snitzer return 0;
186298fb372SMike Snitzer }
187298fb372SMike Snitzer
188298fb372SMike Snitzer /*
189298fb372SMike Snitzer * Select a path having the minimum number of in-flight I/Os
190298fb372SMike Snitzer */
ql_select_path(struct path_selector * ps,size_t nr_bytes)191298fb372SMike Snitzer static struct dm_path *ql_select_path(struct path_selector *ps, size_t nr_bytes)
192298fb372SMike Snitzer {
193298fb372SMike Snitzer struct selector *s = ps->context;
194298fb372SMike Snitzer struct path_info *pi = NULL, *best = NULL;
195298fb372SMike Snitzer struct dm_path *ret = NULL;
196298fb372SMike Snitzer unsigned long flags;
197298fb372SMike Snitzer
198298fb372SMike Snitzer spin_lock_irqsave(&s->lock, flags);
199298fb372SMike Snitzer if (list_empty(&s->valid_paths))
200298fb372SMike Snitzer goto out;
201298fb372SMike Snitzer
202298fb372SMike Snitzer list_for_each_entry(pi, &s->valid_paths, list) {
203298fb372SMike Snitzer if (!best ||
204298fb372SMike Snitzer (atomic_read(&pi->qlen) < atomic_read(&best->qlen)))
205298fb372SMike Snitzer best = pi;
206298fb372SMike Snitzer
207298fb372SMike Snitzer if (!atomic_read(&best->qlen))
208298fb372SMike Snitzer break;
209298fb372SMike Snitzer }
210298fb372SMike Snitzer
211298fb372SMike Snitzer if (!best)
212298fb372SMike Snitzer goto out;
213298fb372SMike Snitzer
214298fb372SMike Snitzer /* Move most recently used to least preferred to evenly balance. */
215298fb372SMike Snitzer list_move_tail(&best->list, &s->valid_paths);
216298fb372SMike Snitzer
217298fb372SMike Snitzer ret = best->path;
218298fb372SMike Snitzer out:
219298fb372SMike Snitzer spin_unlock_irqrestore(&s->lock, flags);
220298fb372SMike Snitzer return ret;
221298fb372SMike Snitzer }
222298fb372SMike Snitzer
ql_start_io(struct path_selector * ps,struct dm_path * path,size_t nr_bytes)223298fb372SMike Snitzer static int ql_start_io(struct path_selector *ps, struct dm_path *path,
224298fb372SMike Snitzer size_t nr_bytes)
225298fb372SMike Snitzer {
226298fb372SMike Snitzer struct path_info *pi = path->pscontext;
227298fb372SMike Snitzer
228298fb372SMike Snitzer atomic_inc(&pi->qlen);
229298fb372SMike Snitzer
230298fb372SMike Snitzer return 0;
231298fb372SMike Snitzer }
232298fb372SMike Snitzer
ql_end_io(struct path_selector * ps,struct dm_path * path,size_t nr_bytes,u64 start_time)233298fb372SMike Snitzer static int ql_end_io(struct path_selector *ps, struct dm_path *path,
234298fb372SMike Snitzer size_t nr_bytes, u64 start_time)
235298fb372SMike Snitzer {
236298fb372SMike Snitzer struct path_info *pi = path->pscontext;
237298fb372SMike Snitzer
238298fb372SMike Snitzer atomic_dec(&pi->qlen);
239298fb372SMike Snitzer
240298fb372SMike Snitzer return 0;
241298fb372SMike Snitzer }
242298fb372SMike Snitzer
243298fb372SMike Snitzer static struct path_selector_type ql_ps = {
244298fb372SMike Snitzer .name = "queue-length",
245298fb372SMike Snitzer .module = THIS_MODULE,
246298fb372SMike Snitzer .table_args = 1,
247298fb372SMike Snitzer .info_args = 1,
248298fb372SMike Snitzer .create = ql_create,
249298fb372SMike Snitzer .destroy = ql_destroy,
250298fb372SMike Snitzer .status = ql_status,
251298fb372SMike Snitzer .add_path = ql_add_path,
252298fb372SMike Snitzer .fail_path = ql_fail_path,
253298fb372SMike Snitzer .reinstate_path = ql_reinstate_path,
254298fb372SMike Snitzer .select_path = ql_select_path,
255298fb372SMike Snitzer .start_io = ql_start_io,
256298fb372SMike Snitzer .end_io = ql_end_io,
257298fb372SMike Snitzer };
258298fb372SMike Snitzer
dm_ql_init(void)259298fb372SMike Snitzer static int __init dm_ql_init(void)
260298fb372SMike Snitzer {
261298fb372SMike Snitzer int r = dm_register_path_selector(&ql_ps);
262298fb372SMike Snitzer
263298fb372SMike Snitzer if (r < 0)
264298fb372SMike Snitzer DMERR("register failed %d", r);
265298fb372SMike Snitzer
266298fb372SMike Snitzer DMINFO("version " QL_VERSION " loaded");
267298fb372SMike Snitzer
268298fb372SMike Snitzer return r;
269298fb372SMike Snitzer }
270298fb372SMike Snitzer
dm_ql_exit(void)271298fb372SMike Snitzer static void __exit dm_ql_exit(void)
272298fb372SMike Snitzer {
273298fb372SMike Snitzer int r = dm_unregister_path_selector(&ql_ps);
274298fb372SMike Snitzer
275298fb372SMike Snitzer if (r < 0)
276298fb372SMike Snitzer DMERR("unregister failed %d", r);
277298fb372SMike Snitzer }
278298fb372SMike Snitzer
279298fb372SMike Snitzer module_init(dm_ql_init);
280298fb372SMike Snitzer module_exit(dm_ql_exit);
281298fb372SMike Snitzer
282298fb372SMike Snitzer MODULE_AUTHOR("Stefan Bader <Stefan.Bader at de.ibm.com>");
283298fb372SMike Snitzer MODULE_DESCRIPTION(
284298fb372SMike Snitzer "(C) Copyright IBM Corp. 2004,2005 All Rights Reserved.\n"
285298fb372SMike Snitzer DM_NAME " path selector to balance the number of in-flight I/Os"
286298fb372SMike Snitzer );
287298fb372SMike Snitzer MODULE_LICENSE("GPL");
288