blk-mq-tag.c 11.9 KB
Newer Older
1
/*
2 3 4
 * Tag allocation using scalable bitmaps. Uses active queue tracking to support
 * fairer distribution of tags between multiple submitters when a shared tag map
 * is used.
5 6 7
 *
 * Copyright (C) 2013-2014 Jens Axboe
 */
8 9
#include <linux/kernel.h>
#include <linux/module.h>
10
#include <linux/random.h>
11 12 13 14 15 16 17 18

#include <linux/blk-mq.h>
#include "blk.h"
#include "blk-mq.h"
#include "blk-mq-tag.h"

bool blk_mq_has_free_tags(struct blk_mq_tags *tags)
{
19 20 21
	if (!tags)
		return true;

22
	return sbitmap_any_bit_clear(&tags->bitmap_tags.sb);
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
}

/*
 * If a previously inactive queue goes active, bump the active user count.
 */
bool __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)
{
	if (!test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state) &&
	    !test_and_set_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
		atomic_inc(&hctx->tags->active_queues);

	return true;
}

/*
38
 * Wakeup all potentially sleeping on tags
39
 */
40
void blk_mq_tag_wakeup_all(struct blk_mq_tags *tags, bool include_reserve)
41
{
42 43 44
	sbitmap_queue_wake_all(&tags->bitmap_tags);
	if (include_reserve)
		sbitmap_queue_wake_all(&tags->breserved_tags);
45 46
}

47 48 49 50 51 52 53 54 55 56 57 58 59
/*
 * If a previously busy queue goes inactive, potential waiters could now
 * be allowed to queue. Wake them up and check.
 */
