xref: /linux/block/blk-mq.h (revision f4b369c6fe0ceaba2da2daff8c9eb415f85926dd)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2320ae51fSJens Axboe #ifndef INT_BLK_MQ_H
3320ae51fSJens Axboe #define INT_BLK_MQ_H
4320ae51fSJens Axboe 
590110e04SChristoph Hellwig #include <linux/blk-mq.h>
6cf43e6beSJens Axboe #include "blk-stat.h"
7cf43e6beSJens Axboe 
824d2f903SChristoph Hellwig struct blk_mq_tag_set;
9b8643372SYu Kuai struct elevator_tags;
1024d2f903SChristoph Hellwig 
111db4909eSMing Lei struct blk_mq_ctxs {
121db4909eSMing Lei 	struct kobject kobj;
131db4909eSMing Lei 	struct blk_mq_ctx __percpu	*queue_ctx;
141db4909eSMing Lei };
151db4909eSMing Lei 
16fe644072SLinus Walleij /**
17fe644072SLinus Walleij  * struct blk_mq_ctx - State for a software queue facing the submitting CPUs
18fe644072SLinus Walleij  */
19320ae51fSJens Axboe struct blk_mq_ctx {
20320ae51fSJens Axboe 	struct {
21320ae51fSJens Axboe 		spinlock_t		lock;
22c16d6b5aSMing Lei 		struct list_head	rq_lists[HCTX_MAX_TYPES];
23320ae51fSJens Axboe 	} ____cacheline_aligned_in_smp;
24320ae51fSJens Axboe 
25320ae51fSJens Axboe 	unsigned int		cpu;
26f31967f0SJens Axboe 	unsigned short		index_hw[HCTX_MAX_TYPES];
278ccdf4a3SJianchao Wang 	struct blk_mq_hw_ctx 	*hctxs[HCTX_MAX_TYPES];
28320ae51fSJens Axboe 
29320ae51fSJens Axboe 	struct request_queue	*queue;
301db4909eSMing Lei 	struct blk_mq_ctxs      *ctxs;
31320ae51fSJens Axboe 	struct kobject		kobj;
324bb659b1SJens Axboe } ____cacheline_aligned_in_smp;
33320ae51fSJens Axboe 
34bebe84ebSChristoph Hellwig enum {
35bebe84ebSChristoph Hellwig 	BLK_MQ_NO_TAG		= -1U,
36bebe84ebSChristoph Hellwig 	BLK_MQ_TAG_MIN		= 1,
37bebe84ebSChristoph Hellwig 	BLK_MQ_TAG_MAX		= BLK_MQ_NO_TAG - 1,
38bebe84ebSChristoph Hellwig };
39bebe84ebSChristoph Hellwig 
403dff6155SJohn Garry #define BLK_MQ_CPU_WORK_BATCH	(8)
413dff6155SJohn Garry 
42710fa378SChristoph Hellwig typedef unsigned int __bitwise blk_insert_t;
43710fa378SChristoph Hellwig #define BLK_MQ_INSERT_AT_HEAD		((__force blk_insert_t)0x01)
44710fa378SChristoph Hellwig 
453e08773cSChristoph Hellwig void blk_mq_submit_bio(struct bio *bio);
465a72e899SJens Axboe int blk_mq_poll(struct request_queue *q, blk_qc_t cookie, struct io_comp_batch *iob,
475a72e899SJens Axboe 		unsigned int flags);
48c7e2d94bSMing Lei void blk_mq_exit_queue(struct request_queue *q);
49b8643372SYu Kuai struct elevator_tags *blk_mq_update_nr_requests(struct request_queue *q,
50b8643372SYu Kuai 						struct elevator_tags *tags,
51b8643372SYu Kuai 						unsigned int nr);
52aed3ea94SJens Axboe void blk_mq_wake_waiters(struct request_queue *q);
531fd40b5eSMing Lei bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *hctx, struct list_head *,
54e093b784SBart Van Assche 			     bool);
552c3ad667SJens Axboe void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list);
56b347689fSMing Lei struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx,
57b347689fSMing Lei 					struct blk_mq_ctx *start);
582e315dc0SMing Lei void blk_mq_put_rq_ref(struct request *rq);
592c3ad667SJens Axboe 
602c3ad667SJens Axboe /*
612c3ad667SJens Axboe  * Internal helpers for allocating/freeing the request map
622c3ad667SJens Axboe  */
63cc71a6f4SJens Axboe void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
642c3ad667SJens Axboe 		     unsigned int hctx_idx);
659ad8e5afSMing Lei void blk_mq_free_rq_map(struct blk_mq_tag_set *set, struct blk_mq_tags *tags);
6663064be1SJohn Garry struct blk_mq_tags *blk_mq_alloc_map_and_rqs(struct blk_mq_tag_set *set,
67cc71a6f4SJens Axboe 				unsigned int hctx_idx, unsigned int depth);
68645db34eSJohn Garry void blk_mq_free_map_and_rqs(struct blk_mq_tag_set *set,
69645db34eSJohn Garry 			     struct blk_mq_tags *tags,
70645db34eSJohn Garry 			     unsigned int hctx_idx);
71396eaf21SMing Lei 
72320ae51fSJens Axboe /*
73320ae51fSJens Axboe  * CPU -> queue mappings
74320ae51fSJens Axboe  */
75ed76e329SJens Axboe extern int blk_mq_hw_queue_to_node(struct blk_mq_queue_map *qmap, unsigned int);
76320ae51fSJens Axboe 
77b3c661b1SJens Axboe /*
78b3c661b1SJens Axboe  * blk_mq_map_queue_type() - map (hctx_type,cpu) to hardware queue
79b3c661b1SJens Axboe  * @q: request queue
80e20ba6e1SChristoph Hellwig  * @type: the hctx type index
81b3c661b1SJens Axboe  * @cpu: CPU
82b3c661b1SJens Axboe  */
blk_mq_map_queue_type(struct request_queue * q,enum hctx_type type,unsigned int cpu)83ff2c5660SJens Axboe static inline struct blk_mq_hw_ctx *blk_mq_map_queue_type(struct request_queue *q,
84e20ba6e1SChristoph Hellwig 							  enum hctx_type type,
85ff2c5660SJens Axboe 							  unsigned int cpu)
86ff2c5660SJens Axboe {
87*4d0e1f21SFengnan Chang 	return queue_hctx((q), (q->tag_set->map[type].mq_map[cpu]));
88b3c661b1SJens Axboe }
89b3c661b1SJens Axboe 
blk_mq_get_hctx_type(blk_opf_t opf)9016458cf3SBart Van Assche static inline enum hctx_type blk_mq_get_hctx_type(blk_opf_t opf)
91b3c661b1SJens Axboe {
92e20ba6e1SChristoph Hellwig 	enum hctx_type type = HCTX_TYPE_DEFAULT;
93b3c661b1SJens Axboe 
94bb94aea1SJianchao Wang 	/*
956ce913feSChristoph Hellwig 	 * The caller ensure that if REQ_POLLED, poll must be enabled.
96bb94aea1SJianchao Wang 	 */
977e923f40SBart Van Assche 	if (opf & REQ_POLLED)
98e20ba6e1SChristoph Hellwig 		type = HCTX_TYPE_POLL;
997e923f40SBart Van Assche 	else if ((opf & REQ_OP_MASK) == REQ_OP_READ)
100e20ba6e1SChristoph Hellwig 		type = HCTX_TYPE_READ;
101b637108aSMing Lei 	return type;
102b637108aSMing Lei }
103e20ba6e1SChristoph Hellwig 
104b637108aSMing Lei /*
105b637108aSMing Lei  * blk_mq_map_queue() - map (cmd_flags,type) to hardware queue
1067e923f40SBart Van Assche  * @opf: operation type (REQ_OP_*) and flags (e.g. REQ_POLLED).
107b637108aSMing Lei  * @ctx: software queue cpu ctx
108b637108aSMing Lei  */
blk_mq_map_queue(blk_opf_t opf,struct blk_mq_ctx * ctx)10961667cb6SGuixin Liu static inline struct blk_mq_hw_ctx *blk_mq_map_queue(blk_opf_t opf,
110b637108aSMing Lei 						     struct blk_mq_ctx *ctx)
111b637108aSMing Lei {
1127e923f40SBart Van Assche 	return ctx->hctxs[blk_mq_get_hctx_type(opf)];
113ff2c5660SJens Axboe }
114ff2c5660SJens Axboe 
115e93ecf60SJens Axboe /*
1166293e336SYu Kuai  * Default to double of smaller one between hw queue_depth and
1176293e336SYu Kuai  * 128, since we don't split into sync/async like the old code
1186293e336SYu Kuai  * did. Additionally, this is a per-hw queue depth.
1196293e336SYu Kuai  */
blk_mq_default_nr_requests(struct blk_mq_tag_set * set)1206293e336SYu Kuai static inline unsigned int blk_mq_default_nr_requests(
1216293e336SYu Kuai 		struct blk_mq_tag_set *set)
1226293e336SYu Kuai {
1236293e336SYu Kuai 	return 2 * min_t(unsigned int, set->queue_depth, BLKDEV_DEFAULT_RQ);
1246293e336SYu Kuai }
1256293e336SYu Kuai 
1266293e336SYu Kuai /*
12767aec14cSJens Axboe  * sysfs helpers
12867aec14cSJens Axboe  */
129737f98cfSMing Lei extern void blk_mq_sysfs_init(struct request_queue *q);
1307ea5fe31SMing Lei extern void blk_mq_sysfs_deinit(struct request_queue *q);
1318682b92eSChristoph Hellwig int blk_mq_sysfs_register(struct gendisk *disk);
1328682b92eSChristoph Hellwig void blk_mq_sysfs_unregister(struct gendisk *disk);
133eaa870f9SChristoph Hellwig int blk_mq_sysfs_register_hctxs(struct request_queue *q);
134eaa870f9SChristoph Hellwig void blk_mq_sysfs_unregister_hctxs(struct request_queue *q);
135868f2f0bSKeith Busch extern void blk_mq_hctx_kobj_init(struct blk_mq_hw_ctx *hctx);
13647c122e3SJens Axboe void blk_mq_free_plug_rqs(struct blk_plug *plug);
137dbb6f764SChristoph Hellwig void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule);
13867aec14cSJens Axboe 
1392a19b28fSMing Lei void blk_mq_cancel_work_sync(struct request_queue *q);
1402a19b28fSMing Lei 
141e09aae7eSMing Lei void blk_mq_release(struct request_queue *q);
142e09aae7eSMing Lei 
__blk_mq_get_ctx(struct request_queue * q,unsigned int cpu)1431aecfe48SMing Lei static inline struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
1441aecfe48SMing Lei 					   unsigned int cpu)
1451aecfe48SMing Lei {
1461aecfe48SMing Lei 	return per_cpu_ptr(q->queue_ctx, cpu);
1471aecfe48SMing Lei }
1481aecfe48SMing Lei 
1491aecfe48SMing Lei /*
1501aecfe48SMing Lei  * This assumes per-cpu software queueing queues. They could be per-node
1511aecfe48SMing Lei  * as well, for instance. For now this is hardcoded as-is. Note that we don't
1521aecfe48SMing Lei  * care about preemption, since we know the ctx's are persistent. This does
1531aecfe48SMing Lei  * mean that we can't rely on ctx always matching the currently running CPU.
1541aecfe48SMing Lei  */
blk_mq_get_ctx(struct request_queue * q)1551aecfe48SMing Lei static inline struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
1561aecfe48SMing Lei {
157c05f4220SBart Van Assche 	return __blk_mq_get_ctx(q, raw_smp_processor_id());
1581aecfe48SMing Lei }
1591aecfe48SMing Lei 
160cb96a42cSMing Lei struct blk_mq_alloc_data {
161cb96a42cSMing Lei 	/* input parameter */
162cb96a42cSMing Lei 	struct request_queue *q;
1639a95e4efSBart Van Assche 	blk_mq_req_flags_t flags;
164229a9287SOmar Sandoval 	unsigned int shallow_depth;
16516458cf3SBart Van Assche 	blk_opf_t cmd_flags;
166ecaf97f4SJens Axboe 	req_flags_t rq_flags;
167cb96a42cSMing Lei 
16847c122e3SJens Axboe 	/* allocate multiple requests/tags in one go */
16947c122e3SJens Axboe 	unsigned int nr_tags;
170a3396b99SChristoph Hellwig 	struct rq_list *cached_rqs;
17147c122e3SJens Axboe 
172cb96a42cSMing Lei 	/* input & output parameter */
173cb96a42cSMing Lei 	struct blk_mq_ctx *ctx;
174cb96a42cSMing Lei 	struct blk_mq_hw_ctx *hctx;
175cb96a42cSMing Lei };
176cb96a42cSMing Lei 
177bebe84ebSChristoph Hellwig struct blk_mq_tags *blk_mq_init_tags(unsigned int nr_tags,
178ce32496eSChristoph Hellwig 		unsigned int reserved_tags, unsigned int flags, int node);
1799ad8e5afSMing Lei void blk_mq_free_tags(struct blk_mq_tag_set *set, struct blk_mq_tags *tags);
180bebe84ebSChristoph Hellwig 
181bebe84ebSChristoph Hellwig unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data);
182bebe84ebSChristoph Hellwig unsigned long blk_mq_get_tags(struct blk_mq_alloc_data *data, int nr_tags,
183bebe84ebSChristoph Hellwig 		unsigned int *offset);
184bebe84ebSChristoph Hellwig void blk_mq_put_tag(struct blk_mq_tags *tags, struct blk_mq_ctx *ctx,
185bebe84ebSChristoph Hellwig 		unsigned int tag);
186bebe84ebSChristoph Hellwig void blk_mq_put_tags(struct blk_mq_tags *tags, int *tag_array, int nr_tags);
187bebe84ebSChristoph Hellwig void blk_mq_tag_resize_shared_tags(struct blk_mq_tag_set *set,
188bebe84ebSChristoph Hellwig 		unsigned int size);
189dc96cefeSYu Kuai void blk_mq_tag_update_sched_shared_tags(struct request_queue *q,
190dc96cefeSYu Kuai 					 unsigned int nr);
191bebe84ebSChristoph Hellwig 
192bebe84ebSChristoph Hellwig void blk_mq_tag_wakeup_all(struct blk_mq_tags *tags, bool);
193bebe84ebSChristoph Hellwig void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_tag_iter_fn *fn,
194bebe84ebSChristoph Hellwig 		void *priv);
195bebe84ebSChristoph Hellwig void blk_mq_all_tag_iter(struct blk_mq_tags *tags, busy_tag_iter_fn *fn,
196bebe84ebSChristoph Hellwig 		void *priv);
197bebe84ebSChristoph Hellwig 
bt_wait_ptr(struct sbitmap_queue * bt,struct blk_mq_hw_ctx * hctx)198bebe84ebSChristoph Hellwig static inline struct sbq_wait_state *bt_wait_ptr(struct sbitmap_queue *bt,
199bebe84ebSChristoph Hellwig 						 struct blk_mq_hw_ctx *hctx)
200bebe84ebSChristoph Hellwig {
201bebe84ebSChristoph Hellwig 	if (!hctx)
202bebe84ebSChristoph Hellwig 		return &bt->ws[0];
203bebe84ebSChristoph Hellwig 	return sbq_wait_ptr(bt, &hctx->wait_index);
204bebe84ebSChristoph Hellwig }
205bebe84ebSChristoph Hellwig 
206bebe84ebSChristoph Hellwig void __blk_mq_tag_busy(struct blk_mq_hw_ctx *);
207bebe84ebSChristoph Hellwig void __blk_mq_tag_idle(struct blk_mq_hw_ctx *);
208bebe84ebSChristoph Hellwig 
blk_mq_tag_busy(struct blk_mq_hw_ctx * hctx)209bebe84ebSChristoph Hellwig static inline void blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)
210bebe84ebSChristoph Hellwig {
211bebe84ebSChristoph Hellwig 	if (hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
212bebe84ebSChristoph Hellwig 		__blk_mq_tag_busy(hctx);
213bebe84ebSChristoph Hellwig }
214bebe84ebSChristoph Hellwig 
blk_mq_tag_idle(struct blk_mq_hw_ctx * hctx)215bebe84ebSChristoph Hellwig static inline void blk_mq_tag_idle(struct blk_mq_hw_ctx *hctx)
216bebe84ebSChristoph Hellwig {
217bebe84ebSChristoph Hellwig 	if (hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
218bebe84ebSChristoph Hellwig 		__blk_mq_tag_idle(hctx);
219bebe84ebSChristoph Hellwig }
220bebe84ebSChristoph Hellwig 
blk_mq_tag_is_reserved(struct blk_mq_tags * tags,unsigned int tag)221bebe84ebSChristoph Hellwig static inline bool blk_mq_tag_is_reserved(struct blk_mq_tags *tags,
222bebe84ebSChristoph Hellwig 					  unsigned int tag)
223bebe84ebSChristoph Hellwig {
224bebe84ebSChristoph Hellwig 	return tag < tags->nr_reserved_tags;
225bebe84ebSChristoph Hellwig }
226bebe84ebSChristoph Hellwig 
blk_mq_is_shared_tags(unsigned int flags)227079a2e3eSJohn Garry static inline bool blk_mq_is_shared_tags(unsigned int flags)
22832bc15afSJohn Garry {
22932bc15afSJohn Garry 	return flags & BLK_MQ_F_TAG_HCTX_SHARED;
23032bc15afSJohn Garry }
23132bc15afSJohn Garry 
blk_mq_tags_from_data(struct blk_mq_alloc_data * data)2324941115bSJens Axboe static inline struct blk_mq_tags *blk_mq_tags_from_data(struct blk_mq_alloc_data *data)
2334941115bSJens Axboe {
234dd6216bbSChristoph Hellwig 	if (data->rq_flags & RQF_SCHED_TAGS)
23556f8da64SJens Axboe 		return data->hctx->sched_tags;
236dd6216bbSChristoph Hellwig 	return data->hctx->tags;
2374941115bSJens Axboe }
2384941115bSJens Axboe 
blk_mq_hctx_stopped(struct blk_mq_hw_ctx * hctx)2395d1b25c1SBart Van Assche static inline bool blk_mq_hctx_stopped(struct blk_mq_hw_ctx *hctx)
2405d1b25c1SBart Van Assche {
24196a9fe64SMuchun Song 	/* Fast path: hardware queue is not stopped most of the time. */
24296a9fe64SMuchun Song 	if (likely(!test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
24396a9fe64SMuchun Song 		return false;
24496a9fe64SMuchun Song 
24596a9fe64SMuchun Song 	/*
24696a9fe64SMuchun Song 	 * This barrier is used to order adding of dispatch list before and
24796a9fe64SMuchun Song 	 * the test of BLK_MQ_S_STOPPED below. Pairs with the memory barrier
24896a9fe64SMuchun Song 	 * in blk_mq_start_stopped_hw_queue() so that dispatch code could
24996a9fe64SMuchun Song 	 * either see BLK_MQ_S_STOPPED is cleared or dispatch list is not
25096a9fe64SMuchun Song 	 * empty to avoid missing dispatching requests.
25196a9fe64SMuchun Song 	 */
25296a9fe64SMuchun Song 	smp_mb();
25396a9fe64SMuchun Song 
2545d1b25c1SBart Van Assche 	return test_bit(BLK_MQ_S_STOPPED, &hctx->state);
2555d1b25c1SBart Van Assche }
2565d1b25c1SBart Van Assche 
blk_mq_hw_queue_mapped(struct blk_mq_hw_ctx * hctx)25719c66e59SMing Lei static inline bool blk_mq_hw_queue_mapped(struct blk_mq_hw_ctx *hctx)
25819c66e59SMing Lei {
25919c66e59SMing Lei 	return hctx->nr_ctx && hctx->tags;
26019c66e59SMing Lei }
26119c66e59SMing Lei 
2626b6c3a97SYu Kuai void blk_mq_in_driver_rw(struct block_device *part, unsigned int inflight[2]);
263f299b7c7SJens Axboe 
blk_mq_put_dispatch_budget(struct request_queue * q,int budget_token)2642a5a24aaSMing Lei static inline void blk_mq_put_dispatch_budget(struct request_queue *q,
2652a5a24aaSMing Lei 					      int budget_token)
266de148297SMing Lei {
267de148297SMing Lei 	if (q->mq_ops->put_budget)
2682a5a24aaSMing Lei 		q->mq_ops->put_budget(q, budget_token);
269de148297SMing Lei }
270de148297SMing Lei 
blk_mq_get_dispatch_budget(struct request_queue * q)2712a5a24aaSMing Lei static inline int blk_mq_get_dispatch_budget(struct request_queue *q)
272de148297SMing Lei {
273de148297SMing Lei 	if (q->mq_ops->get_budget)
27465c76369SMing Lei 		return q->mq_ops->get_budget(q);
2752a5a24aaSMing Lei 	return 0;
2762a5a24aaSMing Lei }
2772a5a24aaSMing Lei 
blk_mq_set_rq_budget_token(struct request * rq,int token)2782a5a24aaSMing Lei static inline void blk_mq_set_rq_budget_token(struct request *rq, int token)
2792a5a24aaSMing Lei {
2802a5a24aaSMing Lei 	if (token < 0)
2812a5a24aaSMing Lei 		return;
2822a5a24aaSMing Lei 
2832a5a24aaSMing Lei 	if (rq->q->mq_ops->set_rq_budget_token)
2842a5a24aaSMing Lei 		rq->q->mq_ops->set_rq_budget_token(rq, token);
2852a5a24aaSMing Lei }
2862a5a24aaSMing Lei 
blk_mq_get_rq_budget_token(struct request * rq)2872a5a24aaSMing Lei static inline int blk_mq_get_rq_budget_token(struct request *rq)
2882a5a24aaSMing Lei {
2892a5a24aaSMing Lei 	if (rq->q->mq_ops->get_rq_budget_token)
2902a5a24aaSMing Lei 		return rq->q->mq_ops->get_rq_budget_token(rq);
2912a5a24aaSMing Lei 	return -1;
292de148297SMing Lei }
293de148297SMing Lei 
__blk_mq_add_active_requests(struct blk_mq_hw_ctx * hctx,int val)294b8643d68SChengming Zhou static inline void __blk_mq_add_active_requests(struct blk_mq_hw_ctx *hctx,
295b8643d68SChengming Zhou 						int val)
296bccf5e26SJohn Garry {
297079a2e3eSJohn Garry 	if (blk_mq_is_shared_tags(hctx->flags))
298b8643d68SChengming Zhou 		atomic_add(val, &hctx->queue->nr_active_requests_shared_tags);
299bccf5e26SJohn Garry 	else
300b8643d68SChengming Zhou 		atomic_add(val, &hctx->nr_active);
301b8643d68SChengming Zhou }
302b8643d68SChengming Zhou 
__blk_mq_inc_active_requests(struct blk_mq_hw_ctx * hctx)303b8643d68SChengming Zhou static inline void __blk_mq_inc_active_requests(struct blk_mq_hw_ctx *hctx)
304b8643d68SChengming Zhou {
305b8643d68SChengming Zhou 	__blk_mq_add_active_requests(hctx, 1);
306bccf5e26SJohn Garry }
307bccf5e26SJohn Garry 
__blk_mq_sub_active_requests(struct blk_mq_hw_ctx * hctx,int val)3083b87c6eaSMing Lei static inline void __blk_mq_sub_active_requests(struct blk_mq_hw_ctx *hctx,
3093b87c6eaSMing Lei 		int val)
310bccf5e26SJohn Garry {
311079a2e3eSJohn Garry 	if (blk_mq_is_shared_tags(hctx->flags))
3123b87c6eaSMing Lei 		atomic_sub(val, &hctx->queue->nr_active_requests_shared_tags);
313bccf5e26SJohn Garry 	else
3143b87c6eaSMing Lei 		atomic_sub(val, &hctx->nr_active);
3153b87c6eaSMing Lei }
3163b87c6eaSMing Lei 
__blk_mq_dec_active_requests(struct blk_mq_hw_ctx * hctx)3173b87c6eaSMing Lei static inline void __blk_mq_dec_active_requests(struct blk_mq_hw_ctx *hctx)
3183b87c6eaSMing Lei {
3193b87c6eaSMing Lei 	__blk_mq_sub_active_requests(hctx, 1);
320bccf5e26SJohn Garry }
321bccf5e26SJohn Garry 
blk_mq_add_active_requests(struct blk_mq_hw_ctx * hctx,int val)322b8643d68SChengming Zhou static inline void blk_mq_add_active_requests(struct blk_mq_hw_ctx *hctx,
323b8643d68SChengming Zhou 					      int val)
324b8643d68SChengming Zhou {
325b8643d68SChengming Zhou 	if (hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
326b8643d68SChengming Zhou 		__blk_mq_add_active_requests(hctx, val);
327b8643d68SChengming Zhou }
328b8643d68SChengming Zhou 
blk_mq_inc_active_requests(struct blk_mq_hw_ctx * hctx)329b8643d68SChengming Zhou static inline void blk_mq_inc_active_requests(struct blk_mq_hw_ctx *hctx)
330b8643d68SChengming Zhou {
331b8643d68SChengming Zhou 	if (hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
332b8643d68SChengming Zhou 		__blk_mq_inc_active_requests(hctx);
333b8643d68SChengming Zhou }
334b8643d68SChengming Zhou 
blk_mq_sub_active_requests(struct blk_mq_hw_ctx * hctx,int val)335b8643d68SChengming Zhou static inline void blk_mq_sub_active_requests(struct blk_mq_hw_ctx *hctx,
336b8643d68SChengming Zhou 					      int val)
337b8643d68SChengming Zhou {
338b8643d68SChengming Zhou 	if (hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
339b8643d68SChengming Zhou 		__blk_mq_sub_active_requests(hctx, val);
340b8643d68SChengming Zhou }
341b8643d68SChengming Zhou 
blk_mq_dec_active_requests(struct blk_mq_hw_ctx * hctx)342b8643d68SChengming Zhou static inline void blk_mq_dec_active_requests(struct blk_mq_hw_ctx *hctx)
343b8643d68SChengming Zhou {
344b8643d68SChengming Zhou 	if (hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
345b8643d68SChengming Zhou 		__blk_mq_dec_active_requests(hctx);
346b8643d68SChengming Zhou }
347b8643d68SChengming Zhou 
__blk_mq_active_requests(struct blk_mq_hw_ctx * hctx)348bccf5e26SJohn Garry static inline int __blk_mq_active_requests(struct blk_mq_hw_ctx *hctx)
349bccf5e26SJohn Garry {
350079a2e3eSJohn Garry 	if (blk_mq_is_shared_tags(hctx->flags))
351079a2e3eSJohn Garry 		return atomic_read(&hctx->queue->nr_active_requests_shared_tags);
352bccf5e26SJohn Garry 	return atomic_read(&hctx->nr_active);
353bccf5e26SJohn Garry }
__blk_mq_put_driver_tag(struct blk_mq_hw_ctx * hctx,struct request * rq)3544e2f62e5SJens Axboe static inline void __blk_mq_put_driver_tag(struct blk_mq_hw_ctx *hctx,
3554e2f62e5SJens Axboe 					   struct request *rq)
3564e2f62e5SJens Axboe {
357b8643d68SChengming Zhou 	blk_mq_dec_active_requests(hctx);
3584e2f62e5SJens Axboe 	blk_mq_put_tag(hctx->tags, rq->mq_ctx, rq->tag);
3594e2f62e5SJens Axboe 	rq->tag = BLK_MQ_NO_TAG;
3604e2f62e5SJens Axboe }
3614e2f62e5SJens Axboe 
blk_mq_put_driver_tag(struct request * rq)3624e2f62e5SJens Axboe static inline void blk_mq_put_driver_tag(struct request *rq)
3634e2f62e5SJens Axboe {
3644e2f62e5SJens Axboe 	if (rq->tag == BLK_MQ_NO_TAG || rq->internal_tag == BLK_MQ_NO_TAG)
3654e2f62e5SJens Axboe 		return;
3664e2f62e5SJens Axboe 
3674e2f62e5SJens Axboe 	__blk_mq_put_driver_tag(rq->mq_hctx, rq);
3684e2f62e5SJens Axboe }
3694e2f62e5SJens Axboe 
370b8643d68SChengming Zhou bool __blk_mq_alloc_driver_tag(struct request *rq);
371a808a9d5SJens Axboe 
blk_mq_get_driver_tag(struct request * rq)372a808a9d5SJens Axboe static inline bool blk_mq_get_driver_tag(struct request *rq)
373a808a9d5SJens Axboe {
374b8643d68SChengming Zhou 	if (rq->tag == BLK_MQ_NO_TAG && !__blk_mq_alloc_driver_tag(rq))
375b8643d68SChengming Zhou 		return false;
376b8643d68SChengming Zhou 
377a808a9d5SJens Axboe 	return true;
378a808a9d5SJens Axboe }
379a808a9d5SJens Axboe 
blk_mq_clear_mq_map(struct blk_mq_queue_map * qmap)380ed76e329SJens Axboe static inline void blk_mq_clear_mq_map(struct blk_mq_queue_map *qmap)
3810da73d00SMinwoo Im {
3820da73d00SMinwoo Im 	int cpu;
3830da73d00SMinwoo Im 
3840da73d00SMinwoo Im 	for_each_possible_cpu(cpu)
385ed76e329SJens Axboe 		qmap->mq_map[cpu] = 0;
3860da73d00SMinwoo Im }
3870da73d00SMinwoo Im 
388fd2ef39cSJan Kara /* Free all requests on the list */
blk_mq_free_requests(struct list_head * list)389fd2ef39cSJan Kara static inline void blk_mq_free_requests(struct list_head *list)
390fd2ef39cSJan Kara {
391fd2ef39cSJan Kara 	while (!list_empty(list)) {
392fd2ef39cSJan Kara 		struct request *rq = list_entry_rq(list->next);
393fd2ef39cSJan Kara 
394fd2ef39cSJan Kara 		list_del_init(&rq->queuelist);
395fd2ef39cSJan Kara 		blk_mq_free_request(rq);
396fd2ef39cSJan Kara 	}
397fd2ef39cSJan Kara }
398fd2ef39cSJan Kara 
399a0235d23SJohn Garry /*
400a0235d23SJohn Garry  * For shared tag users, we track the number of currently active users
401a0235d23SJohn Garry  * and attempt to provide a fair share of the tag depth for each of them.
402a0235d23SJohn Garry  */
hctx_may_queue(struct blk_mq_hw_ctx * hctx,struct sbitmap_queue * bt)403a0235d23SJohn Garry static inline bool hctx_may_queue(struct blk_mq_hw_ctx *hctx,
404a0235d23SJohn Garry 				  struct sbitmap_queue *bt)
405a0235d23SJohn Garry {
406a0235d23SJohn Garry 	unsigned int depth, users;
407a0235d23SJohn Garry 
408a0235d23SJohn Garry 	if (!hctx || !(hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED))
409a0235d23SJohn Garry 		return true;
410a0235d23SJohn Garry 
411a0235d23SJohn Garry 	/*
412a0235d23SJohn Garry 	 * Don't try dividing an ant
413a0235d23SJohn Garry 	 */
414a0235d23SJohn Garry 	if (bt->sb.depth == 1)
415a0235d23SJohn Garry 		return true;
416a0235d23SJohn Garry 
417079a2e3eSJohn Garry 	if (blk_mq_is_shared_tags(hctx->flags)) {
418f1b49fdcSJohn Garry 		struct request_queue *q = hctx->queue;
419f1b49fdcSJohn Garry 
4202569063cSMing Lei 		if (!test_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags))
421f1b49fdcSJohn Garry 			return true;
422f1b49fdcSJohn Garry 	} else {
423f1b49fdcSJohn Garry 		if (!test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
424f1b49fdcSJohn Garry 			return true;
425f1b49fdcSJohn Garry 	}
426f1b49fdcSJohn Garry 
4274f1731dfSYu Kuai 	users = READ_ONCE(hctx->tags->active_queues);
428a0235d23SJohn Garry 	if (!users)
429a0235d23SJohn Garry 		return true;
430a0235d23SJohn Garry 
431a0235d23SJohn Garry 	/*
432a0235d23SJohn Garry 	 * Allow at least some tags
433a0235d23SJohn Garry 	 */
434a0235d23SJohn Garry 	depth = max((bt->sb.depth + users - 1) / users, 4U);
435bccf5e26SJohn Garry 	return __blk_mq_active_requests(hctx) < depth;
436a0235d23SJohn Garry }
437a0235d23SJohn Garry 
4382a904d00SMing Lei /* run the code block in @dispatch_ops with rcu/srcu read lock held */
43941adf531SMing Lei #define __blk_mq_run_dispatch_ops(q, check_sleep, dispatch_ops)	\
4402a904d00SMing Lei do {								\
44180bd4a7aSChristoph Hellwig 	if ((q)->tag_set->flags & BLK_MQ_F_BLOCKING) {		\
44200e885efSChris Leech 		struct blk_mq_tag_set *__tag_set = (q)->tag_set; \
4432a904d00SMing Lei 		int srcu_idx;					\
4442a904d00SMing Lei 								\
44541adf531SMing Lei 		might_sleep_if(check_sleep);			\
44600e885efSChris Leech 		srcu_idx = srcu_read_lock(__tag_set->srcu);	\
4472a904d00SMing Lei 		(dispatch_ops);					\
44800e885efSChris Leech 		srcu_read_unlock(__tag_set->srcu, srcu_idx);	\
44980bd4a7aSChristoph Hellwig 	} else {						\
45080bd4a7aSChristoph Hellwig 		rcu_read_lock();				\
45180bd4a7aSChristoph Hellwig 		(dispatch_ops);					\
45280bd4a7aSChristoph Hellwig 		rcu_read_unlock();				\
4532a904d00SMing Lei 	}							\
4542a904d00SMing Lei } while (0)
455a0235d23SJohn Garry 
45641adf531SMing Lei #define blk_mq_run_dispatch_ops(q, dispatch_ops)		\
45741adf531SMing Lei 	__blk_mq_run_dispatch_ops(q, true, dispatch_ops)	\
45841adf531SMing Lei 
blk_mq_can_poll(struct request_queue * q)459d432c817SChristoph Hellwig static inline bool blk_mq_can_poll(struct request_queue *q)
460d432c817SChristoph Hellwig {
461d432c817SChristoph Hellwig 	return (q->limits.features & BLK_FEAT_POLL) &&
462d432c817SChristoph Hellwig 		q->tag_set->map[HCTX_TYPE_POLL].nr_queues;
463d432c817SChristoph Hellwig }
464d432c817SChristoph Hellwig 
465320ae51fSJens Axboe #endif
466