From patchwork Thu Jun 29 12:13:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengming Zhou X-Patchwork-Id: 13296941 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AE018EB64D9 for ; Thu, 29 Jun 2023 12:14:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232073AbjF2MOA (ORCPT ); Thu, 29 Jun 2023 08:14:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50262 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231865AbjF2MNy (ORCPT ); Thu, 29 Jun 2023 08:13:54 -0400 Received: from out-33.mta1.migadu.com (out-33.mta1.migadu.com [IPv6:2001:41d0:203:375::21]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B295B30C7 for ; Thu, 29 Jun 2023 05:13:53 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1688040831; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PIs+ICQNDa5Vfiyu1UAjrg6pN938Jdy2JxIM6CdLM3k=; b=NFz7GpY3S3aV7jV8xRsur9xzqu6C4Qib21R6jTKJkpaZrJiUA8T5yvi48X0ycq/iBk6OgY 58R8Mp6ZmSf/ViThCrgmYM54zKo6r+gs/HHWzHmZhIMHtEiTN7Abue0iPQSPpEgqWoa6+B nhyB0C469Q0wFK9N6G1ka56gVityIBc= From: chengming.zhou@linux.dev To: axboe@kernel.dk, ming.lei@redhat.com, hch@lst.de, tj@kernel.org Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Chengming Zhou Subject: [PATCH v4 1/2] blk-mq: move data->rq_flags initialization code Date: Thu, 29 Jun 2023 20:13:01 +0800 Message-Id: <20230629121302.1124851-2-chengming.zhou@linux.dev> In-Reply-To: <20230629121302.1124851-1-chengming.zhou@linux.dev> References: <20230629121302.1124851-1-chengming.zhou@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org From: Chengming Zhou The blk_mq_rq_ctx_init() has some data->rq_flags initialization: ``` if (data->flags & BLK_MQ_REQ_PM) data->rq_flags |= RQF_PM; if (blk_queue_io_stat(q)) data->rq_flags |= RQF_IO_STAT; rq->rq_flags = data->rq_flags; ``` We will need this data->rq_flags to tell if we need start_time_ns, so we need to put these initialization in the callers of blk_mq_rq_ctx_init(). Now we basically have two callers, the 1st is general __blk_mq_alloc_requests(), the 2nd is the special blk_mq_alloc_request_hctx(). So change the 2nd caller to reuse the 1st __blk_mq_alloc_requests() to avoid code duplication. This is safe because blk_mq_alloc_request_hctx() always has BLK_MQ_REQ_NOWAIT flag, it won't go into the normal retry path when doesn't have free tag. But it indeed make the general __blk_mq_alloc_requests() more complex. Signed-off-by: Chengming Zhou --- block/blk-mq.c | 46 ++++++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/block/blk-mq.c b/block/blk-mq.c index decb6ab2d508..c50ef953759f 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -349,11 +349,6 @@ static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data, rq->mq_ctx = ctx; rq->mq_hctx = hctx; rq->cmd_flags = data->cmd_flags; - - if (data->flags & BLK_MQ_REQ_PM) - data->rq_flags |= RQF_PM; - if (blk_queue_io_stat(q)) - data->rq_flags |= RQF_IO_STAT; rq->rq_flags = data->rq_flags; if (data->rq_flags & RQF_SCHED_TAGS) { @@ -447,6 +442,15 @@ static struct request *__blk_mq_alloc_requests(struct blk_mq_alloc_data *data) if (data->cmd_flags & REQ_NOWAIT) data->flags |= BLK_MQ_REQ_NOWAIT; + if (data->flags & BLK_MQ_REQ_RESERVED) + data->rq_flags |= RQF_RESV; + + if (data->flags & BLK_MQ_REQ_PM) + data->rq_flags |= RQF_PM; + + if (blk_queue_io_stat(q)) + data->rq_flags |= RQF_IO_STAT; + if (q->elevator) { /* * All requests use scheduler tags when an I/O scheduler is @@ -471,14 +475,15 @@ static struct request *__blk_mq_alloc_requests(struct blk_mq_alloc_data *data) } retry: - data->ctx = blk_mq_get_ctx(q); - data->hctx = blk_mq_map_queue(q, data->cmd_flags, data->ctx); + /* See blk_mq_alloc_request_hctx() for details */ + if (!data->ctx) { + data->ctx = blk_mq_get_ctx(q); + data->hctx = blk_mq_map_queue(q, data->cmd_flags, data->ctx); + } + if (!(data->rq_flags & RQF_SCHED_TAGS)) blk_mq_tag_busy(data->hctx); - if (data->flags & BLK_MQ_REQ_RESERVED) - data->rq_flags |= RQF_RESV; - /* * Try batched alloc if we want more than 1 tag. */ @@ -505,6 +510,7 @@ static struct request *__blk_mq_alloc_requests(struct blk_mq_alloc_data *data) * is going away. */ msleep(3); + data->ctx = NULL; goto retry; } @@ -613,16 +619,10 @@ struct request *blk_mq_alloc_request_hctx(struct request_queue *q, .cmd_flags = opf, .nr_tags = 1, }; - u64 alloc_time_ns = 0; struct request *rq; unsigned int cpu; - unsigned int tag; int ret; - /* alloc_time includes depth and tag waits */ - if (blk_queue_rq_alloc_time(q)) - alloc_time_ns = ktime_get_ns(); - /* * If the tag allocator sleeps we could get an allocation for a * different hardware context. No need to complicate the low level @@ -653,20 +653,10 @@ struct request *blk_mq_alloc_request_hctx(struct request_queue *q, goto out_queue_exit; data.ctx = __blk_mq_get_ctx(q, cpu); - if (q->elevator) - data.rq_flags |= RQF_SCHED_TAGS; - else - blk_mq_tag_busy(data.hctx); - - if (flags & BLK_MQ_REQ_RESERVED) - data.rq_flags |= RQF_RESV; - ret = -EWOULDBLOCK; - tag = blk_mq_get_tag(&data); - if (tag == BLK_MQ_NO_TAG) + rq = __blk_mq_alloc_requests(&data); + if (!rq) goto out_queue_exit; - rq = blk_mq_rq_ctx_init(&data, blk_mq_tags_from_data(&data), tag, - alloc_time_ns); rq->__data_len = 0; rq->__sector = (sector_t) -1; rq->bio = rq->biotail = NULL; From patchwork Thu Jun 29 12:13:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengming Zhou X-Patchwork-Id: 13296942 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DAE00EB64DC for ; Thu, 29 Jun 2023 12:14:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232011AbjF2MOH (ORCPT ); Thu, 29 Jun 2023 08:14:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232060AbjF2MOA (ORCPT ); Thu, 29 Jun 2023 08:14:00 -0400 Received: from out-38.mta1.migadu.com (out-38.mta1.migadu.com [IPv6:2001:41d0:203:375::26]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0D648358B for ; Thu, 29 Jun 2023 05:13:57 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1688040836; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+xfO61CVQK88KgiArAYWZDOZyMihC48YiU9gHQY2Va0=; b=r1G3+cS2ZjO3+13QKYsSzJ/FdnDotCg6MG25x1PCO1qA3EsmJ25mFkfmwoZ8tyEbLrywd6 kG8Y9sHdRBxod+sKC2TuYi79+yJpwlFsFtuQQFonuc+gpqC90ZcUOL117fy+Q4XJ1tLzGF lwkmah3iNxTbOm6uCR8pFLwgG+8OMTo= From: chengming.zhou@linux.dev To: axboe@kernel.dk, ming.lei@redhat.com, hch@lst.de, tj@kernel.org Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Chengming Zhou Subject: [PATCH v4 2/2] blk-mq: fix start_time_ns and alloc_time_ns for pre-allocated rq Date: Thu, 29 Jun 2023 20:13:02 +0800 Message-Id: <20230629121302.1124851-3-chengming.zhou@linux.dev> In-Reply-To: <20230629121302.1124851-1-chengming.zhou@linux.dev> References: <20230629121302.1124851-1-chengming.zhou@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org From: Chengming Zhou The iocost rely on rq start_time_ns and alloc_time_ns to tell saturation state of the block device. Most of the time request is allocated after rq_qos_throttle() and its alloc_time_ns or start_time_ns won't be affected. But for plug batched allocation introduced by the commit 47c122e35d7e ("block: pre-allocate requests if plug is started and is a batch"), we can rq_qos_throttle() after the allocation of the request. This is what the blk_mq_get_cached_request() does. In this case, the cached request alloc_time_ns or start_time_ns is much ahead if blocked in any qos ->throttle(). This patch fix it by setting alloc_time_ns and start_time_ns to now when the pre-allocated rq is actually used. And we skip setting the alloc_time_ns and start_time_ns during pre-allocation, so just pass 0 in __blk_mq_alloc_requests_batch(). Signed-off-by: Chengming Zhou --- block/blk-mq.c | 41 +++++++++++++++++++++++++++++++---------- block/blk-mq.h | 1 + include/linux/blk-mq.h | 6 +++--- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/block/blk-mq.c b/block/blk-mq.c index c50ef953759f..2849395ca4c6 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -337,6 +337,24 @@ void blk_rq_init(struct request_queue *q, struct request *rq) } EXPORT_SYMBOL(blk_rq_init); +/* Set alloc and start time when pre-allocated rq is actually used */ +static inline void blk_mq_rq_time_init(struct request *rq) +{ + if (blk_mq_need_time_stamp(rq->rq_flags)) { + u64 now = ktime_get_ns(); + +#ifdef CONFIG_BLK_RQ_ALLOC_TIME + /* + * The alloc time is only used by iocost for now, + * only possible when blk_mq_need_time_stamp(). + */ + if (blk_queue_rq_alloc_time(rq->q)) + rq->alloc_time_ns = now; +#endif + rq->start_time_ns = now; + } +} + static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data, struct blk_mq_tags *tags, unsigned int tag, u64 alloc_time_ns) { @@ -360,14 +378,11 @@ static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data, } rq->timeout = 0; - if (blk_mq_need_time_stamp(rq)) - rq->start_time_ns = ktime_get_ns(); - else - rq->start_time_ns = 0; rq->part = NULL; #ifdef CONFIG_BLK_RQ_ALLOC_TIME rq->alloc_time_ns = alloc_time_ns; #endif + rq->start_time_ns = data->start_time_ns; rq->io_start_time_ns = 0; rq->stats_sectors = 0; rq->nr_phys_segments = 0; @@ -397,8 +412,7 @@ static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data, } static inline struct request * -__blk_mq_alloc_requests_batch(struct blk_mq_alloc_data *data, - u64 alloc_time_ns) +__blk_mq_alloc_requests_batch(struct blk_mq_alloc_data *data) { unsigned int tag, tag_offset; struct blk_mq_tags *tags; @@ -417,7 +431,7 @@ __blk_mq_alloc_requests_batch(struct blk_mq_alloc_data *data, tag = tag_offset + i; prefetch(tags->static_rqs[tag]); tag_mask &= ~(1UL << i); - rq = blk_mq_rq_ctx_init(data, tags, tag, alloc_time_ns); + rq = blk_mq_rq_ctx_init(data, tags, tag, 0); rq_list_add(data->cached_rq, rq); nr++; } @@ -488,9 +502,11 @@ static struct request *__blk_mq_alloc_requests(struct blk_mq_alloc_data *data) * Try batched alloc if we want more than 1 tag. */ if (data->nr_tags > 1) { - rq = __blk_mq_alloc_requests_batch(data, alloc_time_ns); - if (rq) + rq = __blk_mq_alloc_requests_batch(data); + if (rq) { + blk_mq_rq_time_init(rq); return rq; + } data->nr_tags = 1; } @@ -514,6 +530,9 @@ static struct request *__blk_mq_alloc_requests(struct blk_mq_alloc_data *data) goto retry; } + if (blk_mq_need_time_stamp(data->rq_flags)) + data->start_time_ns = ktime_get_ns(); + return blk_mq_rq_ctx_init(data, blk_mq_tags_from_data(data), tag, alloc_time_ns); } @@ -570,6 +589,7 @@ static struct request *blk_mq_alloc_cached_request(struct request_queue *q, return NULL; plug->cached_rq = rq_list_next(rq); + blk_mq_rq_time_init(rq); } rq->cmd_flags = opf; @@ -1004,7 +1024,7 @@ static inline void __blk_mq_end_request_acct(struct request *rq, u64 now) inline void __blk_mq_end_request(struct request *rq, blk_status_t error) { - if (blk_mq_need_time_stamp(rq)) + if (blk_mq_need_time_stamp(rq->rq_flags)) __blk_mq_end_request_acct(rq, ktime_get_ns()); if (rq->end_io) { @@ -2891,6 +2911,7 @@ static inline struct request *blk_mq_get_cached_request(struct request_queue *q, plug->cached_rq = rq_list_next(rq); rq_qos_throttle(q, *bio); + blk_mq_rq_time_init(rq); rq->cmd_flags = (*bio)->bi_opf; INIT_LIST_HEAD(&rq->queuelist); return rq; diff --git a/block/blk-mq.h b/block/blk-mq.h index 1743857e0b01..29ed4d05110e 100644 --- a/block/blk-mq.h +++ b/block/blk-mq.h @@ -150,6 +150,7 @@ struct blk_mq_alloc_data { unsigned int shallow_depth; blk_opf_t cmd_flags; req_flags_t rq_flags; + u64 start_time_ns; /* allocate multiple requests/tags in one go */ unsigned int nr_tags; diff --git a/include/linux/blk-mq.h b/include/linux/blk-mq.h index f401067ac03a..e8366e9c3388 100644 --- a/include/linux/blk-mq.h +++ b/include/linux/blk-mq.h @@ -834,9 +834,9 @@ void blk_mq_end_request_batch(struct io_comp_batch *ib); * Only need start/end time stamping if we have iostat or * blk stats enabled, or using an IO scheduler. */ -static inline bool blk_mq_need_time_stamp(struct request *rq) +static inline bool blk_mq_need_time_stamp(req_flags_t rq_flags) { - return (rq->rq_flags & (RQF_IO_STAT | RQF_STATS | RQF_USE_SCHED)); + return (rq_flags & (RQF_IO_STAT | RQF_STATS | RQF_USE_SCHED)); } static inline bool blk_mq_is_reserved_rq(struct request *rq) @@ -860,7 +860,7 @@ static inline bool blk_mq_add_to_batch(struct request *req, iob->complete = complete; else if (iob->complete != complete) return false; - iob->need_ts |= blk_mq_need_time_stamp(req); + iob->need_ts |= blk_mq_need_time_stamp(req->rq_flags); rq_list_add(&iob->req_list, req); return true; }