From nobody Sun Jun 14 08:16:31 2026 Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A403531A56D for ; Wed, 1 Apr 2026 23:53:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775087582; cv=none; b=o/Ke3DoGGrQBtLKm9rWa+2DPSLt8CwUPpsYz/k76eaRiDs8tFxk0cN+rc0WEPTnrp/hmraXGVIR4zJu5XXyNX5aiHTtg7ScbF6qT2kZqfHGrrp20jK+MHw242iEsInMaE0AgQJSEsKvoawY+s5+u90xEEms95s46XeBIiROATgI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775087582; c=relaxed/simple; bh=FpfE5G/aiPXrSwvKAYsxHijMr5QmhOe0heWhhfXLC24=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=u+gBoo29arVeXqBWjifcTaW4/hdXSUd0cAWs3yeXllhMVONc8bGqCzthyMpwR33JZmHdA83FsSHaihAH0ixs/wzeQZZfLxmU76irRwfvvgc14It5ROMz7LAOYavnwkVkRAIK4p1hJcoqCYq1nE/J1XOgvOiIyWyNqBDEt1z+qhI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=RP/PB5i6; arc=none smtp.client-ip=209.85.128.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="RP/PB5i6" Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-4852a9c6309so1924255e9.0 for ; Wed, 01 Apr 2026 16:53:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775087579; x=1775692379; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=cJ3m3gvWAxk1Tlyn4HV1OGvgOsTxc97XPaeHMTCg50Q=; b=RP/PB5i6x7f68TUaUIuk47g9XsOTVAHtYZj4ZKqonTQXvXGYrreQbleyFBJzAliUWe gL/gHpd2DplpxmzWkIpV2K7x82AoAwow5U7vcEmKNYBT5jVVG5SEuaKotLsTdLbUjEbT BcS9l2+fNH1ZbXWRkqsDdfQlebzDC91EVd+2Ym44wWwThxUJ37lep/oAi9fqVjhr0MpT PUSGlszjv2pZBi1iwfmX4xgnpR18FQJUb2mHVcKwwaDiTL2KPDEwKtBOkBoY6bRzwaNR 0BY8RYBRscd66YIpdXDE2CMewGBvM8rtzYGviE8Wc7i9ygK896axPK/fey9Oqd9/oYtW liyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775087579; x=1775692379; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=cJ3m3gvWAxk1Tlyn4HV1OGvgOsTxc97XPaeHMTCg50Q=; b=UZ7ics6aHgR6FVJzPYSRZ4U88as+xUStdbEXSUkFw6uS86Hq6ieRQPDTt3voUtCwzm yUMorwpgqhCO057kB8DOdcGQGnkRpWrt+gfug23ruHKMa4kcLPHjstSG4g/tc0K2dZ/e lp64Q4r6M/eAh9I3vTykwEery5hHiDPhPdVV4ASNAVLtHWrabn5r/4esOFuhV2X7JdoR 0Sn3kXs8yjtg9qlFg2cVmRplotiq9VeUJDuHU043mzv1mly5TiZZ/brKJGeHAaann39Z TH5oStsO2yw0JZk3sN0ZhAPDUQijZKVAPO93JTGxcPoLFjSpmBdhCuALBv2F7w+RsOeq FZXQ== X-Forwarded-Encrypted: i=1; AJvYcCUr6F0FxMTevWJ9y+yWGQWOQllO3BYoGJS6BnXoAWzsrksIt9j8Zo7gQaj6aOKM2Td0gzyD8XA4wNENxAs=@vger.kernel.org X-Gm-Message-State: AOJu0Yybf6oZU1iqtLBmPsoyplqErkklp5gUo3unMTx5EjPKXvnTxqn6 ADWSU4SQkUr1hXRkaw7ubaKXfAmvCE9/+z3nx7/fizqjXAcklhdA+ORY X-Gm-Gg: ATEYQzwd4ypjT7jCBPaXLu9SW41p5BCR26Fb4cxRNCoz/+4YbxT0s5WWUxb/HsPle2f AYlPSs3VcA3Bs3I/aB8ECcer7BskKWd8+ILaHGaLIMzhPuHCgww0ucXRJ5FBoMucospcVbq9Go4 hM6R8JZN6keabpaB6H48ZBzvddrlY/r1Hy77mksjUnqEZLdx1Fsmd9vtzTYHZKimxcGQh4+z7gB 2d81Cu74OrRXpZBiB56ilqbNUQUiHymAzlN2CcqHXOJdiBXbDWgxnx9J9SQPPbAkB3maE9Kx5c1 3Mkqe1ikTLTLe8INjeD/uJYqoA2hugZK0GnJC74oMgVduKM1vci1vBOyw4sh34YYbAXMbuv+/Vg NxvKNr0Lm6SckgKxqmSaJSsmmmjHq0OS70mbDjb8f7102cDQhFAXxgBaeu4Nx6J1bUTgHea5RsQ AGXH3F4w/l7qQ6ZmLiE7FfFtLyqQzIsaQKYd+qRL04WXZrPAW2pVptOPJnZVVw94dolV2nolEb7 wUBxxrtMfrOiJi2ZjPuHENukhEkyNp9VO3y4jyqrv6E2+D2 X-Received: by 2002:a05:600c:45c5:b0:486:ffa3:584 with SMTP id 5b1f17b1804b1-48883591f7amr89950115e9.15.1775087578767; Wed, 01 Apr 2026 16:52:58 -0700 (PDT) Received: from DESKTOP-NQ2T5I7.localdomain (heme-13-b2-v4wan-167795-cust403.vm32.cable.virginm.net. [81.108.45.148]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4888a705f99sm27380715e9.11.2026.04.01.16.52.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Apr 2026 16:52:57 -0700 (PDT) From: Prathamesh Deshpande To: prathameshdeshpande7@gmail.com Cc: dledford@redhat.com, haggaie@mellanox.com, jgg@ziepe.ca, leon@kernel.org, linux-kernel@vger.kernel.org, linux-rdma@vger.kernel.org Subject: [PATCH v4] IB/mlx5: Fix state corruption and resource leaks in loopback enablement Date: Thu, 2 Apr 2026 00:52:32 +0100 Message-ID: <20260401235232.21155-1-prathameshdeshpande7@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260401223550.20040-1-prathameshdeshpande7@gmail.com> References: <20260401223550.20040-1-prathameshdeshpande7@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In mlx5_ib_alloc_transport_domain(), an early success path was returning 'err' (which is 0) instead of a literal 0. Additionally, as identified by Sashiko, if mlx5_ib_enable_lb() fails to update the hardware, it leaves the software state in an inconsistent state where reference counters are incremented but the hardware remains disabled. Fixing this in the caller created a race window where the mutex was released between enablement and rollback. Update mlx5_ib_enable_lb() to perform an atomic rollback of reference counters and only set the 'enabled' flag if the hardware command succeeds. Also, add error handling in mlx5_ib_alloc_transport_domain() to deallocate the transport domain (tdn) if loopback enablement fails. Signed-off-by: Prathamesh Deshpande --- v4: - Moved rollback logic into mlx5_ib_enable_lb() to ensure atomicity within the mutex and prevent race conditions [Sashiko]. v3: - Also call mlx5_ib_disable_lb() on failure to roll back software state/cou= nters [Sashiko]. v2: - Added deallocation of tdn if mlx5_ib_enable_lb() fails [Sashiko]. - Reworded commit message to reflect the functional fix and credit the tool. drivers/infiniband/hw/mlx5/main.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/drivers/infiniband/hw/mlx5/main.c b/drivers/infiniband/hw/mlx5= /main.c index 635002e684a5..877b02e98033 100644 --- a/drivers/infiniband/hw/mlx5/main.c +++ b/drivers/infiniband/hw/mlx5/main.c @@ -2022,7 +2022,14 @@ int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool = td, bool qp) dev->lb.qps =3D=3D 1) { if (!dev->lb.enabled) { err =3D mlx5_nic_vport_update_local_lb(dev->mdev, true); - dev->lb.enabled =3D true; + if (err) { + if (td) + dev->lb.user_td--; + if (qp) + dev->lb.qps--; + } else { + dev->lb.enabled =3D true; + } } } =20 @@ -2068,9 +2075,13 @@ static int mlx5_ib_alloc_transport_domain(struct mlx= 5_ib_dev *dev, u32 *tdn, if ((MLX5_CAP_GEN(dev->mdev, port_type) !=3D MLX5_CAP_PORT_TYPE_ETH) || (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) && !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc))) - return err; + return 0; =20 - return mlx5_ib_enable_lb(dev, true, false); + err =3D mlx5_ib_enable_lb(dev, true, false); + if (err) + mlx5_cmd_dealloc_transport_domain(dev->mdev, *tdn, uid); + + return err; } =20 static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 = tdn, --=20 2.43.0