dm-rq.c 15.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (C) 2016 Red Hat, Inc. All rights reserved.
 *
 * This file is released under the GPL.
 */

#include "dm-core.h"
#include "dm-rq.h"

#include <linux/elevator.h> /* for rq_end_sector() */
#include <linux/blk-mq.h>

#define DM_MSG_PREFIX "core-rq"

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * One of these is allocated per request.
 */
struct dm_rq_target_io {
	struct mapped_device *md;
	struct dm_target *ti;
	struct request *orig, *clone;
	struct kthread_work work;
	blk_status_t error;
	union map_info info;
	struct dm_stats_aux stats_aux;
	unsigned long duration_jiffies;
	unsigned n_sectors;
	unsigned completed;
};

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#define DM_MQ_NR_HW_QUEUES 1
#define DM_MQ_QUEUE_DEPTH 2048
static unsigned dm_mq_nr_hw_queues = DM_MQ_NR_HW_QUEUES;
static unsigned dm_mq_queue_depth = DM_MQ_QUEUE_DEPTH;

/*
 * Request-based DM's mempools' reserved IOs set by the user.
 */
#define RESERVED_REQUEST_BASED_IOS	256
static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;

unsigned dm_get_reserved_rq_based_ios(void)
{
	return __dm_get_module_param(&reserved_rq_based_ios,
				     RESERVED_REQUEST_BASED_IOS, DM_RESERVED_MAX_IOS);
}
EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);

static unsigned dm_get_blk_mq_nr_hw_queues(void)
{
	return __dm_get_module_param(&dm_mq_nr_hw_queues, 1, 32);
}

static unsigned dm_get_blk_mq_queue_depth(void)
{
	return __dm_get_module_param(&dm_mq_queue_depth,
				     DM_MQ_QUEUE_DEPTH, BLK_MQ_MAX_DEPTH);
}

int dm_request_based(struct mapped_device *md)
{
J
Jens Axboe 已提交
62
	return queue_is_mq(md->queue);
63 64
}

65
void dm_start_queue(struct request_queue *q)
66
{
67
	blk_mq_unquiesce_queue(q);
68 69 70
	blk_mq_kick_requeue_list(q);
}

71
void dm_stop_queue(struct request_queue *q)
72
{
73
	blk_mq_quiesce_queue(q);
74 75 76 77 78 79 80 81 82 83 84
}

/*
 * Partial completion handling for request-based dm
 */
static void end_clone_bio(struct bio *clone)
{
	struct dm_rq_clone_bio_info *info =
		container_of(clone, struct dm_rq_clone_bio_info, clone);
	struct dm_rq_target_io *tio = info->tio;
	unsigned int nr_bytes = info->orig->bi_iter.bi_size;
85
	blk_status_t error = clone->bi_status;
86
	bool is_last = !clone->bi_next;
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

	bio_put(clone);

	if (tio->error)
		/*
		 * An error has already been detected on the request.
		 * Once error occurred, just let clone->end_io() handle
		 * the remainder.
		 */
		return;
	else if (error) {
		/*
		 * Don't notice the error to the upper layer yet.
		 * The error handling decision is made by the target driver,
		 * when the request is completed.
		 */
		tio->error = error;
104
		goto exit;
105 106 107 108 109 110
	}

	/*
	 * I/O for the bio successfully completed.
	 * Notice the data completion to the upper layer.
	 */
111
	tio->completed += nr_bytes;
112 113 114

	/*
	 * Update the original request.
115
	 * Do not use blk_mq_end_request() here, because it may complete
116 117
	 * the original request before the clone, and break the ordering.
	 */
118 119 120
	if (is_last)
 exit:
		blk_update_request(tio->orig, BLK_STS_OK, tio->completed);
121 122 123 124
}

static struct dm_rq_target_io *tio_from_request(struct request *rq)
{
125
	return blk_mq_rq_to_pdu(rq);
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
}

static void rq_end_stats(struct mapped_device *md, struct request *orig)
{
	if (unlikely(dm_stats_used(&md->stats))) {
		struct dm_rq_target_io *tio = tio_from_request(orig);
		tio->duration_jiffies = jiffies - tio->duration_jiffies;
		dm_stats_account_io(&md->stats, rq_data_dir(orig),
				    blk_rq_pos(orig), tio->n_sectors, true,
				    tio->duration_jiffies, &tio->stats_aux);
	}
}

