Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp875794pxk; Thu, 17 Sep 2020 20:01:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx92iIELOs3V2VeExQyPdDNMdT4GnxfNQcrxxH2j0mTrBoeToVuOTfSPVhDgzS//HxnK86O X-Received: by 2002:a50:ce06:: with SMTP id y6mr36929906edi.273.1600398111647; Thu, 17 Sep 2020 20:01:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600398111; cv=none; d=google.com; s=arc-20160816; b=M0QckdajzXZAVOL+i4a0rN2MAOumjDqHBGvT7xzNSUsUGTfMIhxEQnPSBqqSisZoUd ONS+V+b6Wc40WH1WXJaM7vNsY1AqAA2QeqeCpMQe01Jxt7la1uo3ON9dfyQ6CY2xuqUg hLIJQZzSJ5GxTYgJscYEjXeoOxbHb6kicrYeZfDkdb5xp6rJh8ojom8D0fxUeJcdyjpA eDcJ/w6PH0k27LATI+Mlv4N0pDVWURytQx8jxklF88yVDZ4nWMEuNXSS8STp1P6tCoA2 z68QEVLOWOOQ0OK4pwusiya58EVHOhrKgp2XAYQRKYfWfv+u+r/UuI/GnyZuMumSrpAy SphQ== 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=rg4fLByZa4f1l2Jw1+7Qb3e+meyz1Iziyz6du5PvoQM=; b=AmEFCE+08/7QZS37kaMS+BvB+KEXKvVDPM5AHE+9WRNNjdA7xcIlPOykXgg7RPmv08 /YuCh5nNBOlr8mvbICASCGg4yMD2yVYQZrCmIva4510YSHdkNdqY3LIDQDAOTSlN4ACt skc2OOoG9HQuLirm/oYq7aIuX9y6Xg323gOI6QpkiSSp1Ls5gXL1kiWkAASFxD0FXh4X 01nXq6E2nqxKUEFmJQXGEcBWDxHoXX4q5D3+Qn73WTsDcNv0aEVI1ovNi7uLv1Ud2Lax ax/D15RcbfwXbjWcT+hnEuIUu+5Ybbaaq2XFXN68eCDyFmiJtNugdhrb3f8AURn1uOar RpkA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=2FNax142; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z23si1079886edl.282.2020.09.17.20.01.28; Thu, 17 Sep 2020 20:01:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=2FNax142; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727664AbgIRCF5 (ORCPT + 99 others); Thu, 17 Sep 2020 22:05:57 -0400 Received: from mail.kernel.org ([198.145.29.99]:54610 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726873AbgIRCFx (ORCPT ); Thu, 17 Sep 2020 22:05:53 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 7D735206BE; Fri, 18 Sep 2020 02:05:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600394752; bh=X1fbTCMHDUEOMtauXEHeNTfiD+8wCfxilNJnslZzrcw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2FNax1422MGaK6ZBWN/45HO+9xxeHddPWya/VATXwvlDDVh/hb29+Q7zu78LkV6dI yvfiTGHRgCltYGZ9H5csFyL+hoyPvZQJPe0pFCnPGpfOY40Lz2CyGmuJplZRhwzNcn +CiXxMc1olVm4WltfRyRRN+t9vWXb1SNMjcgs45A= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Israel Rukshin , Sagi Grimberg , Max Gurtovoy , Christoph Hellwig , Sasha Levin Subject: [PATCH AUTOSEL 5.4 230/330] nvmet-rdma: fix double free of rdma queue Date: Thu, 17 Sep 2020 21:59:30 -0400 Message-Id: <20200918020110.2063155-230-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200918020110.2063155-1-sashal@kernel.org> References: <20200918020110.2063155-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Israel Rukshin [ Upstream commit 21f9024355e58772ec5d7fc3534aa5e29d72a8b6 ] In case rdma accept fails at nvmet_rdma_queue_connect(), release work is scheduled. Later on, a new RDMA CM event may arrive since we didn't destroy the cm-id and call nvmet_rdma_queue_connect_fail(), which schedule another release work. This will cause calling nvmet_rdma_free_queue twice. To fix this we implicitly destroy the cm_id with non-zero ret code, which guarantees that new rdma_cm events will not arrive afterwards. Also add a qp pointer to nvmet_rdma_queue structure, so we can use it when the cm_id pointer is NULL or was destroyed. Signed-off-by: Israel Rukshin Suggested-by: Sagi Grimberg Reviewed-by: Max Gurtovoy Reviewed-by: Sagi Grimberg Signed-off-by: Christoph Hellwig Signed-off-by: Sasha Levin --- drivers/nvme/target/rdma.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/drivers/nvme/target/rdma.c b/drivers/nvme/target/rdma.c index 36d906a7f70d3..b5314164479e9 100644 --- a/drivers/nvme/target/rdma.c +++ b/drivers/nvme/target/rdma.c @@ -75,6 +75,7 @@ enum nvmet_rdma_queue_state { struct nvmet_rdma_queue { struct rdma_cm_id *cm_id; + struct ib_qp *qp; struct nvmet_port *port; struct ib_cq *cq; atomic_t sq_wr_avail; @@ -464,7 +465,7 @@ static int nvmet_rdma_post_recv(struct nvmet_rdma_device *ndev, if (ndev->srq) ret = ib_post_srq_recv(ndev->srq, &cmd->wr, NULL); else - ret = ib_post_recv(cmd->queue->cm_id->qp, &cmd->wr, NULL); + ret = ib_post_recv(cmd->queue->qp, &cmd->wr, NULL); if (unlikely(ret)) pr_err("post_recv cmd failed\n"); @@ -503,7 +504,7 @@ static void nvmet_rdma_release_rsp(struct nvmet_rdma_rsp *rsp) atomic_add(1 + rsp->n_rdma, &queue->sq_wr_avail); if (rsp->n_rdma) { - rdma_rw_ctx_destroy(&rsp->rw, queue->cm_id->qp, + rdma_rw_ctx_destroy(&rsp->rw, queue->qp, queue->cm_id->port_num, rsp->req.sg, rsp->req.sg_cnt, nvmet_data_dir(&rsp->req)); } @@ -587,7 +588,7 @@ static void nvmet_rdma_read_data_done(struct ib_cq *cq, struct ib_wc *wc) WARN_ON(rsp->n_rdma <= 0); atomic_add(rsp->n_rdma, &queue->sq_wr_avail); - rdma_rw_ctx_destroy(&rsp->rw, queue->cm_id->qp, + rdma_rw_ctx_destroy(&rsp->rw, queue->qp, queue->cm_id->port_num, rsp->req.sg, rsp->req.sg_cnt, nvmet_data_dir(&rsp->req)); rsp->n_rdma = 0; @@ -742,7 +743,7 @@ static bool nvmet_rdma_execute_command(struct nvmet_rdma_rsp *rsp) } if (nvmet_rdma_need_data_in(rsp)) { - if (rdma_rw_ctx_post(&rsp->rw, queue->cm_id->qp, + if (rdma_rw_ctx_post(&rsp->rw, queue->qp, queue->cm_id->port_num, &rsp->read_cqe, NULL)) nvmet_req_complete(&rsp->req, NVME_SC_DATA_XFER_ERROR); } else { @@ -1025,6 +1026,7 @@ static int nvmet_rdma_create_queue_ib(struct nvmet_rdma_queue *queue) pr_err("failed to create_qp ret= %d\n", ret); goto err_destroy_cq; } + queue->qp = queue->cm_id->qp; atomic_set(&queue->sq_wr_avail, qp_attr.cap.max_send_wr); @@ -1053,11 +1055,10 @@ err_destroy_cq: static void nvmet_rdma_destroy_queue_ib(struct nvmet_rdma_queue *queue) { - struct ib_qp *qp = queue->cm_id->qp; - - ib_drain_qp(qp); - rdma_destroy_id(queue->cm_id); - ib_destroy_qp(qp); + ib_drain_qp(queue->qp); + if (queue->cm_id) + rdma_destroy_id(queue->cm_id); + ib_destroy_qp(queue->qp); ib_free_cq(queue->cq); } @@ -1291,9 +1292,12 @@ static int nvmet_rdma_queue_connect(struct rdma_cm_id *cm_id, ret = nvmet_rdma_cm_accept(cm_id, queue, &event->param.conn); if (ret) { - schedule_work(&queue->release_work); - /* Destroying rdma_cm id is not needed here */ - return 0; + /* + * Don't destroy the cm_id in free path, as we implicitly + * destroy the cm_id here with non-zero ret code. + */ + queue->cm_id = NULL; + goto free_queue; } mutex_lock(&nvmet_rdma_queue_mutex); @@ -1302,6 +1306,8 @@ static int nvmet_rdma_queue_connect(struct rdma_cm_id *cm_id, return 0; +free_queue: + nvmet_rdma_free_queue(queue); put_device: kref_put(&ndev->ref, nvmet_rdma_free_dev); -- 2.25.1