From patchwork Wed Jun 28 12:45:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengming Zhou X-Patchwork-Id: 113850 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp8900668vqr; Wed, 28 Jun 2023 05:54:53 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4rRD2rRbLLlIztNNLh0CZWcWv6IYqAT7n6AurthRGSQ3hjG757l1KcfS0/bcBh1S4U3M2G X-Received: by 2002:a17:907:7d9e:b0:991:bd74:ea3e with SMTP id oz30-20020a1709077d9e00b00991bd74ea3emr7458930ejc.4.1687956893494; Wed, 28 Jun 2023 05:54:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687956893; cv=none; d=google.com; s=arc-20160816; b=IxAualhIBS30ntrg9IOMzmk0ku8LM5VG37n4OQQc1fpPAh67qT901ujgO/hw6dbsfi +AbYcqAucnqH3KDB+s9zHUtoJNfNhlv4WYUNhKyFd3SoqhSt5Yyjc9AzZml/85OFERV7 CcJZY2IFgtU0tYpDkxEbxOdEwiW0WuDgz3nEhilqhz4/WILlOmVimj4CD5OQE+PX8e89 FNHxpDrQ9pzk9RsnZHulodI/ZMvAqtEAcdCGVdAqlutle3BlZSfmrjgq9bOCUe7iylWc g9cU2pqNtQSo4FqZCAle9OfSMopZWVeKYIbzTc0tMgwRu4UI3ZI13Y3frO48Z6U1fnzS RVLA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=pM/RdRZgsIDACaBVp4cmgZyrhE7a7KC7/ymGEmkl5Ls=; fh=lfhyp4TQmAVJIcYCHUoUy74bD0K2M2yBrUVAhHYFDUg=; b=xlmGVbfzr9i69AnE5bznjIoqbM90xj+uNeViPaG2OI9XI8uV/I+gOVfy3//e8FgFUM pl+8GVmnAs73vB9BkjioKWwoZJvCq4Um1g4VxIXIdcy0jX1HEGYUD8Eb2XLHe8Fsp3gt bYMUKBOARvfdcK68KcEdcI0RW9Jcg1JehBdxf/P8Xucn5UDu9vozllgupvlyy24+kX/2 zSNPomGtnTv7xHOgK+IEEKEbPEkiO2t4+XtKKSL34ecalGwpBX+vsDi6mdGiMz+3COPP C8eUOzk3YDz7lJFjDKk7BlDOKPsX+axqFUpptTy4JEddb+N5lUNKSfQK0oQTMu8oduCD ocpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=a5srjzai; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g17-20020a170906869100b0098df1cbe2acsi5112300ejx.997.2023.06.28.05.54.30; Wed, 28 Jun 2023 05:54:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=a5srjzai; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229951AbjF1Mr7 (ORCPT + 99 others); Wed, 28 Jun 2023 08:47:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41870 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231643AbjF1MrY (ORCPT ); Wed, 28 Jun 2023 08:47:24 -0400 Received: from out-10.mta1.migadu.com (out-10.mta1.migadu.com [IPv6:2001:41d0:203:375::a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7979F30F3 for ; Wed, 28 Jun 2023 05:46:18 -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=1687956376; 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=a5srjzaiS6K4nB3ZXnm0AFNJNnvAlJ+SIEb7kdp7kGnt8nR/79si34wBYmtpyqERKJFv8I eVtIL4RSuhZrsSSmG2xZXQp4NhCObzOVQLYI/yxDhZZUg/+b1APrxemcQ5Qm9jkVah+iGX B5753HdUD0P4NpeZ+22N6nKf+HV7Nf4= 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, ming.lei@redhat.com, hch@lst.de Subject: [PATCH v3 1/3] blk-mq: always use __blk_mq_alloc_requests() to alloc and init rq Date: Wed, 28 Jun 2023 20:45:44 +0800 Message-Id: <20230628124546.1056698-2-chengming.zhou@linux.dev> In-Reply-To: <20230628124546.1056698-1-chengming.zhou@linux.dev> References: <20230628124546.1056698-1-chengming.zhou@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1769951087525118139?= X-GMAIL-MSGID: =?utf-8?q?1769951087525118139?= 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_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 Wed Jun 28 12:45:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengming Zhou X-Patchwork-Id: 113849 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp8900656vqr; Wed, 28 Jun 2023 05:54:52 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7iHu6BlSlvgakupkOaOkpJq1r/oogVcobslRLH/2eksc88hiIdEfTszzNmclMSXrV7i67H X-Received: by 2002:aa7:ca55:0:b0:51d:9dee:e3ce with SMTP id j21-20020aa7ca55000000b0051d9deee3cemr4754123edt.8.1687956891760; Wed, 28 Jun 2023 05:54:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687956891; cv=none; d=google.com; s=arc-20160816; b=EfjPzCGFUlq4EA7n+Iojy7m8LzyxN1bwybWbDAEsd1+gqH/II1YULwkpsgc2OQfsuT 4bvbRKtAbaCvazH8j8050WBnr9KxhwOBjE1rxbvIgV4in8unAb6nrN2a3A4IncARN+Z4 VzI5Wo3i6C3zOvod1vV4qK4wkyxxxnmgaZXFrh3ZQYtnGBFR6RHaeE8efgAeaUz12D8D drK4Wsvix7fH72mluBzm3caO9stNNEYzKcnfi8tyye8C+4larovRJdx35wKjzv3g90vw VUxM0fKQkXe/YXKUGsRNmewIoEYz1hNBaHDm3WwvbYmoJ8u0iUBUGnavFMvQkv0n6cKz Pu3Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=3XFNAPAH6EuvNrQxjh5kQssWIEiKUAzjh/YE9gXkp8o=; fh=lfhyp4TQmAVJIcYCHUoUy74bD0K2M2yBrUVAhHYFDUg=; b=Z31ECYbYgLgXNrLG/SyOW0XPqGsAfs8yM8zaX3y1aKFZnVUZRD/VEp6xVV+Ik0VsQ1 VngmikJ2kdwCgiFen/WwZc9A/wVguz539r4qoE35pC5zX5ZllKwnHxcPlF9gqvguaN6k BMxZYmeLPiYArIEvZfGP9MTlnaTyJEfRTVWIzoPFZ2iLqMtR2lFK7SJ6Rx+Zxb7kVR+d 0Tfv8Ec8jr2/qWsFSeiwvNa+vTM1kZrhsq1YnYfVmFEYEbHfnYvuVFgRB4wCH2y3X4xC wYuGxitj/WVDtkWba9c8ynpq1Qf+Qa20ABwpIGEXCJ77BtqARA/6poIZ7d7ug1kdfYSG 23eg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=ZB6x5iMJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m3-20020aa7d343000000b0051be558d4dcsi4847052edr.205.2023.06.28.05.54.28; Wed, 28 Jun 2023 05:54:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=ZB6x5iMJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231196AbjF1MrK (ORCPT + 99 others); Wed, 28 Jun 2023 08:47:10 -0400 Received: from out-20.mta1.migadu.com ([95.215.58.20]:42890 "EHLO out-20.mta1.migadu.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231770AbjF1MqV (ORCPT ); Wed, 28 Jun 2023 08:46:21 -0400 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=1687956379; 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=ZB6x5iMJqA8fmsPlQ9csKa8eBIPh55rqemfIfNxgnDavPid2FV70CgRteGjoqUMd16rJ1U C/6TTcH8RqjGAph+iWU30iPJEa/lm+izi4ZcSndLvXFhJGOMdzX/kNxaS6plfuUlB2M8HD JfQ5E4WnEkjpfYXg5wEuKqhZD6zApL0= 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, ming.lei@redhat.com, hch@lst.de Subject: [PATCH v3 2/3] blk-mq: ktime_get_ns() only once for batched requests init Date: Wed, 28 Jun 2023 20:45:45 +0800 Message-Id: <20230628124546.1056698-3-chengming.zhou@linux.dev> In-Reply-To: <20230628124546.1056698-1-chengming.zhou@linux.dev> References: <20230628124546.1056698-1-chengming.zhou@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1769951086121509222?= X-GMAIL-MSGID: =?utf-8?q?1769951086121509222?= 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 request *rq) EXPORT_SYMBOL(blk_rq_init); 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 = data->ctx; struct blk_mq_hw_ctx *hctx = data->hctx; @@ -360,14 +361,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 = start_time_ns; rq->io_start_time_ns = 0; rq->stats_sectors = 0; rq->nr_phys_segments = 0; @@ -405,11 +403,15 @@ __blk_mq_alloc_requests_batch(struct blk_mq_alloc_data *data, struct request *rq; unsigned long tag_mask; int i, nr = 0; + u64 start_time_ns = 0; tag_mask = blk_mq_get_tags(data, data->nr_tags, &tag_offset); if (unlikely(!tag_mask)) return NULL; + if (blk_mq_need_time_stamp(data->rq_flags)) + start_time_ns = ktime_get_ns(); + tags = blk_mq_tags_from_data(data); for (i = 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 = 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, 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 *data) { struct request_queue *q = data->q; - u64 alloc_time_ns = 0; + u64 alloc_time_ns = 0, start_time_ns = 0; struct request *rq; unsigned int tag; @@ -514,8 +516,11 @@ static struct request *__blk_mq_alloc_requests(struct blk_mq_alloc_data *data) goto retry; } + if (blk_mq_need_time_stamp(data->rq_flags)) + start_time_ns = 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); } 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 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) { 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; } From patchwork Wed Jun 28 12:45:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengming Zhou X-Patchwork-Id: 113854 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp8911500vqr; Wed, 28 Jun 2023 06:08:15 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4D6Ah9LhX8ZMjNEBEJOj5rFvmUGqpDLGc9ngtk2EkEltAHbQ18Lk7DVG6qVcrhpl9E5DV4 X-Received: by 2002:a17:907:318c:b0:987:e230:690 with SMTP id xe12-20020a170907318c00b00987e2300690mr30294240ejb.57.1687957695204; Wed, 28 Jun 2023 06:08:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687957695; cv=none; d=google.com; s=arc-20160816; b=hHGLHaSVBoLaU4oOsGGMRAUxHCXDeLgsxRy1tuZiGvJYsMLrgHkomaUw3+wqOd+MfU Tid8US1WmUbaHvSEbjE0+DxtIgalPjaL5g3qqTNcBZ77HC/G+i5sEsgZjKzGWRpiOHmm 1mIBFenXnv6OPYcVnYgNe/1SKOHHN91kV9fBMqmw8+aDkSOUtr260sz8Ee8QU0FEVCWK S2rui9tidiMPAJogLGDBen/FaU4NJG+T+Cqv9cCUMwVi24nw7jSHZ50/Mnp/VINXn8Rb f3dY5WjqC0SS9cmtu9JHD/wpO7DfSrfG6g0JhV+bvvE+cawpmRcnmxB2W8rsuHdPotOd QIPA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=v+XqY897Bsd8trRe4zaYsQVNZ9l3mE6f4vuEKbcRGH4=; fh=lfhyp4TQmAVJIcYCHUoUy74bD0K2M2yBrUVAhHYFDUg=; b=AHic/RsPsCgklKDYqMu3IDwx6h+bb+ZWHRBy1O//EBuVWWv2EbV96surnvFErAIHDu AgGz6yCfR5EvYedQcAwT/4uX097pRf9PaVBV61a5R8sCpDNpUBU2dkUuDdwQBhVqjj1q LPsjoLASGP16Q1Vq5KVklYq7ScE+wu6Tkyp9n0m5QtgpV3QMiBmomCYs/E6m92xWEoEV WqWXFDKyOoGdGZQOaDg/6rBeiWhYeJiVabdMHuZQqcZZws+4lJjYcUWVZcMTxd+Duswc 4kBjo2aEY0Wa1KO+qq+NePoTkeKsuHz/CKjj36Ijs6alTXTxPFjNrjwtZgwg4kaFaMNS Mglg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=CSTLZSKN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id rl12-20020a170907216c00b0099263f8cdbbsi1074575ejb.175.2023.06.28.06.07.49; Wed, 28 Jun 2023 06:08:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=CSTLZSKN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231594AbjF1MsG (ORCPT + 99 others); Wed, 28 Jun 2023 08:48:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41872 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230121AbjF1MrZ (ORCPT ); Wed, 28 Jun 2023 08:47:25 -0400 Received: from out-44.mta1.migadu.com (out-44.mta1.migadu.com [IPv6:2001:41d0:203:375::2c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D7BF3580 for ; Wed, 28 Jun 2023 05:46:25 -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=1687956383; 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=v+XqY897Bsd8trRe4zaYsQVNZ9l3mE6f4vuEKbcRGH4=; b=CSTLZSKNj6ys56vnYR2av9Ny99Lqz1lGsQthqeKYSIcQ6AA/1ibqgqVW9kWf815EDeArpi 9C1qTXEzPKb0JJNdsulSf4a5ulx+7FvHM42bKdtMAxak2qdoSqhvzC2EOfZVQn+aQOxbcQ CHFXZ5JpciTdAFydsQibQI+8YlT8oIk= 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, ming.lei@redhat.com, hch@lst.de Subject: [PATCH v3 3/3] blk-mq: fix start_time_ns and alloc_time_ns for pre-allocated rq Date: Wed, 28 Jun 2023 20:45:46 +0800 Message-Id: <20230628124546.1056698-4-chengming.zhou@linux.dev> In-Reply-To: <20230628124546.1056698-1-chengming.zhou@linux.dev> References: <20230628124546.1056698-1-chengming.zhou@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1769951928397701391?= X-GMAIL-MSGID: =?utf-8?q?1769951928397701391?= 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 | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/block/blk-mq.c b/block/blk-mq.c index 8b981d0a868e..55a2e600f943 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 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 request *rq) +{ + if (blk_mq_need_time_stamp(rq->rq_flags)) { + u64 now = 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 = 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, u64 start_time_ns) @@ -395,23 +413,18 @@ 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; struct request *rq; unsigned long tag_mask; int i, nr = 0; - u64 start_time_ns = 0; tag_mask = blk_mq_get_tags(data, data->nr_tags, &tag_offset); if (unlikely(!tag_mask)) return NULL; - if (blk_mq_need_time_stamp(data->rq_flags)) - start_time_ns = ktime_get_ns(); - tags = blk_mq_tags_from_data(data); for (i = 0; tag_mask; i++) { if (!(tag_mask & (1UL << i))) @@ -419,7 +432,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, start_time_ns); + rq = blk_mq_rq_ctx_init(data, tags, tag, 0, 0); rq_list_add(data->cached_rq, rq); nr++; } @@ -490,9 +503,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(q, rq); return rq; + } data->nr_tags = 1; } @@ -575,6 +590,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(q, rq); } rq->cmd_flags = opf; @@ -2896,6 +2912,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(q, rq); rq->cmd_flags = (*bio)->bi_opf; INIT_LIST_HEAD(&rq->queuelist); return rq;