/*
 * Don't touch any member of the md after calling this function because
 * the md may be freed in dm_put() at the end of this function.
 * Or do dm_get() before calling this function and dm_put() later.
 */
144
static void rq_completed(struct mapped_device *md)
145 146 147 148 149 150 151 152 153 154 155 156
{
	/*
	 * dm_put() must be at the end of this function. See the comment above
	 */
	dm_put(md);
}

/*
 * Complete the clone and the original request.
 * Must be called without clone's queue lock held,
 * see end_clone_request() for more details.
 */
157
static void dm_end_request(struct request *clone, blk_status_t error)
158 159 160 161 162
{
	struct dm_rq_target_io *tio = clone->end_io_data;
	struct mapped_device *md = tio->md;
	struct request *rq = tio->orig;

163
	blk_rq_unprep_clone(clone);
164
	tio->ti->type->release_clone_rq(clone, NULL);
165 166

	rq_end_stats(md, rq);
167
	blk_mq_end_request(rq, error);
168
	rq_completed(md);
169 170
}

171
static void __dm_mq_kick_requeue_list(struct request_queue *q, unsigned long msecs)
172
{
173
	blk_mq_delay_kick_requeue_list(q, msecs);
174 175
}

176 177
void dm_mq_kick_requeue_list(struct mapped_device *md)
{
178
	__dm_mq_kick_requeue_list(md->queue, 0);
179 180 181 182 183
}
EXPORT_SYMBOL(dm_mq_kick_requeue_list);

static void dm_mq_delay_requeue_request(struct request *rq, unsigned long msecs)
{
184
	blk_mq_requeue_request(rq, false);
185 186 187
	__dm_mq_kick_requeue_list(rq->q, msecs);
}

188
static void dm_requeue_original_request(struct dm_rq_target_io *tio, bool delay_requeue)
189
{
190 191
	struct mapped_device *md = tio->md;
	struct request *rq = tio->orig;
192
	unsigned long delay_ms = delay_requeue ? 100 : 0;
193 194

	rq_end_stats(md, rq);
195 196
	if (tio->clone) {
		blk_rq_unprep_clone(tio->clone);
197
		tio->ti->type->release_clone_rq(tio->clone, NULL);
198
	}
199

200
	dm_mq_delay_requeue_request(rq, delay_ms);
201
	rq_completed(md);
202 203
}

204
static void dm_done(struct request *clone, blk_status_t error, bool mapped)
205
{
206
	int r = DM_ENDIO_DONE;
207 208 209 210 211 212 213 214 215 216
	struct dm_rq_target_io *tio = clone->end_io_data;
	dm_request_endio_fn rq_end_io = NULL;

	if (tio->ti) {
		rq_end_io = tio->ti->type->rq_end_io;

		if (mapped && rq_end_io)
			r = rq_end_io(tio->ti, clone, error, &tio->info);
	}

217
	if (unlikely(error == BLK_STS_TARGET)) {
218 219 220 221 222
		if (req_op(clone) == REQ_OP_DISCARD &&
		    !clone->q->limits.max_discard_sectors)
			disable_discard(tio->md);
		else if (req_op(clone) == REQ_OP_WRITE_SAME &&
			 !clone->q->limits.max_write_same_sectors)
223
			disable_write_same(tio->md);
224 225
		else if (req_op(clone) == REQ_OP_WRITE_ZEROES &&
			 !clone->q->limits.max_write_zeroes_sectors)
226 227
			disable_write_zeroes(tio->md);
	}
228

229 230
	switch (r) {
	case DM_ENDIO_DONE:
231
		/* The target wants to complete the I/O */
232 233 234
		dm_end_request(clone, error);
		break;
	case DM_ENDIO_INCOMPLETE:
235 236
		/* The target will handle the I/O */
		return;
237
	case DM_ENDIO_REQUEUE:
238
		/* The target wants to requeue the I/O */
239
		dm_requeue_original_request(tio, false);
240
		break;
241 242 243 244
	case DM_ENDIO_DELAY_REQUEUE:
		/* The target wants to requeue the I/O after a delay */
		dm_requeue_original_request(tio, true);
		break;
245
	default:
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
		DMWARN("unimplemented target endio return value: %d", r);
		BUG();
	}
}