void __blk_mq_tag_idle(struct blk_mq_hw_ctx *hctx)
{
	struct blk_mq_tags *tags = hctx->tags;

	if (!test_and_clear_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
		return;

	atomic_dec(&tags->active_queues);

60
	blk_mq_tag_wakeup_all(tags, false);
61 62
}

63 64 65 66 67
/*
 * For shared tag users, we track the number of currently active users
 * and attempt to provide a fair share of the tag depth for each of them.
 */
static inline bool hctx_may_queue(struct blk_mq_hw_ctx *hctx,
68
				  struct sbitmap_queue *bt)
69 70 71 72 73 74 75 76 77 78 79
{
	unsigned int depth, users;

	if (!hctx || !(hctx->flags & BLK_MQ_F_TAG_SHARED))
		return true;
	if (!test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
		return true;

	/*
	 * Don't try dividing an ant
	 */
80
	if (bt->sb.depth == 1)
81 82 83 84 85 86 87 88 89
		return true;

	users = atomic_read(&hctx->tags->active_queues);
	if (!users)
		return true;

	/*
	 * Allow at least some tags
	 */
90
	depth = max((bt->sb.depth + users - 1) / users, 4U);
91 92 93
	return atomic_read(&hctx->nr_active) < depth;
}

S
Shaohua Li 已提交
94 95
#define BT_ALLOC_RR(tags) (tags->alloc_policy == BLK_TAG_ALLOC_RR)

96
static int __bt_get(struct blk_mq_hw_ctx *hctx, struct sbitmap_queue *bt,
S
Shaohua Li 已提交
97
		    unsigned int *tag_cache, struct blk_mq_tags *tags)
98
{
99 100
	unsigned int last_tag;
	int tag;
101

102 103 104
	if (!hctx_may_queue(hctx, bt))
		return -1;

105 106
	last_tag = *tag_cache;
	tag = sbitmap_get(&bt->sb, last_tag, BT_ALLOC_RR(tags));
107

108 109 110
	if (tag == -1) {
		*tag_cache = 0;
	} else if (tag == last_tag || unlikely(BT_ALLOC_RR(tags))) {
111
		last_tag = tag + 1;
112
		if (last_tag >= bt->sb.depth - 1)
113 114 115 116 117 118 119
			last_tag = 0;
		*tag_cache = last_tag;
	}

	return tag;
}

120
static int bt_get(struct blk_mq_alloc_data *data,
121 122 123
		  struct sbitmap_queue *bt,
		  struct blk_mq_hw_ctx *hctx,
		  unsigned int *last_tag, struct blk_mq_tags *tags)
124
{
125
	struct sbq_wait_state *ws;
126
	DEFINE_WAIT(wait);
127 128
	int tag;

S
Shaohua Li 已提交
129
	tag = __bt_get(hctx, bt, last_tag, tags);
130 131 132
	if (tag != -1)
		return tag;

133
	if (data->flags & BLK_MQ_REQ_NOWAIT)
134 135
		return -1;

136
	ws = bt_wait_ptr(bt, hctx);
137
	do {
138
		prepare_to_wait(&ws->wait, &wait, TASK_UNINTERRUPTIBLE);
139

S
Shaohua Li 已提交
140
		tag = __bt_get(hctx, bt, last_tag, tags);
141 142 143
		if (tag != -1)
			break;

B
Bart Van Assche 已提交
144 145 146
		/*
		 * We're out of tags on this hardware queue, kick any
		 * pending IO submits before going to sleep waiting for
147 148
		 * some to complete. Note that hctx can be NULL here for
		 * reserved tag allocation.
B
Bart Van Assche 已提交
149
		 */
150 151
		if (hctx)
			blk_mq_run_hw_queue(hctx, false);
B
Bart Van Assche 已提交
152

153 154 155 156
		/*
		 * Retry tag allocation after running the hardware queue,
		 * as running the queue may also have found completions.
		 */
S
Shaohua Li 已提交
157
		tag = __bt_get(hctx, bt, last_tag, tags);
158 159 160
		if (tag != -1)
			break;

161 162
		blk_mq_put_ctx(data->ctx);

163
		io_schedule();
164 165 166 167

		data->ctx = blk_mq_get_ctx(data->q);
		data->hctx = data->q->mq_ops->map_queue(data->q,
				data->ctx->cpu);
168
		if (data->flags & BLK_MQ_REQ_RESERVED) {
169 170 171 172 173 174
			bt = &data->hctx->tags->breserved_tags;
		} else {
			last_tag = &data->ctx->last_tag;
			hctx = data->hctx;
			bt = &hctx->tags->bitmap_tags;
		}
175 176
		finish_wait(&ws->wait, &wait);
		ws = bt_wait_ptr(bt, hctx);
177 178
	} while (1);

179
	finish_wait(&ws->wait, &wait);
180 181 182
	return tag;
}

183
static unsigned int __blk_mq_get_tag(struct blk_mq_alloc_data *data)
184 185 186
{
	int tag;

187
	tag = bt_get(data, &data->hctx->tags->bitmap_tags, data->hctx,
S
Shaohua Li 已提交
188
			&data->ctx->last_tag, data->hctx->tags);
189
	if (tag >= 0)
190
		return tag + data->hctx->tags->nr_reserved_tags;
191 192

	return BLK_MQ_TAG_FAIL;
193 194
}

195
static unsigned int __blk_mq_get_reserved_tag(struct blk_mq_alloc_data *data)
196
{
197
	int tag, zero = 0;
198

199
	if (unlikely(!data->hctx->tags->nr_reserved_tags)) {
200 201 202 203
		WARN_ON_ONCE(1);
		return BLK_MQ_TAG_FAIL;
	}

S
Shaohua Li 已提交
204 205
	tag = bt_get(data, &data->hctx->tags->breserved_tags, NULL, &zero,
		data->hctx->tags);
206 207
	if (tag < 0)
		return BLK_MQ_TAG_FAIL;
208

209 210 211
	return tag;
}

212
unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data)
213
{
214 215 216
	if (data->flags & BLK_MQ_REQ_RESERVED)
		return __blk_mq_get_reserved_tag(data);
	return __blk_mq_get_tag(data);
217 218
}

219
void blk_mq_put_tag(struct blk_mq_hw_ctx *hctx, unsigned int tag,
220
		    unsigned int *last_tag)
221
{
222 223
	struct blk_mq_tags *tags = hctx->tags;

224 225 226
	if (tag >= tags->nr_reserved_tags) {
		const int real_tag = tag - tags->nr_reserved_tags;

J
Jens Axboe 已提交
227
		BUG_ON(real_tag >= tags->nr_tags);
228
		sbitmap_queue_clear(&tags->bitmap_tags, real_tag);
S
Shaohua Li 已提交
229 230
		if (likely(tags->alloc_policy == BLK_TAG_ALLOC_FIFO))
			*last_tag = real_tag;
J
Jens Axboe 已提交
231 232
	} else {
		BUG_ON(tag >= tags->nr_reserved_tags);
233
		sbitmap_queue_clear(&tags->breserved_tags, tag);
J
Jens Axboe 已提交
234
	}
235 236
}

237 238 239 240 241 242 243 244
struct bt_iter_data {
	struct blk_mq_hw_ctx *hctx;
	busy_iter_fn *fn;
	void *data;
	bool reserved;
};

static bool bt_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
245
{
246 247 248 249
	struct bt_iter_data *iter_data = data;
	struct blk_mq_hw_ctx *hctx = iter_data->hctx;
	struct blk_mq_tags *tags = hctx->tags;
	bool reserved = iter_data->reserved;
250
	struct request *rq;
251

252 253 254
	if (!reserved)
		bitnr += tags->nr_reserved_tags;
	rq = tags->rqs[bitnr];
255

256 257 258 259
	if (rq->q == hctx->queue)
		iter_data->fn(hctx, rq, iter_data->data, reserved);
	return true;
}
260

261 262 263 264 265 266 267 268 269 270 271
static void bt_for_each(struct blk_mq_hw_ctx *hctx, struct sbitmap_queue *bt,
			busy_iter_fn *fn, void *data, bool reserved)
{
	struct bt_iter_data iter_data = {
		.hctx = hctx,
		.fn = fn,
		.data = data,
		.reserved = reserved,
	};

	sbitmap_for_each_set(&bt->sb, bt_iter, &iter_data);
272 273
}

274 275 276 277 278 279 280 281
struct bt_tags_iter_data {
	struct blk_mq_tags *tags;
	busy_tag_iter_fn *fn;
	void *data;
	bool reserved;
};

static bool bt_tags_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
K
Keith Busch 已提交
282
{
283 284 285
	struct bt_tags_iter_data *iter_data = data;
	struct blk_mq_tags *tags = iter_data->tags;
	bool reserved = iter_data->reserved;
K
Keith Busch 已提交
286 287
	struct request *rq;

288 289 290
	if (!reserved)
		bitnr += tags->nr_reserved_tags;
	rq = tags->rqs[bitnr];
K
Keith Busch 已提交
291

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
	iter_data->fn(rq, iter_data->data, reserved);
	return true;
}

static void bt_tags_for_each(struct blk_mq_tags *tags, struct sbitmap_queue *bt,
			     busy_tag_iter_fn *fn, void *data, bool reserved)
{
	struct bt_tags_iter_data iter_data = {
		.tags = tags,
		.fn = fn,
		.data = data,
		.reserved = reserved,
	};

	if (tags->rqs)
		sbitmap_for_each_set(&bt->sb, bt_tags_iter, &iter_data);
K
Keith Busch 已提交
308 309
}

310 311
static void blk_mq_all_tag_busy_iter(struct blk_mq_tags *tags,
		busy_tag_iter_fn *fn, void *priv)
K
Keith Busch 已提交
312 313
{
	if (tags->nr_reserved_tags)
314 315
		bt_tags_for_each(tags, &tags->breserved_tags, fn, priv, true);
	bt_tags_for_each(tags, &tags->bitmap_tags, fn, priv, false);
K
Keith Busch 已提交
316 317
}

318 319 320 321 322 323 324 325 326 327 328 329
void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
		busy_tag_iter_fn *fn, void *priv)
{
	int i;

	for (i = 0; i < tagset->nr_hw_queues; i++) {
		if (tagset->tags && tagset->tags[i])
			blk_mq_all_tag_busy_iter(tagset->tags[i], fn, priv);
	}
}
EXPORT_SYMBOL(blk_mq_tagset_busy_iter);

