From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA3351D0794; Wed, 2 Oct 2024 14:28:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727879314; cv=none; b=rZeYoLrN3aSHFE5MhOEoiGu54fxyAHwDGDdEgIf+TkpJ6SC+7GcWOxd1wFpHkbWDP33QgUw5MU63LwbwX5dbdNgZlpsowoKmlvLZpGN1tBw60/SZr9fNAK72hAqig87QcALM/ta2ExNUkjr4OudVMV8Vm0JjSXlCEspnzbE497M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727879314; c=relaxed/simple; bh=ILCgJmItzwa/BEHK26Pu33036KO2U594b45+8PlB2XY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SiXDwi/ziDTvDjesCkfSbXJIJo122hSQgZqmC/Q37Rvl9ueD4DGF6bnKWC9CFx/KKipb2CwTeBMKyOGzOpjIQLiUs15dv5SmucW+fCWKmJ6sHp9ezFL530r/cJeD60N4WtHyNeek0yYfbYi1ib5MksCaF1nUNxhKxKZIZRSFr/k= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=ITDXjQzw; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="ITDXjQzw" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 335DEC4CEC2; Wed, 2 Oct 2024 14:28:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1727879314; bh=ILCgJmItzwa/BEHK26Pu33036KO2U594b45+8PlB2XY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ITDXjQzwQzNhdl+50YFKyG6z3CFFfOhFvR5oVpTbXDYfdr585zqgzV1gfzl9y3Ch8 Yvvd1ZpBWfLrehBar6Q37lll5Gwa/bZrYP3QeYCjvXE+zk7kU9RSQ+LSsuQRxbMCmY 6m/LsAC47pnnRg2jVWRlJEoMbgyUG3zoN5uGxR2w= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Josef Bacik , Yu Kuai , Ming Lei , Jens Axboe , Sasha Levin Subject: [PATCH 6.6 077/538] nbd: fix race between timeout and normal completion Date: Wed, 2 Oct 2024 14:55:16 +0200 Message-ID: <20241002125755.257345162@linuxfoundation.org> X-Mailer: git-send-email 2.46.2 In-Reply-To: <20241002125751.964700919@linuxfoundation.org> References: <20241002125751.964700919@linuxfoundation.org> User-Agent: quilt/0.67 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Ming Lei [ Upstream commit c9ea57c91f03bcad415e1a20113bdb2077bcf990 ] If request timetout is handled by nbd_requeue_cmd(), normal completion has to be stopped for avoiding to complete this requeued request, other use-after-free can be triggered. Fix the race by clearing NBD_CMD_INFLIGHT in nbd_requeue_cmd(), meantime make sure that cmd->lock is grabbed for clearing the flag and the requeue. Cc: Josef Bacik Cc: Yu Kuai Fixes: 2895f1831e91 ("nbd: don't clear 'NBD_CMD_INFLIGHT' flag if request is not completed") Signed-off-by: Ming Lei Reviewed-by: Yu Kuai Link: https://lore.kernel.org/r/20240830034145.1827742-1-ming.lei@redhat.com Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- drivers/block/nbd.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c index 1089dc646b808..96b349148e578 100644 --- a/drivers/block/nbd.c +++ b/drivers/block/nbd.c @@ -181,6 +181,17 @@ static void nbd_requeue_cmd(struct nbd_cmd *cmd) { struct request *req = blk_mq_rq_from_pdu(cmd); + lockdep_assert_held(&cmd->lock); + + /* + * Clear INFLIGHT flag so that this cmd won't be completed in + * normal completion path + * + * INFLIGHT flag will be set when the cmd is queued to nbd next + * time. + */ + __clear_bit(NBD_CMD_INFLIGHT, &cmd->flags); + if (!test_and_set_bit(NBD_CMD_REQUEUED, &cmd->flags)) blk_mq_requeue_request(req, true); } @@ -461,8 +472,8 @@ static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req) nbd_mark_nsock_dead(nbd, nsock, 1); mutex_unlock(&nsock->tx_lock); } - mutex_unlock(&cmd->lock); nbd_requeue_cmd(cmd); + mutex_unlock(&cmd->lock); nbd_config_put(nbd); return BLK_EH_DONE; } -- 2.43.0