From nobody Sun Feb 8 01:29:49 2026 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 8F472EB64DA for ; Mon, 26 Jun 2023 05:06:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229556AbjFZFGH (ORCPT ); Mon, 26 Jun 2023 01:06:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229481AbjFZFGA (ORCPT ); Mon, 26 Jun 2023 01:06:00 -0400 Received: from out-11.mta0.migadu.com (out-11.mta0.migadu.com [91.218.175.11]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EA06611C for ; Sun, 25 Jun 2023 22:05:58 -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=1687755957; 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=pM/RdRZgsIDACaBVp4cmgZyrhE7a7KC7/ymGEmkl5Ls=; b=qqmlYU33idi+L25aeu0BxjrOCMeoS6+/x3Xh4aumx5bgSi8bY5S2o+ygYcNgvpqpgGtNa2 PER3wbn148AqyWu7JR/54qzmjJA6KJa4uhXdAd+7pFyMB+quNHtv4qZLvycK+iOB8oJ8h7 ah8HEUHyzNeBz97525DRwiDkqu8MhIM= From: chengming.zhou@linux.dev To: axboe@kernel.dk, tj@kernel.org Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, zhouchengming@bytedance.com Subject: [PATCH v2 1/3] blk-mq: always use __blk_mq_alloc_requests() to alloc and init rq Date: Mon, 26 Jun 2023 13:04:03 +0800 Message-Id: <20230626050405.781253-2-chengming.zhou@linux.dev> In-Reply-To: <20230626050405.781253-1-chengming.zhou@linux.dev> References: <20230626050405.781253-1-chengming.zhou@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Chengming Zhou This patch is preparation for the next patch that ktime_get_ns() only once for batched pre-allocated requests start_time_ns setting. 1. data->flags is input for blk_mq_rq_ctx_init(), shouldn't update in every blk_mq_rq_ctx_init() in batched requests alloc. So put the data->flags initialization in the caller. 2. make blk_mq_alloc_request_hctx() to reuse __blk_mq_alloc_requests(), instead of directly using blk_mq_rq_ctx_init() by itself, so avoid doing the same data->flags initialization in it. After these cleanup, __blk_mq_alloc_requests() is the only entry to alloc and init rq. 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_m= q_alloc_data *data, rq->mq_ctx =3D ctx; rq->mq_hctx =3D hctx; rq->cmd_flags =3D data->cmd_flags; - - if (data->flags & BLK_MQ_REQ_PM) - data->rq_flags |=3D RQF_PM; - if (blk_queue_io_stat(q)) - data->rq_flags |=3D RQF_IO_STAT; rq->rq_flags =3D data->rq_flags; =20 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 |=3D BLK_MQ_REQ_NOWAIT; =20 + if (data->flags & BLK_MQ_REQ_RESERVED) + data->rq_flags |=3D RQF_RESV; + + if (data->flags & BLK_MQ_REQ_PM) + data->rq_flags |=3D RQF_PM; + + if (blk_queue_io_stat(q)) + data->rq_flags |=3D 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) } =20 retry: - data->ctx =3D blk_mq_get_ctx(q); - data->hctx =3D blk_mq_map_queue(q, data->cmd_flags, data->ctx); + /* See blk_mq_alloc_request_hctx() for details */ + if (!data->ctx) { + data->ctx =3D blk_mq_get_ctx(q); + data->hctx =3D blk_mq_map_queue(q, data->cmd_flags, data->ctx); + } + if (!(data->rq_flags & RQF_SCHED_TAGS)) blk_mq_tag_busy(data->hctx); =20 - if (data->flags & BLK_MQ_REQ_RESERVED) - data->rq_flags |=3D RQF_RESV; - /* * Try batched alloc if we want more than 1 tag. */ @@ -505,6 +510,7 @@ static struct request *__blk_mq_alloc_requests(struct b= lk_mq_alloc_data *data) * is going away. */ msleep(3); + data->ctx =3D NULL; goto retry; } =20 @@ -613,16 +619,10 @@ struct request *blk_mq_alloc_request_hctx(struct requ= est_queue *q, .cmd_flags =3D opf, .nr_tags =3D 1, }; - u64 alloc_time_ns =3D 0; struct request *rq; unsigned int cpu; - unsigned int tag; int ret; =20 - /* alloc_time includes depth and tag waits */ - if (blk_queue_rq_alloc_time(q)) - alloc_time_ns =3D 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 requ= est_queue *q, goto out_queue_exit; data.ctx =3D __blk_mq_get_ctx(q, cpu); =20 - if (q->elevator) - data.rq_flags |=3D RQF_SCHED_TAGS; - else - blk_mq_tag_busy(data.hctx); - - if (flags & BLK_MQ_REQ_RESERVED) - data.rq_flags |=3D RQF_RESV; - ret =3D -EWOULDBLOCK; - tag =3D blk_mq_get_tag(&data); - if (tag =3D=3D BLK_MQ_NO_TAG) + rq =3D __blk_mq_alloc_requests(&data); + if (!rq) goto out_queue_exit; - rq =3D blk_mq_rq_ctx_init(&data, blk_mq_tags_from_data(&data), tag, - alloc_time_ns); rq->__data_len =3D 0; rq->__sector =3D (sector_t) -1; rq->bio =3D rq->biotail =3D NULL; --=20 2.39.2 From nobody Sun Feb 8 01:29:49 2026 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 E9BF9C001B3 for ; Mon, 26 Jun 2023 05:06:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229597AbjFZFGN (ORCPT ); Mon, 26 Jun 2023 01:06:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51006 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229454AbjFZFGE (ORCPT ); Mon, 26 Jun 2023 01:06:04 -0400 Received: from out-1.mta0.migadu.com (out-1.mta0.migadu.com [91.218.175.1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8AF7EFF for ; Sun, 25 Jun 2023 22:06:02 -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=1687755960; 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=3XFNAPAH6EuvNrQxjh5kQssWIEiKUAzjh/YE9gXkp8o=; b=tD0d+5qYO6crBNQpQPfSbmc2v9paPknwdcNbwEoyIkGwjSY2Mm6x5YvDxNERPAEVzoNRwi nxxUk8jY8PWlstP/NBG1TVDZN6zd3ucM/b5aFqRi7fuvjMFCphadJNP79M02HjnRm05c69 Pvk7oawyS3/6jwLiUdx5sYox438bZlw= From: chengming.zhou@linux.dev To: axboe@kernel.dk, tj@kernel.org Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, zhouchengming@bytedance.com Subject: [PATCH v2 2/3] blk-mq: ktime_get_ns() only once for batched requests init Date: Mon, 26 Jun 2023 13:04:04 +0800 Message-Id: <20230626050405.781253-3-chengming.zhou@linux.dev> In-Reply-To: <20230626050405.781253-1-chengming.zhou@linux.dev> References: <20230626050405.781253-1-chengming.zhou@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Chengming Zhou Extend blk_mq_rq_ctx_init() to receive start_time_ns, so we can ktime_get_ns() only once for batched requests start_time_ns setting. Since data->rq_flags initialization has been moved to the caller __blk_mq_alloc_requests(), we can use it to check if time is needed. Signed-off-by: Chengming Zhou --- block/blk-mq.c | 23 ++++++++++++++--------- include/linux/blk-mq.h | 6 +++--- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/block/blk-mq.c b/block/blk-mq.c index c50ef953759f..8b981d0a868e 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -338,7 +338,8 @@ void blk_rq_init(struct request_queue *q, struct reques= t *rq) EXPORT_SYMBOL(blk_rq_init); =20 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) + struct blk_mq_tags *tags, unsigned int tag, + u64 alloc_time_ns, u64 start_time_ns) { struct blk_mq_ctx *ctx =3D data->ctx; struct blk_mq_hw_ctx *hctx =3D data->hctx; @@ -360,14 +361,11 @@ static struct request *blk_mq_rq_ctx_init(struct blk_= mq_alloc_data *data, } rq->timeout =3D 0; =20 - if (blk_mq_need_time_stamp(rq)) - rq->start_time_ns =3D ktime_get_ns(); - else - rq->start_time_ns =3D 0; rq->part =3D NULL; #ifdef CONFIG_BLK_RQ_ALLOC_TIME rq->alloc_time_ns =3D alloc_time_ns; #endif + rq->start_time_ns =3D start_time_ns; rq->io_start_time_ns =3D 0; rq->stats_sectors =3D 0; rq->nr_phys_segments =3D 0; @@ -405,11 +403,15 @@ __blk_mq_alloc_requests_batch(struct blk_mq_alloc_dat= a *data, struct request *rq; unsigned long tag_mask; int i, nr =3D 0; + u64 start_time_ns =3D 0; =20 tag_mask =3D blk_mq_get_tags(data, data->nr_tags, &tag_offset); if (unlikely(!tag_mask)) return NULL; =20 + if (blk_mq_need_time_stamp(data->rq_flags)) + start_time_ns =3D ktime_get_ns(); + tags =3D blk_mq_tags_from_data(data); for (i =3D 0; tag_mask; i++) { if (!(tag_mask & (1UL << i))) @@ -417,7 +419,7 @@ __blk_mq_alloc_requests_batch(struct blk_mq_alloc_data = *data, tag =3D tag_offset + i; prefetch(tags->static_rqs[tag]); tag_mask &=3D ~(1UL << i); - rq =3D blk_mq_rq_ctx_init(data, tags, tag, alloc_time_ns); + rq =3D blk_mq_rq_ctx_init(data, tags, tag, alloc_time_ns, start_time_ns); rq_list_add(data->cached_rq, rq); nr++; } @@ -431,7 +433,7 @@ __blk_mq_alloc_requests_batch(struct blk_mq_alloc_data = *data, static struct request *__blk_mq_alloc_requests(struct blk_mq_alloc_data *d= ata) { struct request_queue *q =3D data->q; - u64 alloc_time_ns =3D 0; + u64 alloc_time_ns =3D 0, start_time_ns =3D 0; struct request *rq; unsigned int tag; =20 @@ -514,8 +516,11 @@ static struct request *__blk_mq_alloc_requests(struct = blk_mq_alloc_data *data) goto retry; } =20 + if (blk_mq_need_time_stamp(data->rq_flags)) + start_time_ns =3D ktime_get_ns(); + return blk_mq_rq_ctx_init(data, blk_mq_tags_from_data(data), tag, - alloc_time_ns); + alloc_time_ns, start_time_ns); } =20 static struct request *blk_mq_rq_cache_fill(struct request_queue *q, @@ -1004,7 +1009,7 @@ static inline void __blk_mq_end_request_acct(struct r= equest *rq, u64 now) =20 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()); =20 if (rq->end_io) { 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)); } =20 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 =3D complete; else if (iob->complete !=3D complete) return false; - iob->need_ts |=3D blk_mq_need_time_stamp(req); + iob->need_ts |=3D blk_mq_need_time_stamp(req->rq_flags); rq_list_add(&iob->req_list, req); return true; } --=20 2.39.2 From nobody Sun Feb 8 01:29:49 2026 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 B230FEB64D7 for ; Mon, 26 Jun 2023 05:06:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229635AbjFZFGT (ORCPT ); Mon, 26 Jun 2023 01:06:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51028 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229566AbjFZFGI (ORCPT ); Mon, 26 Jun 2023 01:06:08 -0400 Received: from out-27.mta0.migadu.com (out-27.mta0.migadu.com [91.218.175.27]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9892912D for ; Sun, 25 Jun 2023 22:06:06 -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=1687755963; 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=bQ1f0H8lujgnS2QY9KNbH8M2Bukb16RSvTZHPii0Y5I=; b=lvw7E9knFolsCy1ySMPIvpFMBrc7UR0e6MThfgqnPdjxDNnCU7/EjXABj2lUVUuWS8Coh3 RjIsc8pcy0woXf34Kpr/XsRT/oSF6zr1BCff+csDOdEI248UxN68j6UlvH0Nfe1wjVPHS+ JrfpuqDXNxNpmkEpLr/jNAqFPuLCBnE= From: chengming.zhou@linux.dev To: axboe@kernel.dk, tj@kernel.org Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, zhouchengming@bytedance.com Subject: [PATCH v2 3/3] blk-mq: fix start_time_ns and alloc_time_ns for pre-allocated rq Date: Mon, 26 Jun 2023 13:04:05 +0800 Message-Id: <20230626050405.781253-4-chengming.zhou@linux.dev> In-Reply-To: <20230626050405.781253-1-chengming.zhou@linux.dev> References: <20230626050405.781253-1-chengming.zhou@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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. Note we don't skip setting alloc_time_ns and start_time_ns for all pre-allocated rq, since the first returned rq still need to be set. Signed-off-by: Chengming Zhou --- block/blk-mq.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/block/blk-mq.c b/block/blk-mq.c index 8b981d0a868e..6a3f1b8aaad8 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -337,6 +337,24 @@ void blk_rq_init(struct request_queue *q, struct reque= st *rq) } EXPORT_SYMBOL(blk_rq_init); =20 +/* Set rq alloc and start time when pre-allocated rq is actually used */ +static inline void blk_mq_rq_time_init(struct request_queue *q, struct req= uest *rq) +{ + if (blk_mq_need_time_stamp(rq->rq_flags)) { + u64 now =3D ktime_get_ns(); + +#ifdef CONFIG_BLK_RQ_ALLOC_TIME + /* + * alloc time is only used by iocost for now, + * only possible when blk_mq_need_time_stamp(). + */ + if (blk_queue_rq_alloc_time(q)) + rq->alloc_time_ns =3D now; +#endif + rq->start_time_ns =3D 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, u64 start_time_ns) @@ -575,6 +593,7 @@ static struct request *blk_mq_alloc_cached_request(stru= ct request_queue *q, return NULL; =20 plug->cached_rq =3D rq_list_next(rq); + blk_mq_rq_time_init(q, rq); } =20 rq->cmd_flags =3D opf; @@ -2896,6 +2915,7 @@ static inline struct request *blk_mq_get_cached_reque= st(struct request_queue *q, plug->cached_rq =3D rq_list_next(rq); rq_qos_throttle(q, *bio); =20 + blk_mq_rq_time_init(q, rq); rq->cmd_flags =3D (*bio)->bi_opf; INIT_LIST_HEAD(&rq->queuelist); return rq; --=20 2.39.2