From patchwork Sat Feb 3 03:53:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shifeng Li X-Patchwork-Id: 196298 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp1004739dyc; Sat, 3 Feb 2024 05:03:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IG5Fx98mN2ooGBsQRNylaYgqFFBYCQivrjIAiBQMEC1ro07UzHSWyOWtJjFP1Gifi2xDblG X-Received: by 2002:a17:902:db0b:b0:1d9:9038:1e52 with SMTP id m11-20020a170902db0b00b001d990381e52mr1267363plx.45.1706965408481; Sat, 03 Feb 2024 05:03:28 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706965408; cv=pass; d=google.com; s=arc-20160816; b=lvEesL26oZ6zyXfAxwHW0DH7CwIhSvtxj5jbLdBuJ0cn6vSao0T9qYVf2t85TxnTYV SqdeCZBvxeBMRMvY8gCK/ff1QnHe4rQ/pZLueJIzXzwzNB1jCW0azcXCwRQQbLzcopiz TX1z3vxAsnwPjVqzaZq4frSel7pegTEvZ0ID/99SAGEOe4Ozmb+x2VZqCTkr+9WtAP/a 3DDWE+dGtt5AtHCzn/XI+KxY2H4UkikM8UpAtYAjGCyR/gk1dCe9yi5Idkq4uT96D49d l8pGQoWyEJMfdzzH3N1mXPMg80wOWRlyEIF/HGqk7gEjPU2yfa6GMFAEio1+J2GwLp8P A0Xw== 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:message-id:date:subject:cc:to :from; bh=766XTvnk316rRG4Kj/BczDA50qzcENvWxOtt4LpZGj0=; fh=HSY62HPXVSCnZg/9SA22DUIJA+dxu1NbVGHY3Ya86ls=; b=jsZajRyqfCPirwVkEOHs2eosYTgTCi8pT8zmaI6VkQzNY4Od6EfFjRYO+S8UCN+QJh 8eBIa4QWZRHA6UEZDOWYvP9zr1KO4JL8Qv+GBncExkl9QqH5ySwchJnAzL4o/adsUIas NynD9mkDFDTbMZb2O9MGfksHvIe+8PhFjnYSfy2xZr2P6AsOBaRjzASrpyuv/v4e2iIS 7z9YgAMxRihawjvcxdKpXNeq/ioioHuM0PXW3kLb8NqN3i06YDjxiJc0fwz5ZXwsqsuh mRCfTY4nRLFIeO5k5U1s413Ajlo0FEN0rUEgnarNlOxO7E6FSIqhTn5KrWDVjW+KFLwU KWdg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=sangfor.com.cn dmarc=pass fromdomain=sangfor.com.cn); spf=pass (google.com: domain of linux-kernel+bounces-51086-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-51086-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=sangfor.com.cn X-Forwarded-Encrypted: i=1; AJvYcCVNUsH0B0BfP/kEV51ZRmTvEBFF2x60aDuQNa7rE02te1Lxst3yXGeM34YYwp+yVqVZi3r2sQG+1eBSGxxztvRzxov4hA== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id w3-20020a170902e88300b001d98bb43fdasi1083948plg.432.2024.02.03.05.03.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 03 Feb 2024 05:03:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-51086-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=sangfor.com.cn dmarc=pass fromdomain=sangfor.com.cn); spf=pass (google.com: domain of linux-kernel+bounces-51086-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-51086-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=sangfor.com.cn 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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 204DBB2330E for ; Sat, 3 Feb 2024 12:50:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 03D325DF30; Sat, 3 Feb 2024 12:49:10 +0000 (UTC) Received: from mail-m603.netease.com (mail-m603.netease.com [210.79.60.3]) (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 A291A17C77; Sat, 3 Feb 2024 12:49:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=210.79.60.3 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706964548; cv=none; b=D3dBvfPC/Pum2f9qZHWkYKRRFkDkQrr5Oma89vrk7HQMF3xglYJm1Fof/auUFMp/ItR6SBKsN5JUPVTzVP8PlgqrDJo3kcaYBiKll6a1g0eOawMoKejg+VOQMhjPoRvetX7ZWTSS724ZG4LOoq/uJw4/YwieC7sppC0LC1YU788= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706964548; c=relaxed/simple; bh=XRj6ASeP5ukkKJj1qJw3G49h4JawUD81xDI2HwCZ1tA=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=MqCCVhEvuC3QP2EMXHMTld9Ha18eYSx4vJrz/LNSUXPAebZv3tnJV89O0Sb9v1zmjzv/gJdQXsY9Um1pzxRwe6nIMsH7JKln62pelhhWWzesAWdeTNK5v9DQ1x6/gKXyoMSQqkVpKZpwh8zWCQVfAZC3fdlMjpGUuvIWm/g680c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sangfor.com.cn; spf=pass smtp.mailfrom=sangfor.com.cn; arc=none smtp.client-ip=210.79.60.3 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sangfor.com.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sangfor.com.cn Received: from ubuntu.localdomain (unknown [36.36.46.39]) by smtp.qiye.163.com (Hmail) with ESMTPA id 3D7EE46015D; Sat, 3 Feb 2024 11:53:38 +0800 (CST) From: Shifeng Li To: jgg@ziepe.ca, leon@kernel.org, wenglianfa@huawei.com, gustavoars@kernel.org, lishifeng@sangfor.com.cn Cc: linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org, Ding Hui , Shifeng Li Subject: [PATCH v2] RDMA/device: Fix a race between mad_client and cm_client init Date: Fri, 2 Feb 2024 19:53:13 -0800 Message-Id: <20240203035313.98991-1-lishifeng@sangfor.com.cn> X-Mailer: git-send-email 2.25.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-HM-Spam-Status: e1kfGhgUHx5ZQUpXWQgPGg8OCBgUHx5ZQUlOS1dZFg8aDwILHllBWSg2Ly tZV1koWUFITzdXWS1ZQUlXWQ8JGhUIEh9ZQVkZT0NNVkNNHh8ZHR1ISh1KGVUTARMWGhIXJBQOD1 lXWRgSC1lBWUhNVUhNVU9NVUhCWVdZFhoPEhUdFFlBWU9LSFVKTU9JTE5VSktLVUpCS0tZBg++ X-HM-Tid: 0a8d6d19b7dd03aekunm3d7ee46015d X-HM-MType: 1 X-HM-Sender-Digest: e1kMHhlZQR0aFwgeV1kSHx4VD1lBWUc6NQw6Aio6MDMJCzgNOhEWSiNI VjUaCxxVSlVKTEtNQkhJT0pDQ0JNVTMWGhIXVRcSCBMSHR4VHDsIGhUcHRQJVRgUFlUYFUVZV1kS C1lBWUhNVUhNVU9NVUhCWVdZCAFZQU9DSUw3Bg++ X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789882959970711434 X-GMAIL-MSGID: 1789882959970711434 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") Suggested-by: Jason Gunthorpe Cc: Ding Hui Cc: Shifeng Li Signed-off-by: Shifeng Li --- drivers/infiniband/core/device.c | 33 +++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) --- v1->v2: Use down_write(&devices_rwsem) in ib_register_client(). diff --git a/drivers/infiniband/core/device.c b/drivers/infiniband/core/device.c index 67bcea7a153c..ff08a665524a 100644 --- a/drivers/infiniband/core/device.c +++ b/drivers/infiniband/core/device.c @@ -1730,7 +1730,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 @@ -1739,14 +1739,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) @@ -1776,25 +1773,31 @@ 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); + + 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);