Netdev List
 help / color / mirror / Atom feed
From: Petr Machata <petrm@nvidia.com>
To: Andrew Lunn <andrew+netdev@lunn.ch>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	<netdev@vger.kernel.org>
Cc: Ido Schimmel <idosch@nvidia.com>, Petr Machata <petrm@nvidia.com>,
	<mlxsw@nvidia.com>, Myeonghun Pak <mhun512@gmail.com>,
	Ijae Kim <ae878000@gmail.com>, Jiale Yao <yaojiale02@163.com>
Subject: [PATCH net-next 2/2] mlxsw: pci: Fix teardown order of CQ NAPI instances
Date: Mon, 16 Nov 2026 11:16:47 +0100	[thread overview]
Message-ID: <3a1a078f21028851b04ba4987fae490362b59cd4.1794823524.git.petrm@nvidia.com> (raw)
In-Reply-To: <cover.1794823524.git.petrm@nvidia.com>

In his report (see the link below), Myeonghun Pak <mhun512@gmail.com>
writes:

> mlxsw_pci_eq_irq_handler() schedules the EQ tasklet. The tasklet reads
> the EQ ring and schedules CQ NAPI instances. The CQ poll callbacks, in
> turn, dereference the RDQ or SDQ associated with the CQ.
>
> mlxsw_pci_fini() unregisters the IRQ and immediately tears down the
> asynchronous queues in RDQ, SDQ, CQ, EQ order. free_irq() waits for IRQ
> handlers, but not for a tasklet already scheduled by one. In addition,
> mlxsw_pci_cq_fini() disables each CQ NAPI only after all RDQs and SDQs
> have been freed. A pending tasklet or NAPI poll can therefore access
> freed queue storage.

The previous patch folded the tasklet into the IRQ handler, which takes
care of the first half of the problem: free_irq() in mlxsw_pci_fini() now
guarantees that nothing schedules CQ NAPI instances anymore. The second
half remains. A NAPI poll that was scheduled right before free_irq(), or
is currently running, can still dereference the RDQ or SDQ of its CQ, which
mlxsw_pci_aqs_fini() frees before the CQ, and where napi_disable() is
currently called.

In this patch, introduce a process-related duals to init / fini per-queue
callbacks: start and stop. Only CQ needs them for now, to manage the NAPI
instances. Invoke them at the end of mlxsw_pci_aqs_init() and at the
beginning of mlxsw_pci_aqs_fini(), respectively, so that NAPI is enabled
only once all queues are set up, and disabled before any of them is
destroyed. This also keeps napi_enable() and napi_disable() paired on error
paths of mlxsw_pci_aqs_init().

In his report, Myeonghun Pak notes that the issue was originally discovered
by static analysis.

Cc: Myeonghun Pak <mhun512@gmail.com>
Cc: Ijae Kim <ae878000@gmail.com>
Cc: Jiale Yao <yaojiale02@163.com>

Reported-by: Myeonghun Pak <mhun512@gmail.com>
Link: https://lore.kernel.org/netdev/20260721125753.35944-1-mhun512@gmail.com/
Reported-by: Jiale Yao <yaojiale02@163.com>
Link: https://lore.kernel.org/netdev/20261018040653.534066-1-yaojiale02@163.com/
Reviewed-by: Ido Schimmel <idosch@nvidia.com>
Signed-off-by: Petr Machata <petrm@nvidia.com>
---
 drivers/net/ethernet/mellanox/mlxsw/pci.c | 46 ++++++++++++++++++++++-
 1 file changed, 44 insertions(+), 2 deletions(-)

diff --git a/drivers/net/ethernet/mellanox/mlxsw/pci.c b/drivers/net/ethernet/mellanox/mlxsw/pci.c
index b2a7f568c74a..dc6cad5637d6 100644
--- a/drivers/net/ethernet/mellanox/mlxsw/pci.c
+++ b/drivers/net/ethernet/mellanox/mlxsw/pci.c
@@ -1059,7 +1059,6 @@ static int mlxsw_pci_cq_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
 	if (err)
 		goto err_page_pool_init;
 
-	napi_enable(&q->u.cq.napi);
 	mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q);
 	mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q);
 	return 0;
