From patchwork Fri Aug 30 03:41:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Lei X-Patchwork-Id: 13784174 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 7FCAB67A0D for ; Fri, 30 Aug 2024 03:42:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724989322; cv=none; b=a3j+PH2IzUxKWHguRUmOKdRhKpQOYfDWs97hRSVlpzhEKAI6AsZLZIbaB+VMHpEJ/B7adl4aB66XlZr5ZEll4ejdAiWk5djxaZfg3bwdIRpWKf0W6U7UPAvzQ+Zaez8+crPDxTl9IjrDAFugtTJO/HEB+D7pMzYwHZ9v5ufOi+8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724989322; c=relaxed/simple; bh=ZS/o40msMyU6szcEeg9PSijvr5UUPaNnCn3yvAtF2zo=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=NI1ylYNR0vDbGEsBWI+YRVWDlKb7IC6Zd6j7cwBPv1x5AqFk9cPWMYyXAh8svbkg3GuKvtQYehOwBn2jaWVZ/gadxLviMfW3IiNyRliBBgvcnvMMBGqF2qZsJy+KewgJ+xeie+bcGfWgOchkTm4U0oBd4OlVQbrxLRYkH0u3yCg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=StJfVR8l; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="StJfVR8l" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724989319; 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; bh=P1oRnRjGxcSCUwyrPTkzt9670Q5+gBTypgzA4GbTwPw=; b=StJfVR8lOY0fOJ9Ts/dvb48PLBznVRYbQpA5nrXBGcU8QIGH1jGBQw7I0BMufGWzJ7powL zZUpD3v7bR2olb7rR1G5DdEZbk3rzUgxwDmEourxKrnbBdLkCT7M5Gt8amSG4bVLMwiwy5 iWgEiBNOltjKR+/NxfFaYyvhagwH9/w= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-499-sO_NXIhGMjOcq8gixJ4wLA-1; Thu, 29 Aug 2024 23:41:54 -0400 X-MC-Unique: sO_NXIhGMjOcq8gixJ4wLA-1 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 9A63F1955D4D; Fri, 30 Aug 2024 03:41:52 +0000 (UTC) Received: from localhost (unknown [10.72.112.71]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 88B43195605A; Fri, 30 Aug 2024 03:41:49 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Ming Lei , Josef Bacik , Yu Kuai Subject: [PATCH] nbd: fix race between timeout and normal completion Date: Fri, 30 Aug 2024 11:41:45 +0800 Message-ID: <20240830034145.1827742-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 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 --- 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 41a90150b501..69b9851b6798 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); } @@ -488,8 +499,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; }