blk-mq: remove blk_mq_alloc_request_pinned
[linux-2.6-block.git] / block / blk-mq-tag.c
CommitLineData
320ae51f
JA
1#include <linux/kernel.h>
2#include <linux/module.h>
4bb659b1 3#include <linux/random.h>
320ae51f
JA
4
5#include <linux/blk-mq.h>
6#include "blk.h"
7#include "blk-mq.h"
8#include "blk-mq-tag.h"
9
4bb659b1
JA
10static bool bt_has_free_tags(struct blk_mq_bitmap_tags *bt)
11{
12 int i;
13
14 for (i = 0; i < bt->map_nr; i++) {
e93ecf60 15 struct blk_align_bitmap *bm = &bt->map[i];
4bb659b1
JA
16 int ret;
17
18 ret = find_first_zero_bit(&bm->word, bm->depth);
19 if (ret < bm->depth)
20 return true;
21 }
22
23 return false;
320ae51f
JA
24}
25
26bool blk_mq_has_free_tags(struct blk_mq_tags *tags)
27{
4bb659b1
JA
28 if (!tags)
29 return true;
30
31 return bt_has_free_tags(&tags->bitmap_tags);
32}
33
0d2602ca
JA
34static inline void bt_index_inc(unsigned int *index)
35{
36 *index = (*index + 1) & (BT_WAIT_QUEUES - 1);
37}
38
39/*
40 * If a previously inactive queue goes active, bump the active user count.
41 */
42bool __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)
43{
44 if (!test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state) &&
45 !test_and_set_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
46 atomic_inc(&hctx->tags->active_queues);
47
48 return true;
49}
50
51/*
e3a2b3f9 52 * Wakeup all potentially sleeping on normal (non-reserved) tags
0d2602ca 53 */
e3a2b3f9 54static void blk_mq_tag_wakeup_all(struct blk_mq_tags *tags)
0d2602ca 55{
0d2602ca
JA
56 struct blk_mq_bitmap_tags *bt;
57 int i, wake_index;
58
0d2602ca
JA
59 bt = &tags->bitmap_tags;
60 wake_index = bt->wake_index;
61 for (i = 0; i < BT_WAIT_QUEUES; i++) {
62 struct bt_wait_state *bs = &bt->bs[wake_index];
63
64 if (waitqueue_active(&bs->wait))
65 wake_up(&bs->wait);
66
67 bt_index_inc(&wake_index);
68 }
69}
70
e3a2b3f9
JA
71/*
72 * If a previously busy queue goes inactive, potential waiters could now
73 * be allowed to queue. Wake them up and check.
74 */
75void __blk_mq_tag_idle(struct blk_mq_hw_ctx *hctx)
76{
77 struct blk_mq_tags *tags = hctx->tags;
78
79 if (!test_and_clear_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
80 return;
81
82 atomic_dec(&tags->active_queues);
83
84 blk_mq_tag_wakeup_all(tags);
85}
86
0d2602ca
JA
87/*
88 * For shared tag users, we track the number of currently active users
89 * and attempt to provide a fair share of the tag depth for each of them.
90 */
91static inline bool hctx_may_queue(struct blk_mq_hw_ctx *hctx,
92 struct blk_mq_bitmap_tags *bt)
93{
94 unsigned int depth, users;
95
96 if (!hctx || !(hctx->flags & BLK_MQ_F_TAG_SHARED))
97 return true;
98 if (!test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
99 return true;
100
101 /*
102 * Don't try dividing an ant
103 */
104 if (bt->depth == 1)
105 return true;
106
107 users = atomic_read(&hctx->tags->active_queues);
108 if (!users)
109 return true;
110
111 /*
112 * Allow at least some tags
113 */
114 depth = max((bt->depth + users - 1) / users, 4U);
115 return atomic_read(&hctx->nr_active) < depth;
116}
117
e93ecf60 118static int __bt_get_word(struct blk_align_bitmap *bm, unsigned int last_tag)
4bb659b1
JA
119{
120 int tag, org_last_tag, end;
121
59d13bf5 122 org_last_tag = last_tag;
4bb659b1
JA
123 end = bm->depth;
124 do {
125restart:
126 tag = find_next_zero_bit(&bm->word, end, last_tag);
127 if (unlikely(tag >= end)) {
128 /*
129 * We started with an offset, start from 0 to
130 * exhaust the map.
131 */
132 if (org_last_tag && last_tag) {
133 end = last_tag;
134 last_tag = 0;
135 goto restart;
136 }
137 return -1;
138 }
139 last_tag = tag + 1;
140 } while (test_and_set_bit_lock(tag, &bm->word));
141
142 return tag;
143}
144
145/*
146 * Straight forward bitmap tag implementation, where each bit is a tag
147 * (cleared == free, and set == busy). The small twist is using per-cpu
148 * last_tag caches, which blk-mq stores in the blk_mq_ctx software queue
149 * contexts. This enables us to drastically limit the space searched,
150 * without dirtying an extra shared cacheline like we would if we stored
151 * the cache value inside the shared blk_mq_bitmap_tags structure. On top
152 * of that, each word of tags is in a separate cacheline. This means that
153 * multiple users will tend to stick to different cachelines, at least
154 * until the map is exhausted.
155 */
0d2602ca
JA
156static int __bt_get(struct blk_mq_hw_ctx *hctx, struct blk_mq_bitmap_tags *bt,
157 unsigned int *tag_cache)
4bb659b1
JA
158{
159 unsigned int last_tag, org_last_tag;
160 int index, i, tag;
161
0d2602ca
JA
162 if (!hctx_may_queue(hctx, bt))
163 return -1;
164
4bb659b1 165 last_tag = org_last_tag = *tag_cache;
59d13bf5 166 index = TAG_TO_INDEX(bt, last_tag);
4bb659b1
JA
167
168 for (i = 0; i < bt->map_nr; i++) {
59d13bf5 169 tag = __bt_get_word(&bt->map[index], TAG_TO_BIT(bt, last_tag));
4bb659b1 170 if (tag != -1) {
59d13bf5 171 tag += (index << bt->bits_per_word);
4bb659b1
JA
172 goto done;
173 }
174
175 last_tag = 0;
176 if (++index >= bt->map_nr)
177 index = 0;
178 }
179
180 *tag_cache = 0;
181 return -1;
182
183 /*
184 * Only update the cache from the allocation path, if we ended
185 * up using the specific cached tag.
186 */
187done:
188 if (tag == org_last_tag) {
189 last_tag = tag + 1;
190 if (last_tag >= bt->depth - 1)
191 last_tag = 0;
192
193 *tag_cache = last_tag;
194 }
195
196 return tag;
197}
198
4bb659b1
JA
199static struct bt_wait_state *bt_wait_ptr(struct blk_mq_bitmap_tags *bt,
200 struct blk_mq_hw_ctx *hctx)
201{
202 struct bt_wait_state *bs;
203
204 if (!hctx)
205 return &bt->bs[0];
206
207 bs = &bt->bs[hctx->wait_index];
208 bt_index_inc(&hctx->wait_index);
209 return bs;
320ae51f
JA
210}
211
4bb659b1
JA
212static int bt_get(struct blk_mq_bitmap_tags *bt, struct blk_mq_hw_ctx *hctx,
213 unsigned int *last_tag, gfp_t gfp)
320ae51f 214{
4bb659b1
JA
215 struct bt_wait_state *bs;
216 DEFINE_WAIT(wait);
320ae51f
JA
217 int tag;
218
0d2602ca 219 tag = __bt_get(hctx, bt, last_tag);
4bb659b1
JA
220 if (tag != -1)
221 return tag;
222
223 if (!(gfp & __GFP_WAIT))
224 return -1;
225
226 bs = bt_wait_ptr(bt, hctx);
227 do {
228 bool was_empty;
229
230 was_empty = list_empty(&wait.task_list);
231 prepare_to_wait(&bs->wait, &wait, TASK_UNINTERRUPTIBLE);
232
0d2602ca 233 tag = __bt_get(hctx, bt, last_tag);
4bb659b1
JA
234 if (tag != -1)
235 break;
236
237 if (was_empty)
238 atomic_set(&bs->wait_cnt, bt->wake_cnt);
239
240 io_schedule();
241 } while (1);
242
243 finish_wait(&bs->wait, &wait);
244 return tag;
245}
246
247static unsigned int __blk_mq_get_tag(struct blk_mq_tags *tags,
248 struct blk_mq_hw_ctx *hctx,
249 unsigned int *last_tag, gfp_t gfp)
250{
251 int tag;
252
253 tag = bt_get(&tags->bitmap_tags, hctx, last_tag, gfp);
254 if (tag >= 0)
255 return tag + tags->nr_reserved_tags;
256
257 return BLK_MQ_TAG_FAIL;
320ae51f
JA
258}
259
260static unsigned int __blk_mq_get_reserved_tag(struct blk_mq_tags *tags,
261 gfp_t gfp)
262{
4bb659b1 263 int tag, zero = 0;
320ae51f
JA
264
265 if (unlikely(!tags->nr_reserved_tags)) {
266 WARN_ON_ONCE(1);
267 return BLK_MQ_TAG_FAIL;
268 }
269
4bb659b1 270 tag = bt_get(&tags->breserved_tags, NULL, &zero, gfp);
320ae51f
JA
271 if (tag < 0)
272 return BLK_MQ_TAG_FAIL;
4bb659b1 273
320ae51f
JA
274 return tag;
275}
276
0d2602ca 277unsigned int blk_mq_get_tag(struct blk_mq_hw_ctx *hctx, unsigned int *last_tag,
4bb659b1 278 gfp_t gfp, bool reserved)
320ae51f
JA
279{
280 if (!reserved)
0d2602ca 281 return __blk_mq_get_tag(hctx->tags, hctx, last_tag, gfp);
320ae51f 282
0d2602ca 283 return __blk_mq_get_reserved_tag(hctx->tags, gfp);
320ae51f
JA
284}
285
4bb659b1
JA
286static struct bt_wait_state *bt_wake_ptr(struct blk_mq_bitmap_tags *bt)
287{
288 int i, wake_index;
289
290 wake_index = bt->wake_index;
291 for (i = 0; i < BT_WAIT_QUEUES; i++) {
292 struct bt_wait_state *bs = &bt->bs[wake_index];
293
294 if (waitqueue_active(&bs->wait)) {
295 if (wake_index != bt->wake_index)
296 bt->wake_index = wake_index;
297
298 return bs;
299 }
300
301 bt_index_inc(&wake_index);
302 }
303
304 return NULL;
305}
306
307static void bt_clear_tag(struct blk_mq_bitmap_tags *bt, unsigned int tag)
308{
59d13bf5 309 const int index = TAG_TO_INDEX(bt, tag);
4bb659b1
JA
310 struct bt_wait_state *bs;
311
0289b2e1
ML
312 /*
313 * The unlock memory barrier need to order access to req in free
314 * path and clearing tag bit
315 */
316 clear_bit_unlock(TAG_TO_BIT(bt, tag), &bt->map[index].word);
4bb659b1
JA
317
318 bs = bt_wake_ptr(bt);
319 if (bs && atomic_dec_and_test(&bs->wait_cnt)) {
4bb659b1
JA
320 atomic_set(&bs->wait_cnt, bt->wake_cnt);
321 bt_index_inc(&bt->wake_index);
322 wake_up(&bs->wait);
323 }
324}
325
320ae51f
JA
326static void __blk_mq_put_tag(struct blk_mq_tags *tags, unsigned int tag)
327{
328 BUG_ON(tag >= tags->nr_tags);
329
4bb659b1 330 bt_clear_tag(&tags->bitmap_tags, tag);
320ae51f
JA
331}
332
333static void __blk_mq_put_reserved_tag(struct blk_mq_tags *tags,
334 unsigned int tag)
335{
336 BUG_ON(tag >= tags->nr_reserved_tags);
337
4bb659b1 338 bt_clear_tag(&tags->breserved_tags, tag);
320ae51f
JA
339}
340
0d2602ca 341void blk_mq_put_tag(struct blk_mq_hw_ctx *hctx, unsigned int tag,
4bb659b1 342 unsigned int *last_tag)
320ae51f 343{
0d2602ca
JA
344 struct blk_mq_tags *tags = hctx->tags;
345
4bb659b1
JA
346 if (tag >= tags->nr_reserved_tags) {
347 const int real_tag = tag - tags->nr_reserved_tags;
348
349 __blk_mq_put_tag(tags, real_tag);
350 *last_tag = real_tag;
351 } else
320ae51f
JA
352 __blk_mq_put_reserved_tag(tags, tag);
353}
354
4bb659b1
JA
355static void bt_for_each_free(struct blk_mq_bitmap_tags *bt,
356 unsigned long *free_map, unsigned int off)
320ae51f 357{
4bb659b1
JA
358 int i;
359
360 for (i = 0; i < bt->map_nr; i++) {
e93ecf60 361 struct blk_align_bitmap *bm = &bt->map[i];
4bb659b1
JA
362 int bit = 0;
363
364 do {
365 bit = find_next_zero_bit(&bm->word, bm->depth, bit);
366 if (bit >= bm->depth)
367 break;
368
369 __set_bit(bit + off, free_map);
370 bit++;
371 } while (1);
372
59d13bf5 373 off += (1 << bt->bits_per_word);
4bb659b1 374 }
320ae51f
JA
375}
376
377void blk_mq_tag_busy_iter(struct blk_mq_tags *tags,
378 void (*fn)(void *, unsigned long *), void *data)
379{
380 unsigned long *tag_map;
381 size_t map_size;
382
383 map_size = ALIGN(tags->nr_tags, BITS_PER_LONG) / BITS_PER_LONG;
384 tag_map = kzalloc(map_size * sizeof(unsigned long), GFP_ATOMIC);
385 if (!tag_map)
386 return;
387
4bb659b1 388 bt_for_each_free(&tags->bitmap_tags, tag_map, tags->nr_reserved_tags);
320ae51f 389 if (tags->nr_reserved_tags)
4bb659b1 390 bt_for_each_free(&tags->breserved_tags, tag_map, 0);
320ae51f
JA
391
392 fn(data, tag_map);
393 kfree(tag_map);
394}
edf866b3 395EXPORT_SYMBOL(blk_mq_tag_busy_iter);
320ae51f 396
4bb659b1
JA
397static unsigned int bt_unused_tags(struct blk_mq_bitmap_tags *bt)
398{
399 unsigned int i, used;
400
401 for (i = 0, used = 0; i < bt->map_nr; i++) {
e93ecf60 402 struct blk_align_bitmap *bm = &bt->map[i];
4bb659b1
JA
403
404 used += bitmap_weight(&bm->word, bm->depth);
405 }
406
407 return bt->depth - used;
408}
409
e3a2b3f9
JA
410static void bt_update_count(struct blk_mq_bitmap_tags *bt,
411 unsigned int depth)
412{
413 unsigned int tags_per_word = 1U << bt->bits_per_word;
414 unsigned int map_depth = depth;
415
416 if (depth) {
417 int i;
418
419 for (i = 0; i < bt->map_nr; i++) {
420 bt->map[i].depth = min(map_depth, tags_per_word);
421 map_depth -= bt->map[i].depth;
422 }
423 }
424
425 bt->wake_cnt = BT_WAIT_BATCH;
426 if (bt->wake_cnt > depth / 4)
427 bt->wake_cnt = max(1U, depth / 4);
428
429 bt->depth = depth;
430}
431
4bb659b1
JA
432static int bt_alloc(struct blk_mq_bitmap_tags *bt, unsigned int depth,
433 int node, bool reserved)
434{
435 int i;
436
59d13bf5
JA
437 bt->bits_per_word = ilog2(BITS_PER_LONG);
438
4bb659b1
JA
439 /*
440 * Depth can be zero for reserved tags, that's not a failure
441 * condition.
442 */
443 if (depth) {
e3a2b3f9 444 unsigned int nr, tags_per_word;
59d13bf5
JA
445
446 tags_per_word = (1 << bt->bits_per_word);
447
448 /*
449 * If the tag space is small, shrink the number of tags
450 * per word so we spread over a few cachelines, at least.
451 * If less than 4 tags, just forget about it, it's not
452 * going to work optimally anyway.
453 */
454 if (depth >= 4) {
455 while (tags_per_word * 4 > depth) {
456 bt->bits_per_word--;
457 tags_per_word = (1 << bt->bits_per_word);
458 }
459 }
4bb659b1 460
59d13bf5 461 nr = ALIGN(depth, tags_per_word) / tags_per_word;
e93ecf60 462 bt->map = kzalloc_node(nr * sizeof(struct blk_align_bitmap),
4bb659b1
JA
463 GFP_KERNEL, node);
464 if (!bt->map)
465 return -ENOMEM;
466
467 bt->map_nr = nr;
4bb659b1
JA
468 }
469
470 bt->bs = kzalloc(BT_WAIT_QUEUES * sizeof(*bt->bs), GFP_KERNEL);
471 if (!bt->bs) {
472 kfree(bt->map);
473 return -ENOMEM;
474 }
475
476 for (i = 0; i < BT_WAIT_QUEUES; i++)
477 init_waitqueue_head(&bt->bs[i].wait);
478
e3a2b3f9 479 bt_update_count(bt, depth);
4bb659b1
JA
480 return 0;
481}
482
483static void bt_free(struct blk_mq_bitmap_tags *bt)
484{
485 kfree(bt->map);
486 kfree(bt->bs);
487}
488
489static struct blk_mq_tags *blk_mq_init_bitmap_tags(struct blk_mq_tags *tags,
490 int node)
491{
492 unsigned int depth = tags->nr_tags - tags->nr_reserved_tags;
493
494 if (bt_alloc(&tags->bitmap_tags, depth, node, false))
495 goto enomem;
496 if (bt_alloc(&tags->breserved_tags, tags->nr_reserved_tags, node, true))
497 goto enomem;
498
499 return tags;
500enomem:
501 bt_free(&tags->bitmap_tags);
502 kfree(tags);
503 return NULL;
504}
505
320ae51f
JA
506struct blk_mq_tags *blk_mq_init_tags(unsigned int total_tags,
507 unsigned int reserved_tags, int node)
508{
320ae51f 509 struct blk_mq_tags *tags;
320ae51f
JA
510
511 if (total_tags > BLK_MQ_TAG_MAX) {
512 pr_err("blk-mq: tag depth too large\n");
513 return NULL;
514 }
515
516 tags = kzalloc_node(sizeof(*tags), GFP_KERNEL, node);
517 if (!tags)
518 return NULL;
519
320ae51f
JA
520 tags->nr_tags = total_tags;
521 tags->nr_reserved_tags = reserved_tags;
320ae51f 522
4bb659b1 523 return blk_mq_init_bitmap_tags(tags, node);
320ae51f
JA
524}
525
526void blk_mq_free_tags(struct blk_mq_tags *tags)
527{
4bb659b1
JA
528 bt_free(&tags->bitmap_tags);
529 bt_free(&tags->breserved_tags);
320ae51f
JA
530 kfree(tags);
531}
532
4bb659b1
JA
533void blk_mq_tag_init_last_tag(struct blk_mq_tags *tags, unsigned int *tag)
534{
535 unsigned int depth = tags->nr_tags - tags->nr_reserved_tags;
536
9d3d21ae 537 *tag = prandom_u32() % depth;
4bb659b1
JA
538}
539
e3a2b3f9
JA
540int blk_mq_tag_update_depth(struct blk_mq_tags *tags, unsigned int tdepth)
541{
542 tdepth -= tags->nr_reserved_tags;
543 if (tdepth > tags->nr_tags)
544 return -EINVAL;
545
546 /*
547 * Don't need (or can't) update reserved tags here, they remain
548 * static and should never need resizing.
549 */
550 bt_update_count(&tags->bitmap_tags, tdepth);
551 blk_mq_tag_wakeup_all(tags);
552 return 0;
553}
554
320ae51f
JA
555ssize_t blk_mq_tag_sysfs_show(struct blk_mq_tags *tags, char *page)
556{
557 char *orig_page = page;
4bb659b1 558 unsigned int free, res;
320ae51f
JA
559
560 if (!tags)
561 return 0;
562
59d13bf5
JA
563 page += sprintf(page, "nr_tags=%u, reserved_tags=%u, "
564 "bits_per_word=%u\n",
565 tags->nr_tags, tags->nr_reserved_tags,
566 tags->bitmap_tags.bits_per_word);
320ae51f 567
4bb659b1
JA
568 free = bt_unused_tags(&tags->bitmap_tags);
569 res = bt_unused_tags(&tags->breserved_tags);
320ae51f 570
4bb659b1 571 page += sprintf(page, "nr_free=%u, nr_reserved=%u\n", free, res);
0d2602ca 572 page += sprintf(page, "active_queues=%u\n", atomic_read(&tags->active_queues));
320ae51f
JA
573
574 return page - orig_page;
575}