๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #26566

Re: [PATCH net] net/mlx5: Enter error state on surprise removal

#26566
From: netdev-bot+sashiko
Date: Thu, 03 Dec 2026 19:44
219 lines
15754 bytes
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.

Critical: 0 ยท High: 1 ยท Medium: 2 ยท Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] remove_one() now calls mlx5_health_enter_error_state_drained().
- [Medium] The patch only fixes the error-state entry on the PCI
  remove_one() path.
- [Medium] mlx5_health_enter_error_state_drained() (health.c) reads the
  fatal sensors only once, right after mlx5_drain_health_wq().

--

net/mlx5: Enter error state on surprise removal

On PCI surprise removal, remove_one() now checks the fatal sensors after
it drains the health work. If the device is dead and the interface is
up, it puts the device in error state, so already-posted work such as
IPoIB multicast leave MADs can complete. A new helper does this:
mlx5_health_enter_error_state_drained(). It skips the "previous error is
still being handled" check of mlx5_enter_error_state().

> diff --git a/drivers/net/ethernet/mellanox/mlx5/core/health.c b/drivers/net/ethernet/mellanox/mlx5/core/health.c
> index aeeb136f5ebc7..622c9e0f065d9 100644
> --- a/drivers/net/ethernet/mellanox/mlx5/core/health.c
> +++ b/drivers/net/ethernet/mellanox/mlx5/core/health.c
> @@ -224,6 +224,18 @@ void mlx5_enter_error_state(struct mlx5_core_dev *dev, bool force)
>  	mutex_unlock(&dev->intf_state_mutex);
>  }
>  
> +/* Caller must drain the fw_reset and health work before calling this */
> +void mlx5_health_enter_error_state_drained(struct mlx5_core_dev *dev)
> +{
> +	if (!mlx5_health_check_fatal_sensors(dev))
> +		return;

[Severity: Medium]

This sensor check runs only once, right after mlx5_drain_health_wq(). What
happens if the device or firmware goes fatal after this check but before
the IB device is unregistered?

mlx5_drain_health_wq() sets MLX5_DROP_HEALTH_WORK and cancels the works,
but the health poll timer stays armed. Only mlx5_stop_health_poll() in
mlx5_function_disable() stops it, and that runs at the end of
mlx5_uninit_one(). By then mlx5_sriov_disable() and
mlx5_unregister_device() have already removed the IB device.

If the failure happens in that window:

poll_health()
    dev->state = MLX5_DEVICE_STATE_INTERNAL_ERROR;
    mlx5_trigger_health_work(dev);

mlx5_fw_fatal_reporter_err_work()
    if (test_bit(MLX5_DROP_HEALTH_WORK, &health->flags)) {
        mlx5_core_err(dev, "health works are not permitted at this stage\n");
        mutex_unlock(&dev->intf_state_mutex);
        return;
    }

In that case mlx5_cmd_flush() never runs, and mlx5_ib never receives
MLX5_DEV_EVENT_SYS_ERROR.

The commit message says "once the health work is drained, nobody else
will handle an error that poll_health() already marked". Wouldn't the
same reasoning also apply to errors that poll_health() marks after this
check?

pciehp surprise removal is covered here, because
pciehp_unconfigure_device() calls pci_dev_set_disconnected() before the
device is removed. Two windows remain:

  - a device or FW failure during an orderly unbind
  - a surprise removal that races an orderly remove already in progress

> +
> +	mutex_lock(&dev->intf_state_mutex);
> +	if (test_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state))
> +		enter_error_state(dev, false);

[Severity: High]

Could this lead to a use-after-free of struct mlx5_ib_dev?

enter_error_state() does:

    mlx5_notifier_call_chain(dev->priv.events, MLX5_DEV_EVENT_SYS_ERROR, (void *)1);

The mlx5_ib listener does not handle the event inline.
mlx5_ib_sys_error_event() in drivers/infiniband/hw/mlx5/main.c defers it
to a work item:

    work->dev = container_of(nb, struct mlx5_ib_dev, sys_error_events);
    ...
    queue_work(mlx5_ib_event_wq, &work->work);

