1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2005-2007 Red Hat GmbH
5 * A target that delays reads and/or writes and can send
6 * them to different devices.
8 * This file is released under the GPL.
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>
18 #include <linux/device-mapper.h>
20 #define DM_MSG_PREFIX "delay"
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;
38 struct delay_class read;
39 struct delay_class write;
40 struct delay_class flush;
45 struct dm_delay_info {
46 struct delay_c *context;
47 struct delay_class *class;
48 struct list_head list;
49 unsigned long expires;
52 static DEFINE_MUTEX(delayed_bios_lock);
54 static void handle_delayed_timer(struct timer_list *t)
56 struct delay_c *dc = from_timer(dc, t, delay_timer);
58 queue_work(dc->kdelayd_wq, &dc->flush_expired_bios);
61 static void queue_timeout(struct delay_c *dc, unsigned long expires)
63 mutex_lock(&dc->timer_lock);
65 if (!timer_pending(&dc->delay_timer) || expires < dc->delay_timer.expires)
66 mod_timer(&dc->delay_timer, expires);
68 mutex_unlock(&dc->timer_lock);
71 static inline bool delay_is_fast(struct delay_c *dc)
76 static void flush_bios(struct bio *bio)
83 dm_submit_bio_remap(bio, NULL);
88 static void flush_delayed_bios(struct delay_c *dc, bool flush_all)
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);
96 mutex_lock(&delayed_bios_lock);
97 list_for_each_entry_safe(delayed, next, &dc->delayed_bios, list) {
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--;
108 if (!delay_is_fast(dc)) {
111 next_expires = delayed->expires;
113 next_expires = min(next_expires, delayed->expires);
117 mutex_unlock(&delayed_bios_lock);
120 queue_timeout(dc, next_expires);
122 flush_bios(bio_list_get(&flush_bio_list));
125 static int flush_worker_fn(void *data)
127 struct delay_c *dc = data;
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);
137 mutex_unlock(&delayed_bios_lock);
145 static void flush_expired_bios(struct work_struct *work)
149 dc = container_of(work, struct delay_c, flush_expired_bios);
150 flush_delayed_bios(dc, false);
153 static void delay_dtr(struct dm_target *ti)
155 struct delay_c *dc = ti->private;
158 destroy_workqueue(dc->kdelayd_wq);
161 dm_put_device(ti, dc->read.dev);
163 dm_put_device(ti, dc->write.dev);
165 dm_put_device(ti, dc->flush.dev);
167 kthread_stop(dc->worker);
169 mutex_destroy(&dc->timer_lock);
174 static int delay_class_ctr(struct dm_target *ti, struct delay_class *c, char **argv)
177 unsigned long long tmpll;
180 if (sscanf(argv[1], "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
181 ti->error = "Invalid device sector";
186 if (sscanf(argv[2], "%u%c", &c->delay, &dummy) != 1) {
187 ti->error = "Invalid delay";
191 ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &c->dev);
193 ti->error = "Device lookup failed";
201 * Mapping parameters:
202 * <device> <offset> <delay> [<write_device> <write_offset> <write_delay>]
204 * With separate write parameters, the first set is only used for reads.
205 * Offsets are specified in sectors.
206 * Delays are specified in milliseconds.
208 static int delay_ctr(struct dm_target *ti, unsigned int argc, char **argv)
212 unsigned int max_delay;
214 if (argc != 3 && argc != 6 && argc != 9) {
215 ti->error = "Requires exactly 3, 6 or 9 arguments";
219 dc = kzalloc(sizeof(*dc), GFP_KERNEL);
221 ti->error = "Cannot allocate context";
226 INIT_LIST_HEAD(&dc->delayed_bios);
227 mutex_init(&dc->timer_lock);
228 dc->may_delay = true;
231 ret = delay_class_ctr(ti, &dc->read, argv);
234 max_delay = dc->read.delay;
237 ret = delay_class_ctr(ti, &dc->write, argv);
240 ret = delay_class_ctr(ti, &dc->flush, argv);
243 max_delay = max(max_delay, dc->write.delay);
244 max_delay = max(max_delay, dc->flush.delay);
248 ret = delay_class_ctr(ti, &dc->write, argv + 3);
252 ret = delay_class_ctr(ti, &dc->flush, argv + 3);
255 max_delay = max(max_delay, dc->flush.delay);
259 ret = delay_class_ctr(ti, &dc->flush, argv + 6);
262 max_delay = max(max_delay, dc->flush.delay);
265 if (max_delay < 50) {
267 * In case of small requested delays, use kthread instead of
268 * timers and workqueue to achieve better latency.
270 dc->worker = kthread_create(&flush_worker_fn, dc,
271 "dm-delay-flush-worker");
272 if (IS_ERR(dc->worker)) {
273 ret = PTR_ERR(dc->worker);
278 timer_setup(&dc->delay_timer, handle_delayed_timer, 0);
279 INIT_WORK(&dc->flush_expired_bios, flush_expired_bios);
280 dc->kdelayd_wq = alloc_workqueue("kdelayd", WQ_MEM_RECLAIM, 0);
281 if (!dc->kdelayd_wq) {
283 DMERR("Couldn't start kdelayd");
288 ti->num_flush_bios = 1;
289 ti->num_discard_bios = 1;
290 ti->accounts_remapped_io = true;
291 ti->per_io_data_size = sizeof(struct dm_delay_info);
299 static int delay_bio(struct delay_c *dc, struct delay_class *c, struct bio *bio)
301 struct dm_delay_info *delayed;
302 unsigned long expires = 0;
305 return DM_MAPIO_REMAPPED;
307 delayed = dm_per_bio_data(bio, sizeof(struct dm_delay_info));
309 delayed->context = dc;
310 delayed->expires = expires = jiffies + msecs_to_jiffies(c->delay);
312 mutex_lock(&delayed_bios_lock);
313 if (unlikely(!dc->may_delay)) {
314 mutex_unlock(&delayed_bios_lock);
315 return DM_MAPIO_REMAPPED;
318 list_add_tail(&delayed->list, &dc->delayed_bios);
319 mutex_unlock(&delayed_bios_lock);
321 if (delay_is_fast(dc))
322 wake_up_process(dc->worker);
324 queue_timeout(dc, expires);
326 return DM_MAPIO_SUBMITTED;
329 static void delay_presuspend(struct dm_target *ti)
331 struct delay_c *dc = ti->private;
333 mutex_lock(&delayed_bios_lock);
334 dc->may_delay = false;
335 mutex_unlock(&delayed_bios_lock);
337 if (!delay_is_fast(dc))
338 del_timer_sync(&dc->delay_timer);
339 flush_delayed_bios(dc, true);
342 static void delay_resume(struct dm_target *ti)
344 struct delay_c *dc = ti->private;
346 dc->may_delay = true;
349 static int delay_map(struct dm_target *ti, struct bio *bio)
351 struct delay_c *dc = ti->private;
352 struct delay_class *c;
353 struct dm_delay_info *delayed = dm_per_bio_data(bio, sizeof(struct dm_delay_info));
355 if (bio_data_dir(bio) == WRITE) {
356 if (unlikely(bio->bi_opf & REQ_PREFLUSH))
364 bio_set_dev(bio, c->dev->bdev);
365 bio->bi_iter.bi_sector = c->start + dm_target_offset(ti, bio->bi_iter.bi_sector);
367 return delay_bio(dc, c, bio);
370 #define DMEMIT_DELAY_CLASS(c) \
371 DMEMIT("%s %llu %u", (c)->dev->name, (unsigned long long)(c)->start, (c)->delay)
373 static void delay_status(struct dm_target *ti, status_type_t type,
374 unsigned int status_flags, char *result, unsigned int maxlen)
376 struct delay_c *dc = ti->private;
380 case STATUSTYPE_INFO:
381 DMEMIT("%u %u %u", dc->read.ops, dc->write.ops, dc->flush.ops);
384 case STATUSTYPE_TABLE:
385 DMEMIT_DELAY_CLASS(&dc->read);
388 DMEMIT_DELAY_CLASS(&dc->write);
392 DMEMIT_DELAY_CLASS(&dc->flush);
402 static int delay_iterate_devices(struct dm_target *ti,
403 iterate_devices_callout_fn fn, void *data)
405 struct delay_c *dc = ti->private;
408 ret = fn(ti, dc->read.dev, dc->read.start, ti->len, data);
411 ret = fn(ti, dc->write.dev, dc->write.start, ti->len, data);
414 ret = fn(ti, dc->flush.dev, dc->flush.start, ti->len, data);
422 static struct target_type delay_target = {
424 .version = {1, 4, 0},
425 .features = DM_TARGET_PASSES_INTEGRITY,
426 .module = THIS_MODULE,
430 .presuspend = delay_presuspend,
431 .resume = delay_resume,
432 .status = delay_status,
433 .iterate_devices = delay_iterate_devices,
437 MODULE_DESCRIPTION(DM_NAME " delay target");
438 MODULE_AUTHOR("Heinz Mauelshagen <mauelshagen@redhat.com>");
439 MODULE_LICENSE("GPL");