330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
int blk_mq_reinit_tagset(struct blk_mq_tag_set *set)
{
	int i, j, ret = 0;

	if (!set->ops->reinit_request)
		goto out;

	for (i = 0; i < set->nr_hw_queues; i++) {
		struct blk_mq_tags *tags = set->tags[i];

		for (j = 0; j < tags->nr_tags; j++) {
			if (!tags->rqs[j])
				continue;

			ret = set->ops->reinit_request(set->driver_data,
						tags->rqs[j]);
			if (ret)
				goto out;
		}
	}

out:
	return ret;
}
EXPORT_SYMBOL_GPL(blk_mq_reinit_tagset);

356
void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_iter_fn *fn,
357
		void *priv)
358
{
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
	struct blk_mq_hw_ctx *hctx;
	int i;


	queue_for_each_hw_ctx(q, hctx, i) {
		struct blk_mq_tags *tags = hctx->tags;

		/*
		 * If not software queues are currently mapped to this
		 * hardware queue, there's nothing to check
		 */
		if (!blk_mq_hw_queue_mapped(hctx))
			continue;

		if (tags->nr_reserved_tags)
374 375
			bt_for_each(hctx, &tags->breserved_tags, fn, priv, true);
		bt_for_each(hctx, &tags->bitmap_tags, fn, priv, false);
376 377 378 379
	}

}

380
static unsigned int bt_unused_tags(const struct sbitmap_queue *bt)
381
{
382
	return bt->sb.depth - sbitmap_weight(&bt->sb);
383 384
}

385
static int bt_alloc(struct sbitmap_queue *bt, unsigned int depth, int node)
386
{
387
	return sbitmap_queue_init_node(bt, depth, -1, GFP_KERNEL, node);
388 389 390
}

static struct blk_mq_tags *blk_mq_init_bitmap_tags(struct blk_mq_tags *tags,
S
Shaohua Li 已提交
391
						   int node, int alloc_policy)