@@ -1074,12 +1073,23 @@ static void mlxsw_pci_cq_fini(struct mlxsw_pci *mlxsw_pci,
 {
 	enum mlxsw_pci_cq_type cq_type = mlxsw_pci_cq_type(mlxsw_pci, q);
 
-	napi_disable(&q->u.cq.napi);
 	mlxsw_pci_cq_page_pool_fini(q, cq_type);
 	mlxsw_pci_cq_napi_teardown(q);
 	mlxsw_cmd_hw2sw_cq(mlxsw_pci->core, q->num);
 }
 
+static void mlxsw_pci_cq_start(struct mlxsw_pci *mlxsw_pci,
+			       struct mlxsw_pci_queue *q)
+{
+	napi_enable(&q->u.cq.napi);
+}
+
+static void mlxsw_pci_cq_stop(struct mlxsw_pci *mlxsw_pci,
+			      struct mlxsw_pci_queue *q)
+{
+	napi_disable(&q->u.cq.napi);
+}
+
 static u16 mlxsw_pci_cq_elem_count(const struct mlxsw_pci_queue *q)
 {
 	return q->u.cq.v == MLXSW_PCI_CQE_V2 ? MLXSW_PCI_CQE2_COUNT :
@@ -1198,6 +1208,10 @@ struct mlxsw_pci_queue_ops {
 		    struct mlxsw_pci_queue *q);
 	void (*fini)(struct mlxsw_pci *mlxsw_pci,
 		     struct mlxsw_pci_queue *q);
+	void (*start)(struct mlxsw_pci *mlxsw_pci,
+		      struct mlxsw_pci_queue *q);
+	void (*stop)(struct mlxsw_pci *mlxsw_pci,
+		     struct mlxsw_pci_queue *q);
 	u16 (*elem_count_f)(const struct mlxsw_pci_queue *q);
 	u8 (*elem_size_f)(const struct mlxsw_pci_queue *q);
 	u16 elem_count;
@@ -1225,6 +1239,8 @@ static const struct mlxsw_pci_queue_ops mlxsw_pci_cq_ops = {
 	.pre_init	= mlxsw_pci_cq_pre_init,
 	.init		= mlxsw_pci_cq_init,
 	.fini		= mlxsw_pci_cq_fini,
+	.start		= mlxsw_pci_cq_start,
+	.stop		= mlxsw_pci_cq_stop,
 	.elem_count_f	= mlxsw_pci_cq_elem_count,
 	.elem_size_f	= mlxsw_pci_cq_elem_size
 };
@@ -1349,6 +1365,28 @@ static void mlxsw_pci_queue_group_fini(struct mlxsw_pci *mlxsw_pci,
 	kfree(queue_group->q);
 }
 
+static void mlxsw_pci_queue_group_start(struct mlxsw_pci *mlxsw_pci,
+					const struct mlxsw_pci_queue_ops *q_ops)
+{
+	struct mlxsw_pci_queue_type_group *queue_group;
+	int i;
+
+	queue_group = mlxsw_pci_queue_type_group_get(mlxsw_pci, q_ops->type);
+	for (i = 0; i < queue_group->count; i++)
+		q_ops->start(mlxsw_pci, &queue_group->q[i]);
+}
+
+static void mlxsw_pci_queue_group_stop(struct mlxsw_pci *mlxsw_pci,
+				       const struct mlxsw_pci_queue_ops *q_ops)
+{
+	struct mlxsw_pci_queue_type_group *queue_group;
+	int i;
+
+	queue_group = mlxsw_pci_queue_type_group_get(mlxsw_pci, q_ops->type);
+	for (i = 0; i < queue_group->count; i++)
+		q_ops->stop(mlxsw_pci, &queue_group->q[i]);
+}
+
 static int mlxsw_pci_aqs_init(struct mlxsw_pci *mlxsw_pci, char *mbox)
 {
 	struct pci_dev *pdev = mlxsw_pci->pdev;
@@ -1426,6 +1464,8 @@ static int mlxsw_pci_aqs_init(struct mlxsw_pci *mlxsw_pci, char *mbox)
 		goto err_rdqs_init;
 	}
 
+	mlxsw_pci_queue_group_start(mlxsw_pci, &mlxsw_pci_cq_ops);
+
 	return 0;
 
 err_rdqs_init:
@@ -1439,6 +1479,8 @@ static int mlxsw_pci_aqs_init(struct mlxsw_pci *mlxsw_pci, char *mbox)
 
 static void mlxsw_pci_aqs_fini(struct mlxsw_pci *mlxsw_pci)
 {
+	mlxsw_pci_queue_group_stop(mlxsw_pci, &mlxsw_pci_cq_ops);
+
 	mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_rdq_ops);
 	mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_sdq_ops);
 	mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_cq_ops);
-- 
2.56.1


  parent reply	other threads:[~2026-11-16 10:19 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16 10:16 [PATCH net-next 0/2] mlxsw: Fix teardown order of CQ NAPI instances Petr Machata
2026-11-16 10:16 ` [PATCH net-next 1/2] mlxsw: pci: Process EQ in the IRQ handler instead of a tasklet Petr Machata
2026-11-16 10:16 ` Petr Machata [this message]
2026-11-16 10:21 ` [PATCH net-next 0/2] mlxsw: Fix teardown order of CQ NAPI instances netdev-bot+sinfo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3a1a078f21028851b04ba4987fae490362b59cd4.1794823524.git.petrm@nvidia.com \
    --to=petrm@nvidia.com \
    --cc=ae878000@gmail.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=idosch@nvidia.com \
    --cc=kuba@kernel.org \
    --cc=mhun512@gmail.com \
    --cc=mlxsw@nvidia.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=yaojiale02@163.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox