xref: /linux-6.15/drivers/md/dm-delay.c (revision d14646f2)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2005-2007 Red Hat GmbH
4  *
5  * A target that delays reads and/or writes and can send
6  * them to different devices.
7  *
8  * This file is released under the GPL.
9  */
10 
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/blkdev.h>
14 #include <linux/bio.h>
15 #include <linux/slab.h>
16 #include <linux/kthread.h>
17 
18 #include <linux/device-mapper.h>
19 
20 #define DM_MSG_PREFIX "delay"
21 
22 struct delay_class {
23 	struct dm_dev *dev;
24 	sector_t start;
25 	unsigned int delay;
26 	unsigned int ops;
27 };
28 
29 struct delay_c {
30 	struct timer_list delay_timer;
31 	struct mutex timer_lock;
32 	struct workqueue_struct *kdelayd_wq;
33 	struct work_struct flush_expired_bios;
34 	struct list_head delayed_bios;
35 	struct task_struct *worker;
36 	bool may_delay;
37 
38 	struct delay_class read;
39 	struct delay_class write;
40 	struct delay_class flush;
41 
42 	int argc;
43 };
44 
45 struct dm_delay_info {
46 	struct delay_c *context;
47 	struct delay_class *class;
48 	struct list_head list;
49 	unsigned long expires;
50 };
51 
52 static DEFINE_MUTEX(delayed_bios_lock);
53 
54 static void handle_delayed_timer(struct timer_list *t)
55 {
56 	struct delay_c *dc = from_timer(dc, t, delay_timer);
57 
58 	queue_work(dc->kdelayd_wq, &dc->flush_expired_bios);
59 }
60 
61 static void queue_timeout(struct delay_c *dc, unsigned long expires)
62 {
63 	mutex_lock(&dc->timer_lock);
64 
65 	if (!timer_pending(&dc->delay_timer) || expires < dc->delay_timer.expires)
66 		mod_timer(&dc->delay_timer, expires);
67 
68 	mutex_unlock(&dc->timer_lock);
69 }
70 
71 static inline bool delay_is_fast(struct delay_c *dc)
72 {
73 	return !!dc->worker;
74 }
75 
76 static void flush_bios(struct bio *bio)
77 {
78 	struct bio *n;
79 
80 	while (bio) {
81 		n = bio->bi_next;
82 		bio->bi_next = NULL;
83 		dm_submit_bio_remap(bio, NULL);
84 		bio = n;
85 	}
86 }
87 
88 static void flush_delayed_bios(struct delay_c *dc, bool flush_all)
89 {
90 	struct dm_delay_info *delayed, *next;
91 	struct bio_list flush_bio_list;
92 	unsigned long next_expires = 0;
93 	bool start_timer = false;
94 	bio_list_init(&flush_bio_list);
95 
96 	mutex_lock(&delayed_bios_lock);
97 	list_for_each_entry_safe(delayed, next, &dc->delayed_bios, list) {
98 		cond_resched();
99 		if (flush_all || time_after_eq(jiffies, delayed->expires)) {
100 			struct bio *bio = dm_bio_from_per_bio_data(delayed,
101 						sizeof(struct dm_delay_info));
102 			list_del(&delayed->list);
103 			bio_list_add(&flush_bio_list, bio);
104 			delayed->class->ops--;
105 			continue;
106 		}
107 
108 		if (!delay_is_fast(dc)) {
109 			if (!start_timer) {
110 				start_timer = true;
111 				next_expires = delayed->expires;
112 			} else {
113 				next_expires = min(next_expires, delayed->expires);
114 			}
115 		}
116 	}
117 	mutex_unlock(&delayed_bios_lock);
118 
119 	if (start_timer)
120 		queue_timeout(dc, next_expires);
121 
122 	flush_bios(bio_list_get(&flush_bio_list));
123 }
124 
125 static int flush_worker_fn(void *data)
126 {
127 	struct delay_c *dc = data;
128 
129 	while (!kthread_should_stop()) {
130 		flush_delayed_bios(dc, false);
131 		mutex_lock(&delayed_bios_lock);
132 		if (unlikely(list_empty(&dc->delayed_bios))) {
133 			set_current_state(TASK_INTERRUPTIBLE);
134 			mutex_unlock(&delayed_bios_lock);
135 			schedule();
136 		} else {
137 			mutex_unlock(&delayed_bios_lock);
138 			cond_resched();
139 		}
140 	}
141 
142 	return 0;
143 }
144 
145 static void flush_expired_bios(struct work_struct *work)
146 {
147 	struct delay_c *dc;
148 
149 	dc = container_of(work, struct delay_c, flush_expired_bios);
150 	flush_delayed_bios(dc, false);
151 }
152 
153 static void delay_dtr(struct dm_target *ti)
154 {
155 	struct delay_c *dc = ti->private;
156 
157 	if (dc->kdelayd_wq) {
158 		timer_shutdown_sync(&dc->delay_timer);
159 		destroy_workqueue(dc->kdelayd_wq);
160 	}
161 
162 	if (dc->read.dev)
163 		dm_put_device(ti, dc->read.dev);
164 	if (dc->write.dev)
165 		dm_put_device(ti, dc->write.dev);
166 	if (dc->flush.dev)
167 		dm_put_device(ti, dc->flush.dev);
168 	if (dc->worker)
169 		kthread_stop(dc->worker);
170 
171 	mutex_destroy(&dc->timer_lock);
172 
173 	kfree(dc);
174 }
175 
176 static int delay_class_ctr(struct dm_target *ti, struct delay_class *c, char **argv)
177 {
178 	int ret;
179 	unsigned long long tmpll;
180 	char dummy;
181 
182 	if (sscanf(argv[1], "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
183 		ti->error = "Invalid device sector";
184 		return -EINVAL;
185 	}
186 	c->start = tmpll;
187 
188 	if (sscanf(argv[2], "%u%c", &c->delay, &dummy) != 1) {
189 		ti->error = "Invalid delay";
190 		return -EINVAL;
191 	}
192 
193 	ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &c->dev);
194 	if (ret) {
195 		ti->error = "Device lookup failed";
196 		return ret;
197 	}
198 
199 	return 0;
200 }
201 
202 /*
203  * Mapping parameters:
204  *    <device> <offset> <delay> [<write_device> <write_offset> <write_delay>]
205  *
206  * With separate write parameters, the first set is only used for reads.
207  * Offsets are specified in sectors.
208  * Delays are specified in milliseconds.
209  */
210 static int delay_ctr(struct dm_target *ti, unsigned int argc, char **argv)
211 {
212 	struct delay_c *dc;
213 	int ret;
214 	unsigned int max_delay;
215 
216 	if (argc != 3 && argc != 6 && argc != 9) {
217 		ti->error = "Requires exactly 3, 6 or 9 arguments";
218 		return -EINVAL;
219 	}
220 
221 	dc = kzalloc(sizeof(*dc), GFP_KERNEL);
222 	if (!dc) {
223 		ti->error = "Cannot allocate context";
224 		return -ENOMEM;
225 	}
226 
227 	ti->private = dc;
228 	INIT_LIST_HEAD(&dc->delayed_bios);
229 	mutex_init(&dc->timer_lock);
230 	dc->may_delay = true;
231 	dc->argc = argc;
232 
233 	ret = delay_class_ctr(ti, &dc->read, argv);
234 	if (ret)
235 		goto bad;
236 	max_delay = dc->read.delay;
237 
238 	if (argc == 3) {
239 		ret = delay_class_ctr(ti, &dc->write, argv);
240 		if (ret)
241 			goto bad;
242 		ret = delay_class_ctr(ti, &dc->flush, argv);
243 		if (ret)
244 			goto bad;
245 		max_delay = max(max_delay, dc->write.delay);
246 		max_delay = max(max_delay, dc->flush.delay);
247 		goto out;
248 	}
249 
250 	ret = delay_class_ctr(ti, &dc->write, argv + 3);
251 	if (ret)
252 		goto bad;
253 	if (argc == 6) {
254 		ret = delay_class_ctr(ti, &dc->flush, argv + 3);
255 		if (ret)
256 			goto bad;
257 		max_delay = max(max_delay, dc->flush.delay);
258 		goto out;
259 	}
260 
261 	ret = delay_class_ctr(ti, &dc->flush, argv + 6);
262 	if (ret)
263 		goto bad;
264 	max_delay = max(max_delay, dc->flush.delay);
265 
266 out:
267 	if (max_delay < 50) {
268 		/*
269 		 * In case of small requested delays, use kthread instead of
270 		 * timers and workqueue to achieve better latency.
271 		 */
272 		dc->worker = kthread_run(&flush_worker_fn, dc, "dm-delay-flush-worker");
273 		if (IS_ERR(dc->worker)) {
274 			ret = PTR_ERR(dc->worker);
275 			dc->worker = NULL;
276 			goto bad;
277 		}
278 	} else {
279 		timer_setup(&dc->delay_timer, handle_delayed_timer, 0);
280 		INIT_WORK(&dc->flush_expired_bios, flush_expired_bios);
281 		dc->kdelayd_wq = alloc_workqueue("kdelayd", WQ_MEM_RECLAIM, 0);
282 		if (!dc->kdelayd_wq) {
283 			ret = -EINVAL;
284 			DMERR("Couldn't start kdelayd");
285 			goto bad;
286 		}
287 	}
288 
289 	ti->num_flush_bios = 1;
290 	ti->num_discard_bios = 1;
291 	ti->accounts_remapped_io = true;
292 	ti->per_io_data_size = sizeof(struct dm_delay_info);
293 	return 0;
294 
295 bad:
296 	delay_dtr(ti);
297 	return ret;
298 }
299 
300 static int delay_bio(struct delay_c *dc, struct delay_class *c, struct bio *bio)
301 {
302 	struct dm_delay_info *delayed;
303 	unsigned long expires = 0;
304 
305 	if (!c->delay)
306 		return DM_MAPIO_REMAPPED;
307 
308 	delayed = dm_per_bio_data(bio, sizeof(struct dm_delay_info));
309 
310 	delayed->context = dc;
311 	delayed->expires = expires = jiffies + msecs_to_jiffies(c->delay);
312 
313 	mutex_lock(&delayed_bios_lock);
314 	if (unlikely(!dc->may_delay)) {
315 		mutex_unlock(&delayed_bios_lock);
316 		return DM_MAPIO_REMAPPED;
317 	}
318 	c->ops++;
319 	list_add_tail(&delayed->list, &dc->delayed_bios);
320 	mutex_unlock(&delayed_bios_lock);
321 
322 	if (delay_is_fast(dc))
323 		wake_up_process(dc->worker);
324 	else
325 		queue_timeout(dc, expires);
326 
327 	return DM_MAPIO_SUBMITTED;
328 }
329 
330 static void delay_presuspend(struct dm_target *ti)
331 {
332 	struct delay_c *dc = ti->private;
333 
334 	mutex_lock(&delayed_bios_lock);
335 	dc->may_delay = false;
336 	mutex_unlock(&delayed_bios_lock);
337 
338 	if (!delay_is_fast(dc))
339 		timer_delete(&dc->delay_timer);
340 	flush_delayed_bios(dc, true);
341 }
342 
343 static void delay_resume(struct dm_target *ti)
344 {
345 	struct delay_c *dc = ti->private;
346 
347 	dc->may_delay = true;
348 }
349 
350 static int delay_map(struct dm_target *ti, struct bio *bio)
351 {
352 	struct delay_c *dc = ti->private;
353 	struct delay_class *c;
354 	struct dm_delay_info *delayed = dm_per_bio_data(bio, sizeof(struct dm_delay_info));
355 
356 	if (bio_data_dir(bio) == WRITE) {
357 		if (unlikely(bio->bi_opf & REQ_PREFLUSH))
358 			c = &dc->flush;
359 		else
360 			c = &dc->write;
361 	} else {
362 		c = &dc->read;
363 	}
364 	delayed->class = c;
365 	bio_set_dev(bio, c->dev->bdev);
366 	bio->bi_iter.bi_sector = c->start + dm_target_offset(ti, bio->bi_iter.bi_sector);
367 
368 	return delay_bio(dc, c, bio);
369 }
370 
371 #define DMEMIT_DELAY_CLASS(c) \
372 	DMEMIT("%s %llu %u", (c)->dev->name, (unsigned long long)(c)->start, (c)->delay)
373 
374 static void delay_status(struct dm_target *ti, status_type_t type,
375 			 unsigned int status_flags, char *result, unsigned int maxlen)
376 {
377 	struct delay_c *dc = ti->private;
378 	int sz = 0;
379 
380 	switch (type) {
381 	case STATUSTYPE_INFO:
382 		DMEMIT("%u %u %u", dc->read.ops, dc->write.ops, dc->flush.ops);
383 		break;
384 
385 	case STATUSTYPE_TABLE:
386 		DMEMIT_DELAY_CLASS(&dc->read);
387 		if (dc->argc >= 6) {
388 			DMEMIT(" ");
389 			DMEMIT_DELAY_CLASS(&dc->write);
390 		}
391 		if (dc->argc >= 9) {
392 			DMEMIT(" ");
393 			DMEMIT_DELAY_CLASS(&dc->flush);
394 		}
395 		break;
396 
397 	case STATUSTYPE_IMA:
398 		*result = '\0';
399 		break;
400 	}
401 }
402 
403 static int delay_iterate_devices(struct dm_target *ti,
404 				 iterate_devices_callout_fn fn, void *data)
405 {
406 	struct delay_c *dc = ti->private;
407 	int ret = 0;
408 
409 	ret = fn(ti, dc->read.dev, dc->read.start, ti->len, data);
410 	if (ret)
411 		goto out;
412 	ret = fn(ti, dc->write.dev, dc->write.start, ti->len, data);
413 	if (ret)
414 		goto out;
415 	ret = fn(ti, dc->flush.dev, dc->flush.start, ti->len, data);
416 	if (ret)
417 		goto out;
418 
419 out:
420 	return ret;
421 }
422 
423 static struct target_type delay_target = {
424 	.name	     = "delay",
425 	.version     = {1, 4, 0},
426 	.features    = DM_TARGET_PASSES_INTEGRITY,
427 	.module      = THIS_MODULE,
428 	.ctr	     = delay_ctr,
429 	.dtr	     = delay_dtr,
430 	.map	     = delay_map,
431 	.presuspend  = delay_presuspend,
432 	.resume	     = delay_resume,
433 	.status	     = delay_status,
434 	.iterate_devices = delay_iterate_devices,
435 };
436 module_dm(delay);
437 
438 MODULE_DESCRIPTION(DM_NAME " delay target");
439 MODULE_AUTHOR("Heinz Mauelshagen <[email protected]>");
440 MODULE_LICENSE("GPL");
441