392 393 394
{
	unsigned int depth = tags->nr_tags - tags->nr_reserved_tags;

S
Shaohua Li 已提交
395 396
	tags->alloc_policy = alloc_policy;

397 398 399 400
	if (bt_alloc(&tags->bitmap_tags, depth, node))
		goto free_tags;
	if (bt_alloc(&tags->breserved_tags, tags->nr_reserved_tags, node))
		goto free_bitmap_tags;
401 402

	return tags;
403 404 405
free_bitmap_tags:
	sbitmap_queue_free(&tags->bitmap_tags);
free_tags:
406 407 408 409
	kfree(tags);
	return NULL;
}

410
struct blk_mq_tags *blk_mq_init_tags(unsigned int total_tags,
S
Shaohua Li 已提交
411 412
				     unsigned int reserved_tags,
				     int node, int alloc_policy)
413 414 415 416 417 418 419 420 421 422 423 424
{
	struct blk_mq_tags *tags;

	if (total_tags > BLK_MQ_TAG_MAX) {
		pr_err("blk-mq: tag depth too large\n");
		return NULL;
	}

	tags = kzalloc_node(sizeof(*tags), GFP_KERNEL, node);
	if (!tags)
		return NULL;

K
Keith Busch 已提交
425 426 427 428 429
	if (!zalloc_cpumask_var(&tags->cpumask, GFP_KERNEL)) {
		kfree(tags);
		return NULL;
	}

430 431 432
	tags->nr_tags = total_tags;
	tags->nr_reserved_tags = reserved_tags;

S
Shaohua Li 已提交
433
	return blk_mq_init_bitmap_tags(tags, node, alloc_policy);
434 435 436 437
}

void blk_mq_free_tags(struct blk_mq_tags *tags)
{
438 439
	sbitmap_queue_free(&tags->bitmap_tags);
	sbitmap_queue_free(&tags->breserved_tags);
440
	free_cpumask_var(tags->cpumask);
441 442 443
	kfree(tags);
}

444 445 446 447
void blk_mq_tag_init_last_tag(struct blk_mq_tags *tags, unsigned int *tag)
{
	unsigned int depth = tags->nr_tags - tags->nr_reserved_tags;

448
	*tag = prandom_u32() % depth;
449 450
}

451 452 453 454 455 456 457 458 459 460
int blk_mq_tag_update_depth(struct blk_mq_tags *tags, unsigned int tdepth)
{
	tdepth -= tags->nr_reserved_tags;
	if (tdepth > tags->nr_tags)
		return -EINVAL;

	/*
	 * Don't need (or can't) update reserved tags here, they remain
	 * static and should never need resizing.
	 */
461 462
	sbitmap_queue_resize(&tags->bitmap_tags, tdepth);

463
	blk_mq_tag_wakeup_all(tags, false);
464 465 466
	return 0;
}

B
Bart Van Assche 已提交
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
/**
 * blk_mq_unique_tag() - return a tag that is unique queue-wide
 * @rq: request for which to compute a unique tag
 *
 * The tag field in struct request is unique per hardware queue but not over
 * all hardware queues. Hence this function that returns a tag with the
 * hardware context index in the upper bits and the per hardware queue tag in
 * the lower bits.
 *
 * Note: When called for a request that is queued on a non-multiqueue request
 * queue, the hardware context index is set to zero.
 */
u32 blk_mq_unique_tag(struct request *rq)
{
	struct request_queue *q = rq->q;
	struct blk_mq_hw_ctx *hctx;
	int hwq = 0;

	if (q->mq_ops) {
		hctx = q->mq_ops->map_queue(q, rq->mq_ctx->cpu);
		hwq = hctx->queue_num;
	}

	return (hwq << BLK_MQ_UNIQUE_TAG_BITS) |
		(rq->tag & BLK_MQ_UNIQUE_TAG_MASK);
}
EXPORT_SYMBOL(blk_mq_unique_tag);

495 496 497
ssize_t blk_mq_tag_sysfs_show(struct blk_mq_tags *tags, char *page)
{
	char *orig_page = page;
498
	unsigned int free, res;
499 500 501 502

	if (!tags)
		return 0;

503 504 505
	page += sprintf(page, "nr_tags=%u, reserved_tags=%u, "
			"bits_per_word=%u\n",
			tags->nr_tags, tags->nr_reserved_tags,
506
			1U << tags->bitmap_tags.sb.shift);
507

508 509
	free = bt_unused_tags(&tags->bitmap_tags);
	res = bt_unused_tags(&tags->breserved_tags);
510

511
	page += sprintf(page, "nr_free=%u, nr_reserved=%u\n", free, res);
512
	page += sprintf(page, "active_queues=%u\n", atomic_read(&tags->active_queues));
513 514 515

	return page - orig_page;
}