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;