Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2571968lqp; Mon, 25 Mar 2024 03:05:24 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW0B+mTibKfPUN6BvaYhiz8G127P/NrinO/Tsxdl/s2PdvfRvTleNnMmNbACLpert7M5zLL/Ps36qQ7K19jk4WBJ1u1oFgEyGAOiSiPbA== X-Google-Smtp-Source: AGHT+IEYeVZBNhYU25Cpzw7fmCbksRztFyYdu2hwgLO7p8u+vFppUxthz/+ECPoEfm+2jpKaRjr7 X-Received: by 2002:a17:907:7d8d:b0:a47:53b2:2041 with SMTP id oz13-20020a1709077d8d00b00a4753b22041mr2352266ejc.11.1711361124066; Mon, 25 Mar 2024 03:05:24 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711361124; cv=pass; d=google.com; s=arc-20160816; b=Heau9BfcXLTg0KFnIBh6OLIeWq3+hCiGG0nhiIVMvawBpPmZov7NHNWgGqAPYNhUZb mfEbZpeiHiw6Hce2SONqgFNA4MO7wrgs4vP1X4Csf95I4dL/MO8KXwZCuE80Hx9JJ+P+ CPDOJX/yLGdYpo3nPD8YbpXb2cUu3SAVFaaL1gVwEQIltfk4sR2aZy9WxnU07CcW8Tli 82eA0qxReexDXj3MAF8nNkqOA8F4mwZ3mhU312xOUJ8Hoohykv8oxnSQI7qvD+Mky+qh CDoOjFim8xqI0yCmuuJ2U983XxGijY7TYgmBQrDZhcn+5c2FXDSug3RN42nQA1lc36bm 1IWA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=Qz0c5TK7kiYvwxXxHaWeJcUowxieHBdK4rjL17BuqQE=; fh=J1g3yPMcWy26GVv9/YErvCD1vRTbNWTTxxBuOY0GVGo=; b=Mz+o0i0R0tUEtynpSJzcdnF/7mnT1gZUAYPrn8zQdS5f6/5Nb7q0xt/OmerWdXZTNs kNX5g/3VCY4GbsmtM3/OG6LG/+F7iq3vz/WNQynGM8B29APKpNk0VgkYFKufMhtIH5PU +3j85HlQiKICyTCps0r6N5LwpGSMg8t8NuWdFx/6tnXhEwijO8pfLK0PuwlheX6WU8Bz mdKgN6/rGx2G9zAoh7Nj11NNdnAfSdr6snmW1UayFY5S4e2nx7ncOqhXXDUvxA8FMEhj YSZvR2cGKWCejusANbYVBddjontkz54N2tDd1e65rMy+/xRgos9pg3AZ4h5RZwnEVG6H 0lwA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=r2sc5nuc; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-116092-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-116092-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id lv16-20020a170906bc9000b00a465fd6b411si2315844ejb.925.2024.03.25.03.05.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 03:05:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-116092-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=r2sc5nuc; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-116092-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-116092-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id BB6701F311CB for ; Mon, 25 Mar 2024 10:05:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D6D4E18148D; Mon, 25 Mar 2024 03:30:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="r2sc5nuc" 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 527E71788A3; Sun, 24 Mar 2024 23:39:17 +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=1711323558; cv=none; b=GxNaBND5NFzaaOsFKa1aijjIRn9HqOAJuIZ75Zeqorhn9kxXXwcNzSCVXZLho06vx7ZHXrxPpC+RnZjeQIH0KqX3SPhgrO5XsIGAZmY/idp/qQ81R0nAcveEHeGN3FUOUGvIEO84bhONIDEjaDEo5jAsL7nrPxTn8r1MnvYmEEw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711323558; c=relaxed/simple; bh=FW4rGZt3DNRlT3vaTYKQZ+V+cMEHg6iz456kybMECmI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=A4OcrBmM0Zhz3KG6sMqj4Gy83rxuwBrZ3y8CB6DjuyXdW6boaOM1QJW7gVRlG7nGIBSj6ONBXdpB9gtkkJCCHMn8EtI9eGFpqw/20fmbMEpMIHBO3fRwTyxOUYEPVOl3H5edwsAt9cJzBQcwqajTfq773SpJeavTJJy7UmXsAlg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=r2sc5nuc; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7E8DEC43390; Sun, 24 Mar 2024 23:39:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711323557; bh=FW4rGZt3DNRlT3vaTYKQZ+V+cMEHg6iz456kybMECmI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=r2sc5nucWrgMMGGAgKjQx2byiCUP6wZYc1FnO9L+LYRPENU6QNKH414AFn1t/Irdf BlIFpcabZTdVeD5m2kk7oEkyr8YTbrkCVQpR2SDSfOI1wc8tdbgxlWiraJ32bWA+oj mae8MxEP1AwP+POkhwZbUt1SRw1R7xSuP8xlpgO6Zy3phnYCMcs/tO0x2a3LwpafZu z6IPh9Mw9SWs9EwPtQ/KgI9UyCRsNIl7GezzEghIYTh27UhmZ9H7xwqD5M/dE+HZQY uLSvf0l8p3QLCaAkJ5NB72CKvK/I4qfn3glditrR5EmiSgFkziRyB+xG1wsH8+SXoF mKZXsLJW8ITsg== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Shifeng Li , Jason Gunthorpe , Jason Gunthorpe , Sasha Levin Subject: [PATCH 5.15 264/317] RDMA/device: Fix a race between mad_client and cm_client init Date: Sun, 24 Mar 2024 19:34:04 -0400 Message-ID: <20240324233458.1352854-265-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240324233458.1352854-1-sashal@kernel.org> References: <20240324233458.1352854-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit From: Shifeng Li [ Upstream commit 7a8bccd8b29c321ac181369b42b04fecf05f98e2 ] The mad_client will be initialized in enable_device_and_get(), while the devices_rwsem will be downgraded to a read semaphore. There is a window that leads to the failed initialization for cm_client, since it can not get matched mad port from ib_mad_port_list, and the matched mad port will be added to the list after that. mad_client | cm_client ------------------|-------------------------------------------------------- ib_register_device| enable_device_and_get down_write(&devices_rwsem) xa_set_mark(&devices, DEVICE_REGISTERED) downgrade_write(&devices_rwsem) | |ib_cm_init |ib_register_client(&cm_client) |down_read(&devices_rwsem) |xa_for_each_marked (&devices, DEVICE_REGISTERED) |add_client_context |cm_add_one |ib_register_mad_agent |ib_get_mad_port |__ib_get_mad_port |list_for_each_entry(entry, &ib_mad_port_list, port_list) |return NULL |up_read(&devices_rwsem) | add_client_context| ib_mad_init_device| ib_mad_port_open | list_add_tail(&port_priv->port_list, &ib_mad_port_list) up_read(&devices_rwsem) | Fix it by using down_write(&devices_rwsem) in ib_register_client(). Fixes: d0899892edd0 ("RDMA/device: Provide APIs from the core code to help unregistration") Link: https://lore.kernel.org/r/20240203035313.98991-1-lishifeng@sangfor.com.cn Suggested-by: Jason Gunthorpe Signed-off-by: Shifeng Li Signed-off-by: Jason Gunthorpe Signed-off-by: Sasha Levin --- drivers/infiniband/core/device.c | 37 +++++++++++++++++++------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/drivers/infiniband/core/device.c b/drivers/infiniband/core/device.c index 2c2ac63b39c42..725f2719132fb 100644 --- a/drivers/infiniband/core/device.c +++ b/drivers/infiniband/core/device.c @@ -1729,7 +1729,7 @@ static int assign_client_id(struct ib_client *client) { int ret; - down_write(&clients_rwsem); + lockdep_assert_held(&clients_rwsem); /* * The add/remove callbacks must be called in FIFO/LIFO order. To * achieve this we assign client_ids so they are sorted in @@ -1738,14 +1738,11 @@ static int assign_client_id(struct ib_client *client) client->client_id = highest_client_id; ret = xa_insert(&clients, client->client_id, client, GFP_KERNEL); if (ret) - goto out; + return ret; highest_client_id++; xa_set_mark(&clients, client->client_id, CLIENT_REGISTERED); - -out: - up_write(&clients_rwsem); - return ret; + return 0; } static void remove_client_id(struct ib_client *client) @@ -1775,25 +1772,35 @@ int ib_register_client(struct ib_client *client) { struct ib_device *device; unsigned long index; + bool need_unreg = false; int ret; refcount_set(&client->uses, 1); init_completion(&client->uses_zero); + + /* + * The devices_rwsem is held in write mode to ensure that a racing + * ib_register_device() sees a consisent view of clients and devices. + */ + down_write(&devices_rwsem); + down_write(&clients_rwsem); ret = assign_client_id(client); if (ret) - return ret; + goto out; - down_read(&devices_rwsem); + need_unreg = true; xa_for_each_marked (&devices, index, device, DEVICE_REGISTERED) { ret = add_client_context(device, client); - if (ret) { - up_read(&devices_rwsem); - ib_unregister_client(client); - return ret; - } + if (ret) + goto out; } - up_read(&devices_rwsem); - return 0; + ret = 0; +out: + up_write(&clients_rwsem); + up_write(&devices_rwsem); + if (need_unreg && ret) + ib_unregister_client(client); + return ret; } EXPORT_SYMBOL(ib_register_client); -- 2.43.0