xref: /linux/block/blk-mq.h (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef INT_BLK_MQ_H
3 #define INT_BLK_MQ_H
4 
5 #include <linux/blk-mq.h>
6 #include "blk-stat.h"
7 
8 struct blk_mq_tag_set;
9 struct elevator_tags;
10 
11 struct blk_mq_ctxs {
12 	struct kobject kobj;
13 	struct blk_mq_ctx __percpu	*queue_ctx;
14 };
15 
16 /**
17  * struct blk_mq_ctx - State for a software queue facing the submitting CPUs
18  */
19 struct blk_mq_ctx {
20 	struct {
21 		spinlock_t		lock;
22 		struct list_head	rq_lists[HCTX_MAX_TYPES];
23 	} ____cacheline_aligned_in_smp;
24 
25 	unsigned int		cpu;
26 	unsigned short		index_hw[HCTX_MAX_TYPES];
27 	struct blk_mq_hw_ctx 	*hctxs[HCTX_MAX_TYPES];
28 
29 	struct request_queue	*queue;
30 	struct blk_mq_ctxs      *ctxs;
31 	struct kobject		kobj;
32 } ____cacheline_aligned_in_smp;
33 
34 enum {
35 	BLK_MQ_NO_TAG		= -1U,
36 	BLK_MQ_TAG_MIN		= 1,
37 	BLK_MQ_TAG_MAX		= BLK_MQ_NO_TAG - 1,
38 };
39 
40 #define BLK_MQ_CPU_WORK_BATCH	(8)
41 
42 typedef unsigned int __bitwise blk_insert_t;
43 #define BLK_MQ_INSERT_AT_HEAD		((__force blk_insert_t)0x01)
44 
45 void blk_mq_submit_bio(struct bio *bio);
46 int blk_mq_poll(struct request_queue *q, blk_qc_t cookie, struct io_comp_batch *iob,
47 		unsigned int flags);
48 void blk_mq_exit_queue(struct request_queue *q);
49 struct elevator_tags *blk_mq_update_nr_requests(struct request_queue *q,
50 						struct elevator_tags *tags,
51 						unsigned int nr);
52 void blk_mq_wake_waiters(struct request_queue *q);
53 bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *hctx, struct list_head *,
54 			     bool);
55 void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list);
56 struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx,
57 					struct blk_mq_ctx *start);
58 void blk_mq_put_rq_ref(struct request *rq);
59 
60 /*
61  * Internal helpers for allocating/freeing the request map
62  */
63 void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
64 		     unsigned int hctx_idx);
65 void blk_mq_free_rq_map(struct blk_mq_tags *tags);
66 struct blk_mq_tags *blk_mq_alloc_map_and_rqs(struct blk_mq_tag_set *set,
67 				unsigned int hctx_idx, unsigned int depth);
68 void blk_mq_free_map_and_rqs(struct blk_mq_tag_set *set,
69 			     struct blk_mq_tags *tags,
70 			     unsigned int hctx_idx);
71 
72 /*
73  * CPU -> queue mappings
74  */
75 extern int blk_mq_hw_queue_to_node(struct blk_mq_queue_map *qmap, unsigned int);
76 
77 /*
78  * blk_mq_map_queue_type() - map (hctx_type,cpu) to hardware queue
79  * @q: request queue
80  * @type: the hctx type index
81  * @cpu: CPU
82  */
83 static inline struct blk_mq_hw_ctx *blk_mq_map_queue_type(struct request_queue *q,
84 							  enum hctx_type type,
85 							  unsigned int cpu)
86 {
87 	return queue_hctx((q), (q->tag_set->map[type].mq_map[cpu]));
88 }
89 
90 static inline enum hctx_type blk_mq_get_hctx_type(blk_opf_t opf)
91 {
92 	enum hctx_type type = HCTX_TYPE_DEFAULT;
93 
94 	/*
95 	 * The caller ensure that if REQ_POLLED, poll must be enabled.
96 	 */
97 	if (opf & REQ_POLLED)
98 		type = HCTX_TYPE_POLL;
99 	else if ((opf & REQ_OP_MASK) == REQ_OP_READ)
100 		type = HCTX_TYPE_READ;
101 	return type;
102 }
103 
104 /*
105  * blk_mq_map_queue() - map (cmd_flags,type) to hardware queue
106  * @opf: operation type (REQ_OP_*) and flags (e.g. REQ_POLLED).
107  * @ctx: software queue cpu ctx
108  */
109 static inline struct blk_mq_hw_ctx *blk_mq_map_queue(blk_opf_t opf,
110 						     struct blk_mq_ctx *ctx)
111 {
112 	return ctx->hctxs[blk_mq_get_hctx_type(opf)];
113 }
114 
115 /*
116  * Default to double of smaller one between hw queue_depth and
117  * 128, since we don't split into sync/async like the old code
118  * did. Additionally, this is a per-hw queue depth.
119  */
120 static inline unsigned int blk_mq_default_nr_requests(
121 		struct blk_mq_tag_set *set)
122 {
123 	return 2 * min_t(unsigned int, set->queue_depth, BLKDEV_DEFAULT_RQ);
124 }
125 
126 /*
127  * sysfs helpers
128  */
129 extern void blk_mq_sysfs_init(struct request_queue *q);
130 extern void blk_mq_sysfs_deinit(struct request_queue *q);
131 int blk_mq_sysfs_register(struct gendisk *disk);
132 void blk_mq_sysfs_unregister(struct gendisk *disk);
133 int blk_mq_sysfs_register_hctxs(struct request_queue *q);
134 void blk_mq_sysfs_unregister_hctxs(struct request_queue *q);
135 extern void blk_mq_hctx_kobj_init(struct blk_mq_hw_ctx *hctx);
136 void blk_mq_free_plug_rqs(struct blk_plug *plug);
137 void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule);
138 
139 void blk_mq_cancel_work_sync(struct request_queue *q);
140 
141 void blk_mq_release(struct request_queue *q);
142 
143 static inline struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
144 					   unsigned int cpu)
145 {
146 	return per_cpu_ptr(q->queue_ctx, cpu);
147 }
148 
149 /*
150  * This assumes per-cpu software queueing queues. They could be per-node
151  * as well, for instance. For now this is hardcoded as-is. Note that we don't
152  * care about preemption, since we know the ctx's are persistent. This does
153  * mean that we can't rely on ctx always matching the currently running CPU.
154  */
155 static inline struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
156 {
157 	return __blk_mq_get_ctx(q, raw_smp_processor_id());
158 }
159 
160 struct blk_mq_alloc_data {
161 	/* input parameter */
162 	struct request_queue *q;
163 	blk_mq_req_flags_t flags;
164 	unsigned int shallow_depth;
165 	blk_opf_t cmd_flags;
166 	req_flags_t rq_flags;
167 
168 	/* allocate multiple requests/tags in one go */
169 	unsigned int nr_tags;
170 	struct rq_list *cached_rqs;
171 
172 	/* input & output parameter */
173 	struct blk_mq_ctx *ctx;
174 	struct blk_mq_hw_ctx *hctx;
175 };
176 
177 struct blk_mq_tags *blk_mq_init_tags(unsigned int nr_tags,
178 		unsigned int reserved_tags, unsigned int flags, int node);
179 void blk_mq_free_tags(struct blk_mq_tags *tags);
180 
181 extern struct srcu_struct blk_mq_tags_srcu;
182 
183 unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data);
184 unsigned long blk_mq_get_tags(struct blk_mq_alloc_data *data, int nr_tags,
185 		unsigned int *offset);
186 void blk_mq_put_tag(struct blk_mq_tags *tags, struct blk_mq_ctx *ctx,
187 		unsigned int tag);
188 void blk_mq_put_tags(struct blk_mq_tags *tags, int *tag_array, int nr_tags);
189 void blk_mq_tag_resize_shared_tags(struct blk_mq_tag_set *set,
190 		unsigned int size);
191 void blk_mq_tag_update_sched_shared_tags(struct request_queue *q,
192 					 unsigned int nr);
193 
194 void blk_mq_tag_wakeup_all(struct blk_mq_tags *tags, bool);
195 void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_tag_iter_fn *fn,
196 		void *priv);
197 void blk_mq_all_tag_iter(struct blk_mq_tags *tags, busy_tag_iter_fn *fn,
198 		void *priv);
199 
200 static inline struct sbq_wait_state *bt_wait_ptr(struct sbitmap_queue *bt,
201 						 struct blk_mq_hw_ctx *hctx)
202 {
203 	if (!hctx)
204 		return &bt->ws[0];
205 	return sbq_wait_ptr(bt, &hctx->wait_index);
206 }
207 
208 void __blk_mq_tag_busy(struct blk_mq_hw_ctx *);
209 void __blk_mq_tag_idle(struct blk_mq_hw_ctx *);
210 
211 static inline void blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)
212 {
213 	if (hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
214 		__blk_mq_tag_busy(hctx);
215 }
216 
217 static inline void blk_mq_tag_idle(struct blk_mq_hw_ctx *hctx)
218 {
219 	if (hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
220 		__blk_mq_tag_idle(hctx);
221 }
222 
223 static inline bool blk_mq_tag_is_reserved(struct blk_mq_tags *tags,
224 					  unsigned int tag)
225 {
226 	return tag < tags->nr_reserved_tags;
227 }
228 
229 static inline bool blk_mq_is_shared_tags(unsigned int flags)
230 {
231 	return flags & BLK_MQ_F_TAG_HCTX_SHARED;
232 }
233 
234 static inline struct blk_mq_tags *blk_mq_tags_from_data(struct blk_mq_alloc_data *data)
235 {
236 	if (data->rq_flags & RQF_SCHED_TAGS)
237 		return data->hctx->sched_tags;
238 	return data->hctx->tags;
239 }
240 
241 static inline bool blk_mq_hctx_stopped(struct blk_mq_hw_ctx *hctx)
242 {
243 	/* Fast path: hardware queue is not stopped most of the time. */
244 	if (likely(!test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
245 		return false;
246 
247 	/*
248 	 * This barrier is used to order adding of dispatch list before and
249 	 * the test of BLK_MQ_S_STOPPED below. Pairs with the memory barrier
250 	 * in blk_mq_start_stopped_hw_queue() so that dispatch code could
251 	 * either see BLK_MQ_S_STOPPED is cleared or dispatch list is not
252 	 * empty to avoid missing dispatching requests.
253 	 */
254 	smp_mb();
255 
256 	return test_bit(BLK_MQ_S_STOPPED, &hctx->state);
257 }
258 
259 static inline bool blk_mq_hw_queue_mapped(struct blk_mq_hw_ctx *hctx)
260 {
261 	return hctx->nr_ctx && hctx->tags;
262 }
263 
264 void blk_mq_in_driver_rw(struct block_device *part, unsigned int inflight[2]);
265 
266 static inline void blk_mq_put_dispatch_budget(struct request_queue *q,
267 					      int budget_token)
268 {
269 	if (q->mq_ops->put_budget)
270 		q->mq_ops->put_budget(q, budget_token);
271 }
272 
273 static inline int blk_mq_get_dispatch_budget(struct request_queue *q)
274 {
275 	if (q->mq_ops->get_budget)
276 		return q->mq_ops->get_budget(q);
277 	return 0;
278 }
279 
280 static inline void blk_mq_set_rq_budget_token(struct request *rq, int token)
281 {
282 	if (token < 0)
283 		return;
284 
285 	if (rq->q->mq_ops->set_rq_budget_token)
286 		rq->q->mq_ops->set_rq_budget_token(rq, token);
287 }
288 
289 static inline int blk_mq_get_rq_budget_token(struct request *rq)
290 {
291 	if (rq->q->mq_ops->get_rq_budget_token)
292 		return rq->q->mq_ops->get_rq_budget_token(rq);
293 	return -1;
294 }
295 
296 static inline void __blk_mq_add_active_requests(struct blk_mq_hw_ctx *hctx,
297 						int val)
298 {
299 	if (blk_mq_is_shared_tags(hctx->flags))
300 		atomic_add(val, &hctx->queue->nr_active_requests_shared_tags);
301 	else
302 		atomic_add(val, &hctx->nr_active);
303 }
304 
305 static inline void __blk_mq_inc_active_requests(struct blk_mq_hw_ctx *hctx)
306 {
307 	__blk_mq_add_active_requests(hctx, 1);
308 }
309 
310 static inline void __blk_mq_sub_active_requests(struct blk_mq_hw_ctx *hctx,
311 		int val)
312 {
313 	if (blk_mq_is_shared_tags(hctx->flags))
314 		atomic_sub(val, &hctx->queue->nr_active_requests_shared_tags);
315 	else
316 		atomic_sub(val, &hctx->nr_active);
317 }
318 
319 static inline void __blk_mq_dec_active_requests(struct blk_mq_hw_ctx *hctx)
320 {
321 	__blk_mq_sub_active_requests(hctx, 1);
322 }
323 
324 static inline void blk_mq_add_active_requests(struct blk_mq_hw_ctx *hctx,
325 					      int val)
326 {
327 	if (hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
328 		__blk_mq_add_active_requests(hctx, val);
329 }
330 
331 static inline void blk_mq_inc_active_requests(struct blk_mq_hw_ctx *hctx)
332 {
333 	if (hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
334 		__blk_mq_inc_active_requests(hctx);
335 }
336 
337 static inline void blk_mq_sub_active_requests(struct blk_mq_hw_ctx *hctx,
338 					      int val)
339 {
340 	if (hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
341 		__blk_mq_sub_active_requests(hctx, val);
342 }
343 
344 static inline void blk_mq_dec_active_requests(struct blk_mq_hw_ctx *hctx)
345 {
346 	if (hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
347 		__blk_mq_dec_active_requests(hctx);
348 }
349 
350 static inline int __blk_mq_active_requests(struct blk_mq_hw_ctx *hctx)
351 {
352 	if (blk_mq_is_shared_tags(hctx->flags))
353 		return atomic_read(&hctx->queue->nr_active_requests_shared_tags);
354 	return atomic_read(&hctx->nr_active);
355 }
356 static inline void __blk_mq_put_driver_tag(struct blk_mq_hw_ctx *hctx,
357 					   struct request *rq)
358 {
359 	blk_mq_dec_active_requests(hctx);
360 	blk_mq_put_tag(hctx->tags, rq->mq_ctx, rq->tag);
361 	rq->tag = BLK_MQ_NO_TAG;
362 }
363 
364 static inline void blk_mq_put_driver_tag(struct request *rq)
365 {
366 	if (rq->tag == BLK_MQ_NO_TAG || rq->internal_tag == BLK_MQ_NO_TAG)
367 		return;
368 
369 	__blk_mq_put_driver_tag(rq->mq_hctx, rq);
370 }
371 
372 bool __blk_mq_alloc_driver_tag(struct request *rq);
373 
374 static inline bool blk_mq_get_driver_tag(struct request *rq)
375 {
376 	if (rq->tag == BLK_MQ_NO_TAG && !__blk_mq_alloc_driver_tag(rq))
377 		return false;
378 
379 	return true;
380 }
381 
382 static inline void blk_mq_clear_mq_map(struct blk_mq_queue_map *qmap)
383 {
384 	int cpu;
385 
386 	for_each_possible_cpu(cpu)
387 		qmap->mq_map[cpu] = 0;
388 }
389 
390 /* Free all requests on the list */
391 static inline void blk_mq_free_requests(struct list_head *list)
392 {
393 	while (!list_empty(list)) {
394 		struct request *rq = list_entry_rq(list->next);
395 
396 		list_del_init(&rq->queuelist);
397 		blk_mq_free_request(rq);
398 	}
399 }
400 
401 /*
402  * For shared tag users, we track the number of currently active users
403  * and attempt to provide a fair share of the tag depth for each of them.
404  */
405 static inline bool hctx_may_queue(struct blk_mq_hw_ctx *hctx,
406 				  struct sbitmap_queue *bt)
407 {
408 	unsigned int depth, users;
409 
410 	if (!hctx || !(hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED))
411 		return true;
412 
413 	/*
414 	 * Don't try dividing an ant
415 	 */
416 	if (bt->sb.depth == 1)
417 		return true;
418 
419 	if (blk_mq_is_shared_tags(hctx->flags)) {
420 		struct request_queue *q = hctx->queue;
421 
422 		if (!test_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags))
423 			return true;
424 	} else {
425 		if (!test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
426 			return true;
427 	}
428 
429 	users = READ_ONCE(hctx->tags->active_queues);
430 	if (!users)
431 		return true;
432 
433 	/*
434 	 * Allow at least some tags
435 	 */
436 	depth = max((bt->sb.depth + users - 1) / users, 4U);
437 	return __blk_mq_active_requests(hctx) < depth;
438 }
439 
440 /* run the code block in @dispatch_ops with rcu/srcu read lock held */
441 #define __blk_mq_run_dispatch_ops(q, check_sleep, dispatch_ops)	\
442 do {								\
443 	if ((q)->tag_set->flags & BLK_MQ_F_BLOCKING) {		\
444 		struct blk_mq_tag_set *__tag_set = (q)->tag_set; \
445 		int srcu_idx;					\
446 								\
447 		might_sleep_if(check_sleep);			\
448 		srcu_idx = srcu_read_lock(__tag_set->srcu);	\
449 		(dispatch_ops);					\
450 		srcu_read_unlock(__tag_set->srcu, srcu_idx);	\
451 	} else {						\
452 		rcu_read_lock();				\
453 		(dispatch_ops);					\
454 		rcu_read_unlock();				\
455 	}							\
456 } while (0)
457 
458 #define blk_mq_run_dispatch_ops(q, dispatch_ops)		\
459 	__blk_mq_run_dispatch_ops(q, true, dispatch_ops)	\
460 
461 static inline bool blk_mq_can_poll(struct request_queue *q)
462 {
463 	return (q->limits.features & BLK_FEAT_POLL) &&
464 		q->tag_set->map[HCTX_TYPE_POLL].nr_queues;
465 }
466 
467 #endif
468