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