/*
 * Request completion handler for request-based dm
 */
static void dm_softirq_done(struct request *rq)
{
	bool mapped = true;
	struct dm_rq_target_io *tio = tio_from_request(rq);
	struct request *clone = tio->clone;

	if (!clone) {
261 262 263
		struct mapped_device *md = tio->md;

		rq_end_stats(md, rq);
264
		blk_mq_end_request(rq, tio->error);
265
		rq_completed(md);
266 267 268
		return;
	}

269
	if (rq->rq_flags & RQF_FAILED)
270 271 272 273 274 275 276 277 278
		mapped = false;

	dm_done(clone, tio->error, mapped);
}

/*
 * Complete the clone and the original request with the error status
 * through softirq context.
 */
279
static void dm_complete_request(struct request *rq, blk_status_t error)
280 281 282 283
{
	struct dm_rq_target_io *tio = tio_from_request(rq);

	tio->error = error;
284 285
	if (likely(!blk_should_fake_timeout(rq->q)))
		blk_mq_complete_request(rq);
286 287 288 289 290 291
}

/*
 * Complete the not-mapped clone and the original request with the error status
 * through softirq context.
 * Target's rq_end_io() function isn't called.
292
 * This may be used when the target's clone_and_map_rq() function fails.
293
 */
294
static void dm_kill_unmapped_request(struct request *rq, blk_status_t error)
295
{
296
	rq->rq_flags |= RQF_FAILED;
297 298 299
	dm_complete_request(rq, error);
}

300
static void end_clone_request(struct request *clone, blk_status_t error)
301 302 303 304 305 306
{
	struct dm_rq_target_io *tio = clone->end_io_data;

	dm_complete_request(tio->orig, error);
}

307
static blk_status_t dm_dispatch_clone_request(struct request *clone, struct request *rq)
308
{
309
	blk_status_t r;
310 311

	if (blk_queue_io_stat(clone->q))
312
		clone->rq_flags |= RQF_IO_STAT;
313

314
	clone->start_time_ns = ktime_get_ns();
315
	r = blk_insert_cloned_request(clone->q, clone);
316
	if (r != BLK_STS_OK && r != BLK_STS_RESOURCE && r != BLK_STS_DEV_RESOURCE)
317 318
		/* must complete clone in terms of original request */
		dm_complete_request(rq, r);
319
	return r;
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
}

static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
				 void *data)
{
	struct dm_rq_target_io *tio = data;
	struct dm_rq_clone_bio_info *info =
		container_of(bio, struct dm_rq_clone_bio_info, clone);

	info->orig = bio_orig;
	info->tio = tio;
	bio->bi_end_io = end_clone_bio;

	return 0;
}

static int setup_clone(struct request *clone, struct request *rq,
		       struct dm_rq_target_io *tio, gfp_t gfp_mask)
{
	int r;

341
	r = blk_rq_prep_clone(clone, rq, &tio->md->bs, gfp_mask,
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
			      dm_rq_bio_constructor, tio);
	if (r)
		return r;

	clone->end_io = end_clone_request;
	clone->end_io_data = tio;

	tio->clone = clone;

	return 0;
}

static void init_tio(struct dm_rq_target_io *tio, struct request *rq,
		     struct mapped_device *md)
{
	tio->md = md;
	tio->ti = NULL;
	tio->clone = NULL;
	tio->orig = rq;
	tio->error = 0;
362
	tio->completed = 0;
363 364 365 366 367 368 369 370 371 372 373
	/*
	 * Avoid initializing info for blk-mq; it passes
	 * target-specific data through info.ptr
	 * (see: dm_mq_init_request)
	 */
	if (!md->init_tio_pdu)
		memset(&tio->info, 0, sizeof(tio->info));
}

/*
 * Returns:
374 375
 * DM_MAPIO_*       : the request has been processed as indicated
 * DM_MAPIO_REQUEUE : the original request needs to be immediately requeued
376 377
 * < 0              : the request was completed due to failure
 */
