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 5cf43e6beSJens Axboe #include "blk-stat.h" 6244c65a3SMing Lei #include "blk-mq-tag.h" 7cf43e6beSJens Axboe 824d2f903SChristoph Hellwig struct blk_mq_tag_set; 924d2f903SChristoph Hellwig 101db4909eSMing Lei struct blk_mq_ctxs { 111db4909eSMing Lei struct kobject kobj; 121db4909eSMing Lei struct blk_mq_ctx __percpu *queue_ctx; 131db4909eSMing Lei }; 141db4909eSMing Lei 15fe644072SLinus Walleij /** 16fe644072SLinus Walleij * struct blk_mq_ctx - State for a software queue facing the submitting CPUs 17fe644072SLinus Walleij */ 18320ae51fSJens Axboe struct blk_mq_ctx { 19320ae51fSJens Axboe struct { 20320ae51fSJens Axboe spinlock_t lock; 21c16d6b5aSMing Lei struct list_head rq_lists[HCTX_MAX_TYPES]; 22320ae51fSJens Axboe } ____cacheline_aligned_in_smp; 23320ae51fSJens Axboe 24320ae51fSJens Axboe unsigned int cpu; 25f31967f0SJens Axboe unsigned short index_hw[HCTX_MAX_TYPES]; 268ccdf4a3SJianchao Wang struct blk_mq_hw_ctx *hctxs[HCTX_MAX_TYPES]; 27320ae51fSJens Axboe 28320ae51fSJens Axboe /* incremented at dispatch time */ 29320ae51fSJens Axboe unsigned long rq_dispatched[2]; 30320ae51fSJens Axboe unsigned long rq_merged; 31320ae51fSJens Axboe 32320ae51fSJens Axboe /* incremented at completion time */ 33320ae51fSJens Axboe unsigned long ____cacheline_aligned_in_smp rq_completed[2]; 34320ae51fSJens Axboe 35320ae51fSJens Axboe struct request_queue *queue; 361db4909eSMing Lei struct blk_mq_ctxs *ctxs; 37320ae51fSJens Axboe struct kobject kobj; 384bb659b1SJens Axboe } ____cacheline_aligned_in_smp; 39320ae51fSJens Axboe 40c7e2d94bSMing Lei void blk_mq_exit_queue(struct request_queue *q); 41e3a2b3f9SJens Axboe int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr); 42aed3ea94SJens Axboe void blk_mq_wake_waiters(struct request_queue *q); 431fd40b5eSMing Lei bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *hctx, struct list_head *, 441fd40b5eSMing Lei unsigned int); 45e6c98712SBart Van Assche void blk_mq_add_to_requeue_list(struct request *rq, bool at_head, 46e6c98712SBart Van Assche bool kick_requeue_list); 472c3ad667SJens Axboe void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list); 48b347689fSMing Lei struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx, 49b347689fSMing Lei struct blk_mq_ctx *start); 502e315dc0SMing Lei void blk_mq_put_rq_ref(struct request *rq); 512c3ad667SJens Axboe 522c3ad667SJens Axboe /* 532c3ad667SJens Axboe * Internal helpers for allocating/freeing the request map 542c3ad667SJens Axboe */ 55cc71a6f4SJens Axboe void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags, 562c3ad667SJens Axboe unsigned int hctx_idx); 571c0706a7SJohn Garry void blk_mq_free_rq_map(struct blk_mq_tags *tags, unsigned int flags); 58*63064be1SJohn Garry struct blk_mq_tags *blk_mq_alloc_map_and_rqs(struct blk_mq_tag_set *set, 59cc71a6f4SJens Axboe unsigned int hctx_idx, unsigned int depth); 602c3ad667SJens Axboe 612c3ad667SJens Axboe /* 622c3ad667SJens Axboe * Internal helpers for request insertion into sw queues 632c3ad667SJens Axboe */ 642c3ad667SJens Axboe void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq, 652c3ad667SJens Axboe bool at_head); 6601e99aecSMing Lei void blk_mq_request_bypass_insert(struct request *rq, bool at_head, 6701e99aecSMing Lei bool run_queue); 68bd166ef1SJens Axboe void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx, 69bd166ef1SJens Axboe struct list_head *list); 70320ae51fSJens Axboe 71fd9c40f6SBart Van Assche /* Used by blk_insert_cloned_request() to issue request directly */ 72fd9c40f6SBart Van Assche blk_status_t blk_mq_request_issue_directly(struct request *rq, bool last); 736ce3dd6eSMing Lei void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx, 746ce3dd6eSMing Lei struct list_head *list); 75396eaf21SMing Lei 76320ae51fSJens Axboe /* 77320ae51fSJens Axboe * CPU -> queue mappings 78320ae51fSJens Axboe */ 79ed76e329SJens Axboe extern int blk_mq_hw_queue_to_node(struct blk_mq_queue_map *qmap, unsigned int); 80320ae51fSJens Axboe 81b3c661b1SJens Axboe /* 82b3c661b1SJens Axboe * blk_mq_map_queue_type() - map (hctx_type,cpu) to hardware queue 83b3c661b1SJens Axboe * @q: request queue 84e20ba6e1SChristoph Hellwig * @type: the hctx type index 85b3c661b1SJens Axboe * @cpu: CPU 86b3c661b1SJens Axboe */ 87ff2c5660SJens Axboe static inline struct blk_mq_hw_ctx *blk_mq_map_queue_type(struct request_queue *q, 88e20ba6e1SChristoph Hellwig enum hctx_type type, 89ff2c5660SJens Axboe unsigned int cpu) 90ff2c5660SJens Axboe { 91e20ba6e1SChristoph Hellwig return q->queue_hw_ctx[q->tag_set->map[type].mq_map[cpu]]; 92b3c661b1SJens Axboe } 93b3c661b1SJens Axboe 94b3c661b1SJens Axboe /* 95b3c661b1SJens Axboe * blk_mq_map_queue() - map (cmd_flags,type) to hardware queue 96b3c661b1SJens Axboe * @q: request queue 97b3c661b1SJens Axboe * @flags: request command flags 98d220a214SMinwoo Im * @ctx: software queue cpu ctx 99b3c661b1SJens Axboe */ 100b3c661b1SJens Axboe static inline struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, 101b3c661b1SJens Axboe unsigned int flags, 1028ccdf4a3SJianchao Wang struct blk_mq_ctx *ctx) 103b3c661b1SJens Axboe { 104e20ba6e1SChristoph Hellwig enum hctx_type type = HCTX_TYPE_DEFAULT; 105b3c661b1SJens Axboe 106bb94aea1SJianchao Wang /* 107bb94aea1SJianchao Wang * The caller ensure that if REQ_HIPRI, poll must be enabled. 108bb94aea1SJianchao Wang */ 109bb94aea1SJianchao Wang if (flags & REQ_HIPRI) 110e20ba6e1SChristoph Hellwig type = HCTX_TYPE_POLL; 111bb94aea1SJianchao Wang else if ((flags & REQ_OP_MASK) == REQ_OP_READ) 112e20ba6e1SChristoph Hellwig type = HCTX_TYPE_READ; 113e20ba6e1SChristoph Hellwig 1148ccdf4a3SJianchao Wang return ctx->hctxs[type]; 115ff2c5660SJens Axboe } 116ff2c5660SJens Axboe 117e93ecf60SJens Axboe /* 11867aec14cSJens Axboe * sysfs helpers 11967aec14cSJens Axboe */ 120737f98cfSMing Lei extern void blk_mq_sysfs_init(struct request_queue *q); 1217ea5fe31SMing Lei extern void blk_mq_sysfs_deinit(struct request_queue *q); 1222d0364c8SBart Van Assche extern int __blk_mq_register_dev(struct device *dev, struct request_queue *q); 12367aec14cSJens Axboe extern int blk_mq_sysfs_register(struct request_queue *q); 12467aec14cSJens Axboe extern void blk_mq_sysfs_unregister(struct request_queue *q); 125868f2f0bSKeith Busch extern void blk_mq_hctx_kobj_init(struct blk_mq_hw_ctx *hctx); 12667aec14cSJens Axboe 127e09aae7eSMing Lei void blk_mq_release(struct request_queue *q); 128e09aae7eSMing Lei 1291aecfe48SMing Lei static inline struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q, 1301aecfe48SMing Lei unsigned int cpu) 1311aecfe48SMing Lei { 1321aecfe48SMing Lei return per_cpu_ptr(q->queue_ctx, cpu); 1331aecfe48SMing Lei } 1341aecfe48SMing Lei 1351aecfe48SMing Lei /* 1361aecfe48SMing Lei * This assumes per-cpu software queueing queues. They could be per-node 1371aecfe48SMing Lei * as well, for instance. For now this is hardcoded as-is. Note that we don't 1381aecfe48SMing Lei * care about preemption, since we know the ctx's are persistent. This does 1391aecfe48SMing Lei * mean that we can't rely on ctx always matching the currently running CPU. 1401aecfe48SMing Lei */ 1411aecfe48SMing Lei static inline struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q) 1421aecfe48SMing Lei { 143c05f4220SBart Van Assche return __blk_mq_get_ctx(q, raw_smp_processor_id()); 1441aecfe48SMing Lei } 1451aecfe48SMing Lei 146cb96a42cSMing Lei struct blk_mq_alloc_data { 147cb96a42cSMing Lei /* input parameter */ 148cb96a42cSMing Lei struct request_queue *q; 1499a95e4efSBart Van Assche blk_mq_req_flags_t flags; 150229a9287SOmar Sandoval unsigned int shallow_depth; 151f9afca4dSJens Axboe unsigned int cmd_flags; 152cb96a42cSMing Lei 153cb96a42cSMing Lei /* input & output parameter */ 154cb96a42cSMing Lei struct blk_mq_ctx *ctx; 155cb96a42cSMing Lei struct blk_mq_hw_ctx *hctx; 156cb96a42cSMing Lei }; 157cb96a42cSMing Lei 15832bc15afSJohn Garry static inline bool blk_mq_is_sbitmap_shared(unsigned int flags) 15932bc15afSJohn Garry { 16032bc15afSJohn Garry return flags & BLK_MQ_F_TAG_HCTX_SHARED; 16132bc15afSJohn Garry } 16232bc15afSJohn Garry 1634941115bSJens Axboe static inline struct blk_mq_tags *blk_mq_tags_from_data(struct blk_mq_alloc_data *data) 1644941115bSJens Axboe { 16542fdc5e4SChristoph Hellwig if (data->q->elevator) 166bd166ef1SJens Axboe return data->hctx->sched_tags; 167bd166ef1SJens Axboe 1684941115bSJens Axboe return data->hctx->tags; 1694941115bSJens Axboe } 1704941115bSJens Axboe 1715d1b25c1SBart Van Assche static inline bool blk_mq_hctx_stopped(struct blk_mq_hw_ctx *hctx) 1725d1b25c1SBart Van Assche { 1735d1b25c1SBart Van Assche return test_bit(BLK_MQ_S_STOPPED, &hctx->state); 1745d1b25c1SBart Van Assche } 1755d1b25c1SBart Van Assche 17619c66e59SMing Lei static inline bool blk_mq_hw_queue_mapped(struct blk_mq_hw_ctx *hctx) 17719c66e59SMing Lei { 17819c66e59SMing Lei return hctx->nr_ctx && hctx->tags; 17919c66e59SMing Lei } 18019c66e59SMing Lei 1818446fe92SChristoph Hellwig unsigned int blk_mq_in_flight(struct request_queue *q, 1828446fe92SChristoph Hellwig struct block_device *part); 1838446fe92SChristoph Hellwig void blk_mq_in_flight_rw(struct request_queue *q, struct block_device *part, 184bf0ddabaSOmar Sandoval unsigned int inflight[2]); 185f299b7c7SJens Axboe 1862a5a24aaSMing Lei static inline void blk_mq_put_dispatch_budget(struct request_queue *q, 1872a5a24aaSMing Lei int budget_token) 188de148297SMing Lei { 189de148297SMing Lei if (q->mq_ops->put_budget) 1902a5a24aaSMing Lei q->mq_ops->put_budget(q, budget_token); 191de148297SMing Lei } 192de148297SMing Lei 1932a5a24aaSMing Lei static inline int blk_mq_get_dispatch_budget(struct request_queue *q) 194de148297SMing Lei { 195de148297SMing Lei if (q->mq_ops->get_budget) 19665c76369SMing Lei return q->mq_ops->get_budget(q); 1972a5a24aaSMing Lei return 0; 1982a5a24aaSMing Lei } 1992a5a24aaSMing Lei 2002a5a24aaSMing Lei static inline void blk_mq_set_rq_budget_token(struct request *rq, int token) 2012a5a24aaSMing Lei { 2022a5a24aaSMing Lei if (token < 0) 2032a5a24aaSMing Lei return; 2042a5a24aaSMing Lei 2052a5a24aaSMing Lei if (rq->q->mq_ops->set_rq_budget_token) 2062a5a24aaSMing Lei rq->q->mq_ops->set_rq_budget_token(rq, token); 2072a5a24aaSMing Lei } 2082a5a24aaSMing Lei 2092a5a24aaSMing Lei static inline int blk_mq_get_rq_budget_token(struct request *rq) 2102a5a24aaSMing Lei { 2112a5a24aaSMing Lei if (rq->q->mq_ops->get_rq_budget_token) 2122a5a24aaSMing Lei return rq->q->mq_ops->get_rq_budget_token(rq); 2132a5a24aaSMing Lei return -1; 214de148297SMing Lei } 215de148297SMing Lei 216bccf5e26SJohn Garry static inline void __blk_mq_inc_active_requests(struct blk_mq_hw_ctx *hctx) 217bccf5e26SJohn Garry { 218bccf5e26SJohn Garry if (blk_mq_is_sbitmap_shared(hctx->flags)) 219bccf5e26SJohn Garry atomic_inc(&hctx->queue->nr_active_requests_shared_sbitmap); 220bccf5e26SJohn Garry else 221bccf5e26SJohn Garry atomic_inc(&hctx->nr_active); 222bccf5e26SJohn Garry } 223bccf5e26SJohn Garry 224bccf5e26SJohn Garry static inline void __blk_mq_dec_active_requests(struct blk_mq_hw_ctx *hctx) 225bccf5e26SJohn Garry { 226bccf5e26SJohn Garry if (blk_mq_is_sbitmap_shared(hctx->flags)) 227bccf5e26SJohn Garry atomic_dec(&hctx->queue->nr_active_requests_shared_sbitmap); 228bccf5e26SJohn Garry else 229bccf5e26SJohn Garry atomic_dec(&hctx->nr_active); 230bccf5e26SJohn Garry } 231bccf5e26SJohn Garry 232bccf5e26SJohn Garry static inline int __blk_mq_active_requests(struct blk_mq_hw_ctx *hctx) 233bccf5e26SJohn Garry { 234bccf5e26SJohn Garry if (blk_mq_is_sbitmap_shared(hctx->flags)) 235bccf5e26SJohn Garry return atomic_read(&hctx->queue->nr_active_requests_shared_sbitmap); 236bccf5e26SJohn Garry return atomic_read(&hctx->nr_active); 237bccf5e26SJohn Garry } 2384e2f62e5SJens Axboe static inline void __blk_mq_put_driver_tag(struct blk_mq_hw_ctx *hctx, 2394e2f62e5SJens Axboe struct request *rq) 2404e2f62e5SJens Axboe { 2414e2f62e5SJens Axboe blk_mq_put_tag(hctx->tags, rq->mq_ctx, rq->tag); 2424e2f62e5SJens Axboe rq->tag = BLK_MQ_NO_TAG; 2434e2f62e5SJens Axboe 2444e2f62e5SJens Axboe if (rq->rq_flags & RQF_MQ_INFLIGHT) { 2454e2f62e5SJens Axboe rq->rq_flags &= ~RQF_MQ_INFLIGHT; 246bccf5e26SJohn Garry __blk_mq_dec_active_requests(hctx); 2474e2f62e5SJens Axboe } 2484e2f62e5SJens Axboe } 2494e2f62e5SJens Axboe 2504e2f62e5SJens Axboe static inline void blk_mq_put_driver_tag(struct request *rq) 2514e2f62e5SJens Axboe { 2524e2f62e5SJens Axboe if (rq->tag == BLK_MQ_NO_TAG || rq->internal_tag == BLK_MQ_NO_TAG) 2534e2f62e5SJens Axboe return; 2544e2f62e5SJens Axboe 2554e2f62e5SJens Axboe __blk_mq_put_driver_tag(rq->mq_hctx, rq); 2564e2f62e5SJens Axboe } 2574e2f62e5SJens Axboe 25861347154SJan Kara bool blk_mq_get_driver_tag(struct request *rq); 25961347154SJan Kara 260ed76e329SJens Axboe static inline void blk_mq_clear_mq_map(struct blk_mq_queue_map *qmap) 2610da73d00SMinwoo Im { 2620da73d00SMinwoo Im int cpu; 2630da73d00SMinwoo Im 2640da73d00SMinwoo Im for_each_possible_cpu(cpu) 265ed76e329SJens Axboe qmap->mq_map[cpu] = 0; 2660da73d00SMinwoo Im } 2670da73d00SMinwoo Im 268b49773e7SDamien Le Moal /* 269b49773e7SDamien Le Moal * blk_mq_plug() - Get caller context plug 270b49773e7SDamien Le Moal * @q: request queue 271b49773e7SDamien Le Moal * @bio : the bio being submitted by the caller context 272b49773e7SDamien Le Moal * 273b49773e7SDamien Le Moal * Plugging, by design, may delay the insertion of BIOs into the elevator in 274b49773e7SDamien Le Moal * order to increase BIO merging opportunities. This however can cause BIO 275b49773e7SDamien Le Moal * insertion order to change from the order in which submit_bio() is being 276b49773e7SDamien Le Moal * executed in the case of multiple contexts concurrently issuing BIOs to a 277b49773e7SDamien Le Moal * device, even if these context are synchronized to tightly control BIO issuing 278b49773e7SDamien Le Moal * order. While this is not a problem with regular block devices, this ordering 279b49773e7SDamien Le Moal * change can cause write BIO failures with zoned block devices as these 280b49773e7SDamien Le Moal * require sequential write patterns to zones. Prevent this from happening by 281b49773e7SDamien Le Moal * ignoring the plug state of a BIO issuing context if the target request queue 282b49773e7SDamien Le Moal * is for a zoned block device and the BIO to plug is a write operation. 283b49773e7SDamien Le Moal * 284b49773e7SDamien Le Moal * Return current->plug if the bio can be plugged and NULL otherwise 285b49773e7SDamien Le Moal */ 286b49773e7SDamien Le Moal static inline struct blk_plug *blk_mq_plug(struct request_queue *q, 287b49773e7SDamien Le Moal struct bio *bio) 288b49773e7SDamien Le Moal { 289b49773e7SDamien Le Moal /* 290b49773e7SDamien Le Moal * For regular block devices or read operations, use the context plug 291b49773e7SDamien Le Moal * which may be NULL if blk_start_plug() was not executed. 292b49773e7SDamien Le Moal */ 293b49773e7SDamien Le Moal if (!blk_queue_is_zoned(q) || !op_is_write(bio_op(bio))) 294b49773e7SDamien Le Moal return current->plug; 295b49773e7SDamien Le Moal 296b49773e7SDamien Le Moal /* Zoned block device write operation case: do not plug the BIO */ 297b49773e7SDamien Le Moal return NULL; 298b49773e7SDamien Le Moal } 299b49773e7SDamien Le Moal 300fd2ef39cSJan Kara /* Free all requests on the list */ 301fd2ef39cSJan Kara static inline void blk_mq_free_requests(struct list_head *list) 302fd2ef39cSJan Kara { 303fd2ef39cSJan Kara while (!list_empty(list)) { 304fd2ef39cSJan Kara struct request *rq = list_entry_rq(list->next); 305fd2ef39cSJan Kara 306fd2ef39cSJan Kara list_del_init(&rq->queuelist); 307fd2ef39cSJan Kara blk_mq_free_request(rq); 308fd2ef39cSJan Kara } 309fd2ef39cSJan Kara } 310fd2ef39cSJan Kara 311a0235d23SJohn Garry /* 312a0235d23SJohn Garry * For shared tag users, we track the number of currently active users 313a0235d23SJohn Garry * and attempt to provide a fair share of the tag depth for each of them. 314a0235d23SJohn Garry */ 315a0235d23SJohn Garry static inline bool hctx_may_queue(struct blk_mq_hw_ctx *hctx, 316a0235d23SJohn Garry struct sbitmap_queue *bt) 317a0235d23SJohn Garry { 318a0235d23SJohn Garry unsigned int depth, users; 319a0235d23SJohn Garry 320a0235d23SJohn Garry if (!hctx || !(hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) 321a0235d23SJohn Garry return true; 322a0235d23SJohn Garry 323a0235d23SJohn Garry /* 324a0235d23SJohn Garry * Don't try dividing an ant 325a0235d23SJohn Garry */ 326a0235d23SJohn Garry if (bt->sb.depth == 1) 327a0235d23SJohn Garry return true; 328a0235d23SJohn Garry 329f1b49fdcSJohn Garry if (blk_mq_is_sbitmap_shared(hctx->flags)) { 330f1b49fdcSJohn Garry struct request_queue *q = hctx->queue; 331f1b49fdcSJohn Garry struct blk_mq_tag_set *set = q->tag_set; 332f1b49fdcSJohn Garry 3332569063cSMing Lei if (!test_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags)) 334f1b49fdcSJohn Garry return true; 335f1b49fdcSJohn Garry users = atomic_read(&set->active_queues_shared_sbitmap); 336f1b49fdcSJohn Garry } else { 337f1b49fdcSJohn Garry if (!test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state)) 338f1b49fdcSJohn Garry return true; 339a0235d23SJohn Garry users = atomic_read(&hctx->tags->active_queues); 340f1b49fdcSJohn Garry } 341f1b49fdcSJohn Garry 342a0235d23SJohn Garry if (!users) 343a0235d23SJohn Garry return true; 344a0235d23SJohn Garry 345a0235d23SJohn Garry /* 346a0235d23SJohn Garry * Allow at least some tags 347a0235d23SJohn Garry */ 348a0235d23SJohn Garry depth = max((bt->sb.depth + users - 1) / users, 4U); 349bccf5e26SJohn Garry return __blk_mq_active_requests(hctx) < depth; 350a0235d23SJohn Garry } 351a0235d23SJohn Garry 352a0235d23SJohn Garry 353320ae51fSJens Axboe #endif 354