It takes no reference on the ibdev.

Right after this, on the same thread, remove_one() continues into:

remove_one()
  mlx5_uninit_one()
    mlx5_unregister_device()
      del_adev()
        mlx5r_remove()
          __mlx5_ib_remove()
            mlx5_ib_stage_sys_error_notifier_cleanup()
              mlx5_notifier_unregister()
            kfree(dev->port);
            ib_dealloc_device(&dev->ib_dev);

Nothing in this sequence flushes or cancels mlx5_ib_event_wq. It looks
like the queue is only destroyed at module exit.

Suppose mlx5_ib_handle_sys_error_event() has not run yet when
ib_dealloc_device() frees the device. It would then run on freed memory:

    struct mlx5_ib_dev *ibdev = work->dev;
    ...
    mlx5_ib_handle_internal_error(ibdev);
    ...
    ibdev->ib_active = false;

That includes walking the QP/CQ lists. The window is a backlogged ordered
workqueue, or a delay in creating workers under memory pressure.

The missing flush in mlx5_ib is older. It can also be reached from
mlx5_pci_err_detected() and from the fatal work unload path. This patch,
though, sends SYS_ERROR right before synchronous IB teardown on every
surprise removal, and the fix depends on that async handoff.

Should the mlx5_ib teardown flush mlx5_ib_event_wq, or cancel the pending
sys_error work, before it frees the device?

> +	mutex_unlock(&dev->intf_state_mutex);
> +}

[ ... ]

> diff --git a/drivers/net/ethernet/mellanox/mlx5/core/main.c b/drivers/net/ethernet/mellanox/mlx5/core/main.c
> index 0439fcfb9ce57..567df9d8eb524 100644
> --- a/drivers/net/ethernet/mellanox/mlx5/core/main.c
> +++ b/drivers/net/ethernet/mellanox/mlx5/core/main.c
> @@ -2014,6 +2014,7 @@ static void remove_one(struct pci_dev *pdev)
>  	set_bit(MLX5_BREAK_FW_WAIT, &dev->intf_state);
>  	mlx5_drain_fw_reset(dev);
>  	mlx5_drain_health_wq(dev);
> +	mlx5_health_enter_error_state_drained(dev);

[Severity: Medium]

This fixes remove_one(). Are there other drain-then-teardown paths that
leave the device in the same drained but never-errored state?

mlx5_sf_dev_remove() in sf/dev/driver.c does:

    set_bit(MLX5_BREAK_FW_WAIT, &mdev->intf_state);
    mlx5_drain_health_wq(mdev);
    if (mlx5_dev_is_lightweight(mdev))
        mlx5_uninit_one_light(mdev);
    else
        mlx5_uninit_one(mdev);

mlx5_sf_dev_shutdown() also drains the health work and then calls
mlx5_unload_one().

SF mdevs share the parent PF BAR, and mdev->pdev is the parent pdev, so
they are dead too after a PF surprise removal. They are removed from
inside the PF teardown:

remove_one()
  mlx5_uninit_one()
    mlx5_unload()
      mlx5_sf_dev_table_destroy()
        ...
          mlx5_sf_dev_remove()

The SYS_ERROR raised for the PF goes only to the PF's own notifier chain.
So the SF's mlx5_ib device never receives it, and mlx5_cmd_flush() never
runs for the SF.

The PCI shutdown() callback in main.c follows a similar pattern:

    mlx5_drain_fw_reset(dev);
    mlx5_drain_health_wq(dev);
    err = mlx5_try_fast_unload(dev);
    if (err)
        mlx5_unload_one(dev, false);

If poll_health() has already set MLX5_DEVICE_STATE_INTERNAL_ERROR,
mlx5_try_fast_unload() returns early:

    if (dev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
        mlx5_core_dbg(dev, "Device in internal error state, giving up\n");
        return -EAGAIN;
    }

That return comes before its own mlx5_enter_error_state(dev, true).
mlx5_unload_one() then removes the IB aux device with no cmd flush and no
SYS_ERROR first.

Should these paths call mlx5_health_enter_error_state_drained() too?