378
static int map_request(struct dm_rq_target_io *tio)
379 380 381
{
	int r;
	struct dm_target *ti = tio->ti;
382 383
	struct mapped_device *md = tio->md;
	struct request *rq = tio->orig;
384
	struct request *clone = NULL;
385
	blk_status_t ret;
386

387
	r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone);
388 389 390 391 392
	switch (r) {
	case DM_MAPIO_SUBMITTED:
		/* The target has taken the I/O to submit by itself later */
		break;
	case DM_MAPIO_REMAPPED:
393 394
		if (setup_clone(clone, rq, tio, GFP_ATOMIC)) {
			/* -ENOMEM */
395
			ti->type->release_clone_rq(clone, &tio->info);
396 397 398
			return DM_MAPIO_REQUEUE;
		}

399
		/* The target has remapped the I/O so dispatch it */
400
		trace_block_rq_remap(clone, disk_devt(dm_disk(md)),
401
				     blk_rq_pos(rq));
402
		ret = dm_dispatch_clone_request(clone, rq);
403
		if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE) {
404
			blk_rq_unprep_clone(clone);
M
Ming Lei 已提交
405
			blk_mq_cleanup_rq(clone);
406
			tio->ti->type->release_clone_rq(clone, &tio->info);
407
			tio->clone = NULL;
408
			return DM_MAPIO_REQUEUE;
409
		}
410 411 412
		break;
	case DM_MAPIO_REQUEUE:
		/* The target wants to requeue the I/O */
413 414 415
		break;
	case DM_MAPIO_DELAY_REQUEUE:
		/* The target wants to requeue the I/O after a delay */
416
		dm_requeue_original_request(tio, true);
417
		break;
418
	case DM_MAPIO_KILL:
419
		/* The target wants to complete the I/O */
420
		dm_kill_unmapped_request(rq, BLK_STS_IOERR);
421
		break;
422 423 424
	default:
		DMWARN("unimplemented target map return value: %d", r);
		BUG();
425 426
	}

427
	return r;
428 429
}

430 431 432 433 434 435 436 437 438 439 440 441
/* DEPRECATED: previously used for request-based merge heuristic in dm_request_fn() */
ssize_t dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device *md, char *buf)
{
	return sprintf(buf, "%u\n", 0);
}

ssize_t dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device *md,
						     const char *buf, size_t count)
{
	return count;
}

442 443
static void dm_start_request(struct mapped_device *md, struct request *orig)
{
444
	blk_mq_start_request(orig);
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464

	if (unlikely(dm_stats_used(&md->stats))) {
		struct dm_rq_target_io *tio = tio_from_request(orig);
		tio->duration_jiffies = jiffies;
		tio->n_sectors = blk_rq_sectors(orig);
		dm_stats_account_io(&md->stats, rq_data_dir(orig),
				    blk_rq_pos(orig), tio->n_sectors, false, 0,
				    &tio->stats_aux);
	}

	/*
	 * Hold the md reference here for the in-flight I/O.
	 * We can't rely on the reference count by device opener,
	 * because the device may be closed during the request completion
	 * when all bios are completed.
	 * See the comment in rq_completed() too.
	 */
	dm_get(md);
}

465 466
static int dm_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
			      unsigned int hctx_idx, unsigned int numa_node)
467
{
468
	struct mapped_device *md = set->driver_data;
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
	struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);

	/*
	 * Must initialize md member of tio, otherwise it won't
	 * be available in dm_mq_queue_rq.
	 */
	tio->md = md;

	if (md->init_tio_pdu) {
		/* target-specific per-io data is immediately after the tio */
		tio->info.ptr = tio + 1;
	}

	return 0;
}

485
static blk_status_t dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
486 487 488 489 490 491 492
			  const struct blk_mq_queue_data *bd)
{
	struct request *rq = bd->rq;
	struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
	struct mapped_device *md = tio->md;
	struct dm_target *ti = md->immutable_target;

493 494 495 496 497 498 499 500
	/*
	 * blk-mq's unquiesce may come from outside events, such as
	 * elevator switch, updating nr_requests or others, and request may
	 * come during suspend, so simply ask for blk-mq to requeue it.
	 */
	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)))
		return BLK_STS_RESOURCE;

