From nobody Fri Dec 19 21:48:09 2025 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 05B9E182712; Sun, 24 Mar 2024 23:49:18 +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=1711324158; cv=none; b=Aan7emsv7Dis/AwCDnZESwks6XdkszEBpMBlerb9lyUClPYvJiXceCV87LyjtlOONcEJgt9c3Co9m17LMuCugWMlsj4UfmyAxn6X5b46eGhc3yI3/x/u+u/bSYyNSI8mZzz9BdKOth4osoyKt25kzlnaj3+/Iolp3S3EOgoyZ6c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711324158; c=relaxed/simple; bh=9sr5xbSGjYYNsmUYabCzZmJYK4fu6ed6dInxnPfYfEo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MVWmoceONWKBkp8f8rQ+UHL51adHG2msy3GQI8aZthdRwRfdkSzCCbnSYmLVXCpZs7UhSTeBMGSxoQJvxi8oNZcfMkguArvtQTycOhtMn4ra10P52d2OONl6VxGIx7hMi5AcqzMVc2in9azRCEA2ng5hSEFys3P9begdldo1yOc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=r6QHZU8A; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="r6QHZU8A" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2D691C43399; Sun, 24 Mar 2024 23:49:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711324157; bh=9sr5xbSGjYYNsmUYabCzZmJYK4fu6ed6dInxnPfYfEo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=r6QHZU8AR1hOW7cqQPk7O1nHKYsonV/zH570Zhk0NVNPgg0H29f1LcMMXRlRbsSzw b00uDed3KkHcXvU+Qz+8cOcOg04iQdzH/QC2pj6jHdyX9cYlInqpCzNh0VXjYMgTTa UHWjK4hwNqmNxFV/oQTp+2iCn9PK9cRJ2AxVZuU4nZzAPWnTfBmhfjElSwVNbkvF0o P/NPm1tiZlu3PTpSmTIlDOQVDo6y5szsNqycxcML4L8i2SDQQvEoJm3wbCrEytdB0Y ke+I1xg4NIwM4yWoQ1vTAPrVC4mMCAjOt1yTDfo1SBLylbIcKGWeNDaAr7FxV2YUCw lHrWcMVbSSNSg== 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.4 154/183] RDMA/device: Fix a race between mad_client and cm_client init Date: Sun, 24 Mar 2024 19:46:07 -0400 Message-ID: <20240324234638.1355609-155-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240324234638.1355609-1-sashal@kernel.org> References: <20240324234638.1355609-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: quoted-printable Content-Type: text/plain; charset="utf-8" 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.co= m.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/dev= ice.c index a12ee8ef27a89..c7c3b9bae938c 100644 --- a/drivers/infiniband/core/device.c +++ b/drivers/infiniband/core/device.c @@ -1700,7 +1700,7 @@ static int assign_client_id(struct ib_client *client) { int ret; =20 - 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 @@ -1709,14 +1709,11 @@ static int assign_client_id(struct ib_client *clien= t) client->client_id =3D highest_client_id; ret =3D xa_insert(&clients, client->client_id, client, GFP_KERNEL); if (ret) - goto out; + return ret; =20 highest_client_id++; xa_set_mark(&clients, client->client_id, CLIENT_REGISTERED); - -out: - up_write(&clients_rwsem); - return ret; + return 0; } =20 static void remove_client_id(struct ib_client *client) @@ -1746,25 +1743,35 @@ int ib_register_client(struct ib_client *client) { struct ib_device *device; unsigned long index; + bool need_unreg =3D false; int ret; =20 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 =3D assign_client_id(client); if (ret) - return ret; + goto out; =20 - down_read(&devices_rwsem); + need_unreg =3D true; xa_for_each_marked (&devices, index, device, DEVICE_REGISTERED) { ret =3D 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 =3D 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); =20 --=20 2.43.0