1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/percpu_ida.h>
5 #include <linux/blk-mq.h>
8 #include "blk-mq-tag.h"
11 * Per tagged queue (tag address space) map
15 unsigned int nr_reserved_tags;
16 unsigned int nr_batch_move;
17 unsigned int nr_max_cache;
19 struct percpu_ida free_tags;
20 struct percpu_ida reserved_tags;
23 void blk_mq_wait_for_tags(struct blk_mq_tags *tags)
25 int tag = blk_mq_get_tag(tags, __GFP_WAIT, false);
26 blk_mq_put_tag(tags, tag);
29 bool blk_mq_has_free_tags(struct blk_mq_tags *tags)
32 percpu_ida_free_tags(&tags->free_tags, nr_cpu_ids) != 0;
35 static unsigned int __blk_mq_get_tag(struct blk_mq_tags *tags, gfp_t gfp)
39 tag = percpu_ida_alloc(&tags->free_tags, gfp);
41 return BLK_MQ_TAG_FAIL;
42 return tag + tags->nr_reserved_tags;
45 static unsigned int __blk_mq_get_reserved_tag(struct blk_mq_tags *tags,
50 if (unlikely(!tags->nr_reserved_tags)) {
52 return BLK_MQ_TAG_FAIL;
55 tag = percpu_ida_alloc(&tags->reserved_tags, gfp);
57 return BLK_MQ_TAG_FAIL;
61 unsigned int blk_mq_get_tag(struct blk_mq_tags *tags, gfp_t gfp, bool reserved)
64 return __blk_mq_get_tag(tags, gfp);
66 return __blk_mq_get_reserved_tag(tags, gfp);
69 static void __blk_mq_put_tag(struct blk_mq_tags *tags, unsigned int tag)
71 BUG_ON(tag >= tags->nr_tags);
73 percpu_ida_free(&tags->free_tags, tag - tags->nr_reserved_tags);
76 static void __blk_mq_put_reserved_tag(struct blk_mq_tags *tags,
79 BUG_ON(tag >= tags->nr_reserved_tags);
81 percpu_ida_free(&tags->reserved_tags, tag);
84 void blk_mq_put_tag(struct blk_mq_tags *tags, unsigned int tag)
86 if (tag >= tags->nr_reserved_tags)
87 __blk_mq_put_tag(tags, tag);
89 __blk_mq_put_reserved_tag(tags, tag);
92 static int __blk_mq_tag_iter(unsigned id, void *data)
94 unsigned long *tag_map = data;
95 __set_bit(id, tag_map);
99 void blk_mq_tag_busy_iter(struct blk_mq_tags *tags,
100 void (*fn)(void *, unsigned long *), void *data)
102 unsigned long *tag_map;
105 map_size = ALIGN(tags->nr_tags, BITS_PER_LONG) / BITS_PER_LONG;
106 tag_map = kzalloc(map_size * sizeof(unsigned long), GFP_ATOMIC);
110 percpu_ida_for_each_free(&tags->free_tags, __blk_mq_tag_iter, tag_map);
111 if (tags->nr_reserved_tags)
112 percpu_ida_for_each_free(&tags->reserved_tags, __blk_mq_tag_iter,
119 struct blk_mq_tags *blk_mq_init_tags(unsigned int total_tags,
120 unsigned int reserved_tags, int node)
122 unsigned int nr_tags, nr_cache;
123 struct blk_mq_tags *tags;
126 if (total_tags > BLK_MQ_TAG_MAX) {
127 pr_err("blk-mq: tag depth too large\n");
131 tags = kzalloc_node(sizeof(*tags), GFP_KERNEL, node);
135 nr_tags = total_tags - reserved_tags;
136 nr_cache = nr_tags / num_possible_cpus();
138 if (nr_cache < BLK_MQ_TAG_CACHE_MIN)
139 nr_cache = BLK_MQ_TAG_CACHE_MIN;
140 else if (nr_cache > BLK_MQ_TAG_CACHE_MAX)
141 nr_cache = BLK_MQ_TAG_CACHE_MAX;
143 tags->nr_tags = total_tags;
144 tags->nr_reserved_tags = reserved_tags;
145 tags->nr_max_cache = nr_cache;
146 tags->nr_batch_move = max(1u, nr_cache / 2);
148 ret = __percpu_ida_init(&tags->free_tags, tags->nr_tags -
149 tags->nr_reserved_tags,
151 tags->nr_batch_move);
157 * With max_cahe and batch set to 1, the allocator fallbacks to
158 * no cached. It's fine reserved tags allocation is slow.
160 ret = __percpu_ida_init(&tags->reserved_tags, reserved_tags,
163 goto err_reserved_tags;
169 percpu_ida_destroy(&tags->free_tags);
175 void blk_mq_free_tags(struct blk_mq_tags *tags)
177 percpu_ida_destroy(&tags->free_tags);
178 percpu_ida_destroy(&tags->reserved_tags);
182 ssize_t blk_mq_tag_sysfs_show(struct blk_mq_tags *tags, char *page)
184 char *orig_page = page;
190 page += sprintf(page, "nr_tags=%u, reserved_tags=%u, batch_move=%u,"
191 " max_cache=%u\n", tags->nr_tags, tags->nr_reserved_tags,
192 tags->nr_batch_move, tags->nr_max_cache);
194 page += sprintf(page, "nr_free=%u, nr_reserved=%u\n",
195 percpu_ida_free_tags(&tags->free_tags, nr_cpu_ids),
196 percpu_ida_free_tags(&tags->reserved_tags, nr_cpu_ids));
198 for_each_possible_cpu(cpu) {
199 page += sprintf(page, " cpu%02u: nr_free=%u\n", cpu,
200 percpu_ida_free_tags(&tags->free_tags, cpu));
203 return page - orig_page;