From patchwork Thu Aug 24 14:44: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: 13364314 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 76323C88CB2 for ; Thu, 24 Aug 2023 14:46:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241743AbjHXOpf (ORCPT ); Thu, 24 Aug 2023 10:45:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51372 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241745AbjHXOp3 (ORCPT ); Thu, 24 Aug 2023 10:45:29 -0400 Received: from out-55.mta1.migadu.com (out-55.mta1.migadu.com [IPv6:2001:41d0:203:375::37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 58CCD1BC5 for ; Thu, 24 Aug 2023 07:45:27 -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=1692888325; 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=WfeZa4Y5jAeM+nwkiT/BS1yQ2iWyllMXdsEwns8FPns=; b=EKJiNVsaft0sHEc9R6T7LMrjABFaeKezxlh5EL18TMxj99tnCeqaL+HKir1bIpvCuj88ql EY91OcF5k/gn2ZYgQx4kYxBZlbVBiM2Fkf2WohB8Bm5XFJx8QrjNdOd0fjIhiStcyFyShC HARTCWpXJhX4r/dYItsWnFfXEba9XnE= From: chengming.zhou@linux.dev To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com, bvanassche@acm.org, kbusch@kernel.org Cc: mst@redhat.com, sagi@grimberg.me, damien.lemoal@opensource.wdc.com, kch@nvidia.com, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, zhouchengming@bytedance.com Subject: [PATCH 5/6] blk-mq: fix potential reorder of request state and deadline Date: Thu, 24 Aug 2023 22:44:02 +0800 Message-ID: <20230824144403.2135739-6-chengming.zhou@linux.dev> In-Reply-To: <20230824144403.2135739-1-chengming.zhou@linux.dev> References: <20230824144403.2135739-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 CPU0 CPU1 blk_mq_start_request() blk_mq_req_expired() WRITE_ONCE(rq->deadline) WRITE_ONCE(rq->state) if (READ_ONCE(rq->state) != IN_FLIGHT) return deadline = READ_ONCE(rq->deadline) If CPU1 speculately reorder rq->deadline LOAD before rq->state, the deadline will be the initial value 0. CPU0 CPU1 blk_mq_start_request() blk_mq_req_expired() deadline = READ_ONCE(rq->deadline) WRITE_ONCE(rq->deadline) WRITE_ONCE(rq->state) if (READ_ONCE(rq->state) != IN_FLIGHT) return Signed-off-by: Chengming Zhou --- block/blk-mq.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/block/blk-mq.c b/block/blk-mq.c index ff1b0f3ab3a8..49cbf826b100 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -1258,6 +1258,8 @@ void blk_mq_start_request(struct request *rq) WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_IDLE); blk_add_timer(rq); + /* Pair with smp_rmb in blk_mq_req_expired(). */ + smp_wmb(); WRITE_ONCE(rq->state, MQ_RQ_IN_FLIGHT); rq->mq_hctx->tags->rqs[rq->tag] = rq; @@ -1568,6 +1570,12 @@ static bool blk_mq_req_expired(struct request *rq, struct blk_expired_data *expi if (rq->rq_flags & RQF_TIMED_OUT) return false; + /* + * Order LOADs of rq->state and rq->deadline, pair with + * smp_wmb in blk_mq_start_request(). + */ + smp_rmb(); + deadline = READ_ONCE(rq->deadline); if (time_after_eq(expired->timeout_start, deadline)) return true;