Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp9081220pxu; Mon, 28 Dec 2020 06:11:12 -0800 (PST) X-Google-Smtp-Source: ABdhPJztJdqZkCkSxvuj3JHkBKyT95XDUKwUGBx3edFsH0Kh6JaIYZ1OC9VI1fuFS4ar+NDjQM1k X-Received: by 2002:a05:6402:139a:: with SMTP id b26mr42553288edv.47.1609164672678; Mon, 28 Dec 2020 06:11:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609164672; cv=none; d=google.com; s=arc-20160816; b=U/w53MRbDZmrbo6nZL9IOUbuNcPkKxKvaLFyKHQn/r1hBzs6zHgf7oFgUkUnZ3x1uy PXqB6w5u0IWpPG6rDI/9kq9kwjpE5y0tE/OFC3tI53tx0R2SlWO3h/oEuID02KLFpw01 FN/dRMC29rf8PLaGlOy+NjraKpVMDvM/j6l2HYBGe5pPaqPuDZiZ1ZiQnCKGkooDK0bO qXG8DiP94dlqoVmHgvjSvrPLCZc9R+EohI51w2ypzli2n8b0np/S4Wt48+sO38OjLReX XALNN/hxUh9YBiObXI5TLMOhHkyQJp1w0OU9S44Eqrw6aU7trc7efOg/kOaQqkXfUOD7 i2Xw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=N77HTB0DAziI7Zr5VtkAu2Z421QYpp8vNKKrL/vBZUw=; b=gr2jNJwduOtcREq16IMrESZh6MNTh+EY62wcw6TfpkInQ2xW2jfBEG2WpePqC1IDwi MOzLuvX36EXX33w5vs7NVZEaAeaK+RwL6u5aEgeOxelsg2LFBtE4F4KuVPwa3R08sKg6 V64g3qjcU7WaJKRftW7s1ukaYo0r2U7v6r9pCoasehtILM9uFM7h5HWoPhEf26Zpa4ra F1lru6+plH09k+JE0gHde7hmAOfFKJkV/bHu633Ws7kl7HEQmXrHv13C9oA6MVGP9ptn MjgLQdQ4fcRiheOCIsv9MK9QzW77wS2pM57GZjhu5JoItmOEILOk/qeTCCi7T6m9KaMi d88g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=szhWcHPE; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id gf18si18642923ejb.202.2020.12.28.06.10.50; Mon, 28 Dec 2020 06:11:12 -0800 (PST) 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=@linuxfoundation.org header.s=korg header.b=szhWcHPE; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2438774AbgL1OJK (ORCPT + 99 others); Mon, 28 Dec 2020 09:09:10 -0500 Received: from mail.kernel.org ([198.145.29.99]:43352 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2438776AbgL1OJI (ORCPT ); Mon, 28 Dec 2020 09:09:08 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id B1A88206E5; Mon, 28 Dec 2020 14:08:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1609164533; bh=qBGylRh7ELqSRZd0Q54qLZPjhjZWbDHUUKNBEPJQRJQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=szhWcHPE9CLY1XbTlxz9qI5+JBDggICGM/iXjwA3jtmcfc0m4Y6L3TN47TetRkcRq TP02EIpTCN5nF7+nClG2JL+/DMMesZ5NFx7El+sxiFr+HsXDoiLeP/f6lchEqPac3P hjwgsmAwlJmgcHiSjiwlJmiV/xe6fp8B7aOdK77s= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+1bc48bf7f78253f664a9@syzkaller.appspotmail.com, Leon Romanovsky , Jason Gunthorpe , Sasha Levin Subject: [PATCH 5.10 207/717] RDMA/cma: Fix deadlock on &lock in rdma_cma_listen_on_all() error unwind Date: Mon, 28 Dec 2020 13:43:25 +0100 Message-Id: <20201228125030.896722377@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201228125020.963311703@linuxfoundation.org> References: <20201228125020.963311703@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jason Gunthorpe [ Upstream commit dd37d2f59eb839d51b988f6668ce5f0d533b23fd ] rdma_detroy_id() cannot be called under &lock - we must instead keep the error'd ID around until &lock can be released, then destroy it. This is complicated by the usual way listen IDs are destroyed through cma_process_remove() which can run at any time and will asynchronously destroy the same ID. Remove the ID from visiblity of cma_process_remove() before going down the destroy path outside the locking. Fixes: c80a0c52d85c ("RDMA/cma: Add missing error handling of listen_id") Link: https://lore.kernel.org/r/20201118133756.GK244516@ziepe.ca Reported-by: syzbot+1bc48bf7f78253f664a9@syzkaller.appspotmail.com Reviewed-by: Leon Romanovsky Signed-off-by: Jason Gunthorpe Signed-off-by: Sasha Levin --- drivers/infiniband/core/cma.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/drivers/infiniband/core/cma.c b/drivers/infiniband/core/cma.c index 4585f654f8836..c06c87a4dc5e7 100644 --- a/drivers/infiniband/core/cma.c +++ b/drivers/infiniband/core/cma.c @@ -2496,7 +2496,8 @@ static int cma_listen_handler(struct rdma_cm_id *id, } static int cma_listen_on_dev(struct rdma_id_private *id_priv, - struct cma_device *cma_dev) + struct cma_device *cma_dev, + struct rdma_id_private **to_destroy) { struct rdma_id_private *dev_id_priv; struct net *net = id_priv->id.route.addr.dev_addr.net; @@ -2504,6 +2505,7 @@ static int cma_listen_on_dev(struct rdma_id_private *id_priv, lockdep_assert_held(&lock); + *to_destroy = NULL; if (cma_family(id_priv) == AF_IB && !rdma_cap_ib_cm(cma_dev->device, 1)) return 0; @@ -2518,7 +2520,6 @@ static int cma_listen_on_dev(struct rdma_id_private *id_priv, rdma_addr_size(cma_src_addr(id_priv))); _cma_attach_to_dev(dev_id_priv, cma_dev); - list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list); cma_id_get(id_priv); dev_id_priv->internal_id = 1; dev_id_priv->afonly = id_priv->afonly; @@ -2528,25 +2529,31 @@ static int cma_listen_on_dev(struct rdma_id_private *id_priv, ret = rdma_listen(&dev_id_priv->id, id_priv->backlog); if (ret) goto err_listen; + list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list); return 0; err_listen: - list_del(&id_priv->listen_list); + /* Caller must destroy this after releasing lock */ + *to_destroy = dev_id_priv; dev_warn(&cma_dev->device->dev, "RDMA CMA: %s, error %d\n", __func__, ret); - rdma_destroy_id(&dev_id_priv->id); return ret; } static int cma_listen_on_all(struct rdma_id_private *id_priv) { + struct rdma_id_private *to_destroy; struct cma_device *cma_dev; int ret; mutex_lock(&lock); list_add_tail(&id_priv->list, &listen_any_list); list_for_each_entry(cma_dev, &dev_list, list) { - ret = cma_listen_on_dev(id_priv, cma_dev); - if (ret) + ret = cma_listen_on_dev(id_priv, cma_dev, &to_destroy); + if (ret) { + /* Prevent racing with cma_process_remove() */ + if (to_destroy) + list_del_init(&to_destroy->list); goto err_listen; + } } mutex_unlock(&lock); return 0; @@ -2554,6 +2561,8 @@ static int cma_listen_on_all(struct rdma_id_private *id_priv) err_listen: list_del(&id_priv->list); mutex_unlock(&lock); + if (to_destroy) + rdma_destroy_id(&to_destroy->id); return ret; } @@ -4855,6 +4864,7 @@ static void cma_process_remove(struct cma_device *cma_dev) static int cma_add_one(struct ib_device *device) { + struct rdma_id_private *to_destroy; struct cma_device *cma_dev; struct rdma_id_private *id_priv; unsigned int i; @@ -4902,7 +4912,7 @@ static int cma_add_one(struct ib_device *device) mutex_lock(&lock); list_add_tail(&cma_dev->list, &dev_list); list_for_each_entry(id_priv, &listen_any_list, list) { - ret = cma_listen_on_dev(id_priv, cma_dev); + ret = cma_listen_on_dev(id_priv, cma_dev, &to_destroy); if (ret) goto free_listen; } @@ -4915,6 +4925,7 @@ free_listen: list_del(&cma_dev->list); mutex_unlock(&lock); + /* cma_process_remove() will delete to_destroy */ cma_process_remove(cma_dev); kfree(cma_dev->default_roce_tos); free_gid_type: -- 2.27.0