Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2610791lqp; Mon, 25 Mar 2024 04:29:52 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUrc7aDE80f+yQBo29gGfO02p0UDIoUm32oCWYjHl2F4zaoLZoZruXWdbB5FTl8yjVtlBlLczXoi2HVIwWpU6z6YdWkaG1ASiiGzkszpg== X-Google-Smtp-Source: AGHT+IFKSyiymVTBlVhXyEwuurVMtq2GGFkw15US3DWECPWWTARlA8UK9N6MPFBuuUVw7JuD7uta X-Received: by 2002:a05:6a00:844:b0:6ea:7dd5:77ba with SMTP id q4-20020a056a00084400b006ea7dd577bamr9331601pfk.10.1711366191728; Mon, 25 Mar 2024 04:29:51 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711366191; cv=pass; d=google.com; s=arc-20160816; b=sFNukqwHIxDaEG+7B+qhct6XAP1HL4gxyH2TDsBtnK8/I8unVImuMWbjG8vLXrspNH sueslwO6rWmIg0Uzhy+Jl6K9RTcRcuSUtuBqMnpJAjBDNICzsGhxz1TQk8LYuPl/Q64z YBeGIPV0w5SZrirUl/j1WxrRqCazZ+7P1ConZyyJcnPdJQHz73AfuFZoKSENSF6E5/jU bktPQib+66VdGnAPbrN3nAlOm5hfwvghBI27qwI3vqbcO7qmyJFR4e5SchDAujTGVA0p jPUQPuAvtYMQ9/X9hTFS7sifLaU1u3mzQq7+4/weYfI6n4lcuBahYGEkcs5tIcpPezqs L44g== 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=KKxkHmvj50cGhtIMtcJ2rR4LKD88CS5G8DavDPvkARU=; fh=J1g3yPMcWy26GVv9/YErvCD1vRTbNWTTxxBuOY0GVGo=; b=TzhoqcPebLug0EQ854pvRfnmYUHv0mPvjeqTmFogQ4gxkO5MuBypVXdK1+Z6RgnraK I+OXADmfkylhkc1qrjRUvS4GcbaGV1A0zsoPVlJBAkzSuYqRWAf6O4LwVkmF8jXgpaet N4JIXX1WrjXfPQgB0FcvJhh42MY/IOAimPLv1ROh68HmtVJQrwRra54/Et9av+t1Hfx1 Sh+We3CSqb73h5D1gNwBf582lAWT58hDVGROX7u3DShfM4b8LuvWYNg8wAkb5SZNb5xW 6vi6MxKkE++AghU8B07pCFDK3i2GQ0HLpihvGOruZ3sMoAolQ5IVZcVW/lKi7tpEfiPF 3Djw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=qGZZKKtc; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-115535-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-115535-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id y1-20020aa793c1000000b006ea8ab87e69si5014689pff.127.2024.03.25.04.29.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 04:29:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-115535-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=qGZZKKtc; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-115535-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-115535-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 41C6A2E2134 for ; Mon, 25 Mar 2024 11:15:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id ED87B245E34; Mon, 25 Mar 2024 02:42:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="qGZZKKtc" 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 A2D7814A600; Sun, 24 Mar 2024 22:57:00 +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=1711321021; cv=none; b=n/7aWlCtsjE3XW5vVrJS2VVsOcA24Ytg43CcvCwRJNYOVZmwy8Fv4hG7FlRYVecEoKPdRmcE0G2NkTSnIth0x+5Gb1cPvuLylcQ1WfP1mOchgRvYLqS5HvYO6m3pCrpm/GmlklLsmchHRx2PajQ9QvvxNzkhI25so1odpYQos1o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711321021; c=relaxed/simple; bh=w9hTT74KPxpjUb9Jz1A1kgmJgCEjM1yGi+Xx/yRSAec=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ne1t2k4cz8AmW/P0wsueM9DmGCcA1KwKjh+wDTjt52nMevRN7DMz/qVLFcRN7RnIRrVrdRdK8ovId4eWq21b8IYAzSqIaSKtNGCdKhD+QJMAVmqnRJ9tWgUCQR+notioiGW8LFqwZkECMlYlIhRfjneXO2dKGA5NDRbVls2AyvA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qGZZKKtc; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id CDE96C433C7; Sun, 24 Mar 2024 22:56:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711321020; bh=w9hTT74KPxpjUb9Jz1A1kgmJgCEjM1yGi+Xx/yRSAec=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qGZZKKtcoe4Cj3gpJ+gUCRIW9L1AK8rpwmpkKsNkKOhlBNu6DXHrHd6iAqHpExIcH uQMZEVquVKhQErm8meHFE3atORbqmzt3zOwhobIVWFuVhI3Qz7kUpD7LzwbWRrl962 I4hgkMoZZNoTxXKwNoBz3uiQStNbaBuC9lVApDPszVBpL7wo3D8qdKKgqaUvd45dY4 5pSnakNNLnBDA87yK9pfbXgxeoWaGfi3rSJiK0/2ErDxTiFd19O/dl6f5/764kpFpZ V+6a/zgY9oo9qGZWD/UD/Id8c6Jn6Zs9/k+txBrG+FdR57y7Vw9dI+MBj7xqZaJu2i fAXj6A2G8ZI5A== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Shifeng Li , Jason Gunthorpe , Jason Gunthorpe , Sasha Levin Subject: [PATCH 6.7 583/713] RDMA/device: Fix a race between mad_client and cm_client init Date: Sun, 24 Mar 2024 18:45:09 -0400 Message-ID: <20240324224720.1345309-584-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240324224720.1345309-1-sashal@kernel.org> References: <20240324224720.1345309-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 67bcea7a153c6..07cb6c5ffda00 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,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