]>
Commit | Line | Data |
---|---|---|
320ae51f JA |
1 | #include <linux/kernel.h> |
2 | #include <linux/module.h> | |
320ae51f JA |
3 | |
4 | #include <linux/blk-mq.h> | |
5 | #include "blk.h" | |
6 | #include "blk-mq.h" | |
7 | #include "blk-mq-tag.h" | |
8 | ||
5810d903 | 9 | void blk_mq_wait_for_tags(struct blk_mq_tags *tags, bool reserved) |
320ae51f | 10 | { |
5810d903 | 11 | int tag = blk_mq_get_tag(tags, __GFP_WAIT, reserved); |
320ae51f JA |
12 | blk_mq_put_tag(tags, tag); |
13 | } | |
14 | ||
15 | bool blk_mq_has_free_tags(struct blk_mq_tags *tags) | |
16 | { | |
17 | return !tags || | |
18 | percpu_ida_free_tags(&tags->free_tags, nr_cpu_ids) != 0; | |
19 | } | |
20 | ||
21 | static unsigned int __blk_mq_get_tag(struct blk_mq_tags *tags, gfp_t gfp) | |
22 | { | |
23 | int tag; | |
24 | ||
6f6b5d1e KO |
25 | tag = percpu_ida_alloc(&tags->free_tags, (gfp & __GFP_WAIT) ? |
26 | TASK_UNINTERRUPTIBLE : TASK_RUNNING); | |
320ae51f JA |
27 | if (tag < 0) |
28 | return BLK_MQ_TAG_FAIL; | |
29 | return tag + tags->nr_reserved_tags; | |
30 | } | |
31 | ||
32 | static unsigned int __blk_mq_get_reserved_tag(struct blk_mq_tags *tags, | |
33 | gfp_t gfp) | |
34 | { | |
35 | int tag; | |
36 | ||
37 | if (unlikely(!tags->nr_reserved_tags)) { | |
38 | WARN_ON_ONCE(1); | |
39 | return BLK_MQ_TAG_FAIL; | |
40 | } | |
41 | ||
6f6b5d1e KO |
42 | tag = percpu_ida_alloc(&tags->reserved_tags, (gfp & __GFP_WAIT) ? |
43 | TASK_UNINTERRUPTIBLE : TASK_RUNNING); | |
320ae51f JA |
44 | if (tag < 0) |
45 | return BLK_MQ_TAG_FAIL; | |
46 | return tag; | |
47 | } | |
48 | ||
49 | unsigned int blk_mq_get_tag(struct blk_mq_tags *tags, gfp_t gfp, bool reserved) | |
50 | { | |
51 | if (!reserved) | |
52 | return __blk_mq_get_tag(tags, gfp); | |
53 | ||
54 | return __blk_mq_get_reserved_tag(tags, gfp); | |
55 | } | |
56 | ||
57 | static void __blk_mq_put_tag(struct blk_mq_tags *tags, unsigned int tag) | |
58 | { | |
59 | BUG_ON(tag >= tags->nr_tags); | |
60 | ||
61 | percpu_ida_free(&tags->free_tags, tag - tags->nr_reserved_tags); | |
62 | } | |
63 | ||
64 | static void __blk_mq_put_reserved_tag(struct blk_mq_tags *tags, | |
65 | unsigned int tag) | |
66 | { | |
67 | BUG_ON(tag >= tags->nr_reserved_tags); | |
68 | ||
69 | percpu_ida_free(&tags->reserved_tags, tag); | |
70 | } | |
71 | ||
72 | void blk_mq_put_tag(struct blk_mq_tags *tags, unsigned int tag) | |
73 | { | |
74 | if (tag >= tags->nr_reserved_tags) | |
75 | __blk_mq_put_tag(tags, tag); | |
76 | else | |
77 | __blk_mq_put_reserved_tag(tags, tag); | |
78 | } | |
79 | ||
80 | static int __blk_mq_tag_iter(unsigned id, void *data) | |
81 | { | |
82 | unsigned long *tag_map = data; | |
83 | __set_bit(id, tag_map); | |
84 | return 0; | |
85 | } | |
86 | ||
87 | void blk_mq_tag_busy_iter(struct blk_mq_tags *tags, | |
88 | void (*fn)(void *, unsigned long *), void *data) | |
89 | { | |
90 | unsigned long *tag_map; | |
91 | size_t map_size; | |
92 | ||
93 | map_size = ALIGN(tags->nr_tags, BITS_PER_LONG) / BITS_PER_LONG; | |
94 | tag_map = kzalloc(map_size * sizeof(unsigned long), GFP_ATOMIC); | |
95 | if (!tag_map) | |
96 | return; | |
97 | ||
98 | percpu_ida_for_each_free(&tags->free_tags, __blk_mq_tag_iter, tag_map); | |
99 | if (tags->nr_reserved_tags) | |
100 | percpu_ida_for_each_free(&tags->reserved_tags, __blk_mq_tag_iter, | |
101 | tag_map); | |
102 | ||
103 | fn(data, tag_map); | |
104 | kfree(tag_map); | |
105 | } | |
106 | ||
107 | struct blk_mq_tags *blk_mq_init_tags(unsigned int total_tags, | |
108 | unsigned int reserved_tags, int node) | |
109 | { | |
110 | unsigned int nr_tags, nr_cache; | |
111 | struct blk_mq_tags *tags; | |
112 | int ret; | |
113 | ||
114 | if (total_tags > BLK_MQ_TAG_MAX) { | |
115 | pr_err("blk-mq: tag depth too large\n"); | |
116 | return NULL; | |
117 | } | |
118 | ||
119 | tags = kzalloc_node(sizeof(*tags), GFP_KERNEL, node); | |
120 | if (!tags) | |
121 | return NULL; | |
122 | ||
123 | nr_tags = total_tags - reserved_tags; | |
124 | nr_cache = nr_tags / num_possible_cpus(); | |
125 | ||
126 | if (nr_cache < BLK_MQ_TAG_CACHE_MIN) | |
127 | nr_cache = BLK_MQ_TAG_CACHE_MIN; | |
128 | else if (nr_cache > BLK_MQ_TAG_CACHE_MAX) | |
129 | nr_cache = BLK_MQ_TAG_CACHE_MAX; | |
130 | ||
131 | tags->nr_tags = total_tags; | |
132 | tags->nr_reserved_tags = reserved_tags; | |
133 | tags->nr_max_cache = nr_cache; | |
134 | tags->nr_batch_move = max(1u, nr_cache / 2); | |
135 | ||
136 | ret = __percpu_ida_init(&tags->free_tags, tags->nr_tags - | |
137 | tags->nr_reserved_tags, | |
138 | tags->nr_max_cache, | |
139 | tags->nr_batch_move); | |
140 | if (ret) | |
141 | goto err_free_tags; | |
142 | ||
143 | if (reserved_tags) { | |
144 | /* | |
145 | * With max_cahe and batch set to 1, the allocator fallbacks to | |
146 | * no cached. It's fine reserved tags allocation is slow. | |
147 | */ | |
148 | ret = __percpu_ida_init(&tags->reserved_tags, reserved_tags, | |
149 | 1, 1); | |
150 | if (ret) | |
151 | goto err_reserved_tags; | |
152 | } | |
153 | ||
154 | return tags; | |
155 | ||
156 | err_reserved_tags: | |
157 | percpu_ida_destroy(&tags->free_tags); | |
158 | err_free_tags: | |
159 | kfree(tags); | |
160 | return NULL; | |
161 | } | |
162 | ||
163 | void blk_mq_free_tags(struct blk_mq_tags *tags) | |
164 | { | |
165 | percpu_ida_destroy(&tags->free_tags); | |
166 | percpu_ida_destroy(&tags->reserved_tags); | |
167 | kfree(tags); | |
168 | } | |
169 | ||
170 | ssize_t blk_mq_tag_sysfs_show(struct blk_mq_tags *tags, char *page) | |
171 | { | |
172 | char *orig_page = page; | |
11c94444 | 173 | unsigned int cpu; |
320ae51f JA |
174 | |
175 | if (!tags) | |
176 | return 0; | |
177 | ||
178 | page += sprintf(page, "nr_tags=%u, reserved_tags=%u, batch_move=%u," | |
179 | " max_cache=%u\n", tags->nr_tags, tags->nr_reserved_tags, | |
180 | tags->nr_batch_move, tags->nr_max_cache); | |
181 | ||
182 | page += sprintf(page, "nr_free=%u, nr_reserved=%u\n", | |
183 | percpu_ida_free_tags(&tags->free_tags, nr_cpu_ids), | |
184 | percpu_ida_free_tags(&tags->reserved_tags, nr_cpu_ids)); | |
185 | ||
186 | for_each_possible_cpu(cpu) { | |
187 | page += sprintf(page, " cpu%02u: nr_free=%u\n", cpu, | |
188 | percpu_ida_free_tags(&tags->free_tags, cpu)); | |
189 | } | |
190 | ||
191 | return page - orig_page; | |
192 | } |