>  	mlx5_sriov_disable(pdev, false);
>  	mlx5_uninit_one(dev);
>  	mlx5_shd_uninit(dev);

[ ... ]

-- 
Sashiko AI review ยท https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202194317.3593082-1-tariqt%40nvidia.com
Technical Details
Message-ID: <179632709835.3146.7657185898438703939@kernel.org>
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!newsfeed.endofthelinebbs.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-376841-linux-kernel-netdev=dewy.ru
References: <20261202194317.3593082-1-tariqt@nvidia.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!newsfeed.endofthelinebbs.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-376841-linux-kernel-netdev=dewy.ru
From: netdev-bot+sashiko@kernel.org
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH net] net/mlx5: Enter error state on surprise removal
Date: Thu, 03 Dec 2026 19:44:58 +0000
Organization: Usenet Network
Message-ID: <179632709835.3146.7657185898438703939@kernel.org>
References: <20261202194317.3593082-1-tariqt@nvidia.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="2579914"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:1BPIgiBh5cZFidYVjdyIN0gHZss= sha256:ERXedq1Nq97kOFPOubfRSCLHKTMQd+rm/j+Nwl5Rebk=
	sha1:Xt645wFfNfwHLSIBqMJ9wTbytCk= sha256:X8bD+u9Ed9juCUbZBdRtAI8UrUVb0GXYZewws4Cs13I=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 3CFDE117A24; Thu,  3 Dec 2026 22:45:22 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.64
X-Spam-Status: No, score=-3.64 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.24, DKIM_SIGNED=0.1,
	DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=kernel.org
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id sEK1J0Yhlj2L;
	Thu,  3 Dec 2026 22:45:17 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-376841-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id 2BB7D1179D5; Thu,  3 Dec 2026 22:45:07 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id 0699C24E69; Thu,  3 Dec 2026 19:45:06 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1796327105;
	b=wEkB/F+BiTZoKB1ykMKimAZgREGz3UBS1jhMhegrCG6TMgUOMcTXOPW8QrvQ0TCxYMmP
	 dNLr3AHD/lfVYMg4ycwqnNbIfxg3BwU8pxIVI2NsJw0dHc1LbpcSKu4A1TQV5dsD8qX7r
	 kEKXOwbNSzorsVMx7qEcEkFyRcittVghh+zK2GB/sYZJQ2X3pOzLsbv3VR3tLYy7C3sWN
	 2XerqQZp+G1zlD+CpXhnsdq+W8GnNt3hWeL8fnurKzTw6/PrdwzSiCH1eBSIOvdEl0A4r
	 SCQj3khoXVkYa/xO3ZRBYsbYvl0DfAFHdWLQ28QWUePhVfEFMkCl/UTd/6LBxxL7dSw==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796327105;
	h=X-Original-To:Received:Received:DKIM-Signature:Subject:From:To:Cc:
	 Date:Message-ID:In-Reply-To:References:X-sashiko-severity:
	 Content-Type:Content-Transfer-Encoding:X-Mailing-List:List-Id:
	 List-Post:List-Help:List-Subscribe:List-Unsubscribe:List-Owner:
	 Precedence:MIME-Version;
	bh=UbaCsEmUV6YOMK9Lzi7cK/9Cas/bsHkjMZLYXYwaHBw=;
	b=fEgk61E4v6Qx0NjpaQztRSFFBi11q1hL79lHPkdu2dm1HNWdn4xdJlciUPWX0ZsRAP53
	 UaEY+TajSwX2N74C6BTjO1HzcuGG1wgNpoMYWPd/xxEbNafFjvuPlgR6ulJzwvr7d1dD4
	 ac+aOXXF/Sfu/x2deWPtZhx3Q0mgAfq5WBkN564n3qf+yJBLF7yLUV0J+Ge/nUk9m798k
	 +SFY0amohcwaNSMxAInq5uR+n4V4xnCYRqK9iknxHOoXkDl/QqbMZgbhtkERiX85SctRZ
	 oCxxvwem9OALeOuK0Y5+LofL4HiJjAhPzgIXuDjwZkAUOWyOD6swTF6DnvUFkwsr1wA==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=BbSuioGP;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id D2EA5606A334;
	Thu,  3 Dec 2026 19:45:05 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=BbSuioGP
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1796327100;
	b=E7oQh1/vYau0ooSfGQZq8HLufK+GBVjFHchvlE6mtb+uIx3daF2+oQt0EFyS+TL3xvvd
	 +CqUk3achkrJWn08KUQH02hRMO1gEiE7+lehAUU8SEndbA/mhLddZ2fqxK84uy5JYYHQt
	 47PPqDoVg5vmJbEHNe22U+Vn5bzm+BAZUHqfV8tPAXH1833bvpPgJs6rpVTtsz2I3z0RC
	 o+TFHH1INBsC7l66g9i4XLkTGMrBwGV3C1vj5CU4YqoxtRPjr0k34oBm6wJ6AtiIwRP29
	 8yUvicsrzVChzpIrK0tC8mZLm8KHo5hpwNNIHlWcWL6U4/Co+bDOmfgRjthZKcP4q7g==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796327100;
	h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID:
	 In-Reply-To:References:X-sashiko-severity:Content-Type:
	 Content-Transfer-Encoding:MIME-Version;
	bh=UbaCsEmUV6YOMK9Lzi7cK/9Cas/bsHkjMZLYXYwaHBw=;
	b=Z+1VBWR6gVrJTiaW/tM9fNcxAi+nQJLh4QYgs0Gpa1pzFLhBDKiul/XTweE6qcYXym1g
	 eflUKcUP7iheZ85S3ofiQfb29OmvxKoSA59bo5qC5Ye1DXxlHCuSa8XymoUS+Xi0Pglf1
	 S8CeiZ9h/FN0ofXtmo12ZAeZEaoORnW1kN0RcPh7JMeU2yk/lUCVcZxtj1r31R9mJeV0J
	 iD6HXtBYCGIjrmNsvDg0uk+G3T9u/+dcTKIiRL/VaSpC0NOKnqxbxQjz/Ot6XbPA27+/L
	 tRCFdTEk6sZfgV4PJKPACjzwBUInnIsf2nJu79JwwNamHc0V+AssczP8pbRu24Tqm7Q==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=BbSuioGP;
	arc=none smtp.remote-ip=100.103.45.18