501 502
	if (unlikely(!ti)) {
		int srcu_idx;
503 504
		struct dm_table *map = dm_get_live_table(md, &srcu_idx);

505 506 507 508 509
		ti = dm_table_find_target(map, 0);
		dm_put_live_table(md, srcu_idx);
	}

	if (ti->type->busy && ti->type->busy(ti))
510
		return BLK_STS_RESOURCE;
511 512 513 514 515 516 517 518 519 520 521 522

	dm_start_request(md, rq);

	/* Init tio using md established in .init_request */
	init_tio(tio, rq, md);

	/*
	 * Establish tio->ti before calling map_request().
	 */
	tio->ti = ti;

	/* Direct call is fine since .queue_rq allows allocations */
523
	if (map_request(tio) == DM_MAPIO_REQUEUE) {
524 525
		/* Undo dm_start_request() before requeuing */
		rq_end_stats(md, rq);
526
		rq_completed(md);
527
		return BLK_STS_RESOURCE;
528 529
	}

530
	return BLK_STS_OK;
531 532
}

533
static const struct blk_mq_ops dm_mq_ops = {
534 535 536 537 538
	.queue_rq = dm_mq_queue_rq,
	.complete = dm_softirq_done,
	.init_request = dm_mq_init_request,
};

539
int dm_mq_init_request_queue(struct mapped_device *md, struct dm_table *t)
540 541
{
	struct request_queue *q;
542
	struct dm_target *immutable_tgt;
543 544 545 546 547 548 549 550 551
	int err;

	md->tag_set = kzalloc_node(sizeof(struct blk_mq_tag_set), GFP_KERNEL, md->numa_node_id);
	if (!md->tag_set)
		return -ENOMEM;

	md->tag_set->ops = &dm_mq_ops;
	md->tag_set->queue_depth = dm_get_blk_mq_queue_depth();
	md->tag_set->numa_node = md->numa_node_id;
552
	md->tag_set->flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_STACKING;
553 554 555 556
	md->tag_set->nr_hw_queues = dm_get_blk_mq_nr_hw_queues();
	md->tag_set->driver_data = md;

	md->tag_set->cmd_size = sizeof(struct dm_rq_target_io);
557
	immutable_tgt = dm_table_get_immutable_target(t);
558 559 560 561 562 563 564 565 566 567
	if (immutable_tgt && immutable_tgt->per_io_data_size) {
		/* any target-specific per-io data is immediately after the tio */
		md->tag_set->cmd_size += immutable_tgt->per_io_data_size;
		md->init_tio_pdu = true;
	}

	err = blk_mq_alloc_tag_set(md->tag_set);
	if (err)
		goto out_kfree_tag_set;

568
	q = blk_mq_init_allocated_queue(md->tag_set, md->queue, true);
569 570 571 572 573 574 575 576 577 578 579
	if (IS_ERR(q)) {
		err = PTR_ERR(q);
		goto out_tag_set;
	}

	return 0;

out_tag_set:
	blk_mq_free_tag_set(md->tag_set);
out_kfree_tag_set:
	kfree(md->tag_set);
580
	md->tag_set = NULL;
581 582 583 584 585 586 587 588 589

	return err;
}

void dm_mq_cleanup_mapped_device(struct mapped_device *md)
{
	if (md->tag_set) {
		blk_mq_free_tag_set(md->tag_set);
		kfree(md->tag_set);
590
		md->tag_set = NULL;
591 592 593 594 595 596
	}
}

module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");

597 598
/* Unused, but preserved for userspace compatibility */
static bool use_blk_mq = true;
599 600 601 602 603 604 605 606
module_param(use_blk_mq, bool, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(use_blk_mq, "Use block multiqueue for request-based DM devices");

module_param(dm_mq_nr_hw_queues, uint, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(dm_mq_nr_hw_queues, "Number of hardware queues for request-based dm-mq devices");

module_param(dm_mq_queue_depth, uint, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(dm_mq_queue_depth, "Queue depth for request-based dm-mq devices");