X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id F1A8C606A322;
	Thu,  3 Dec 2026 19:44:59 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id D1CB31F000C2;
	Thu,  3 Dec 2026 19:44:58 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1796327099;
	bh=UbaCsEmUV6YOMK9Lzi7cK/9Cas/bsHkjMZLYXYwaHBw=;
	h=Subject:From:To:Cc:Date:In-Reply-To:References;
	b=BbSuioGPMUl3xDY3XJB7snn+Y8eCupoOO/0E8P3H9wsnRMoJvtHo17Z+bPiI5D6iQ
	 A2apxN1ovkd0d8aMPXmisw3BF5QuxEGRGaIy5qmZEUzvp4GKX80zEatGTPUytGgqEB
	 NWaBv/8gKLe0KQgjYqzrarZFw4qSpygeYcQZn9bQoL5jVGN8zd1ZJt2/Auk0DCWWhk
	 q1UIaJSmlKECGHhMjnM9rAMXJIeIm40GMpSWLPccGRSTu3nPQgNukaGQvqwK7BGo1N
	 UPEheQsSdmAMQu/qBmsQcq5IelKIhOHGY4peEuDVEHbgXRUENXCgRBjck2NxB6Q/Hd
	 pwdmXyLDynLUw==
X-Cc: andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@kernel.org,
 kuba@kernel.org, netdev@vger.kernel.org, pabeni@redhat.com,
 willemb@google.com, gal@nvidia.com, leon@kernel.org,
 linux-rdma@vger.kernel.org, mbloch@nvidia.com, moshe@mellanox.com,
 moshe@nvidia.com, parav@mellanox.com, saeedm@mellanox.com, saeedm@nvidia.com,
 udar@nvidia.com
X-In-Reply-To: <20261202194317.3593082-1-tariqt@nvidia.com>
X-sashiko-severity: High
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list